2013-11-01 29 views
0

用800 * 480創建背景資源如果其他照片的寬度爲< = height.whatever這張照片的像素會變大或變大!老是唧唧的背景資源這張照片配合,照片必須是在後臺的資源範圍內招!我怎麼能明白嗎?謝謝java.lang.IllegalArgumentException:x width必須是<= bitmap.width()

private Bitmap scale(Bitmap origin) { 
    Bitmap bitmap = Bitmap.createBitmap(BitmapFactory.decodeResource(
      getResources(), R.drawable.example_bg), 0, 0, 800, 480); 
    Canvas canvas = new Canvas(bitmap); 
    Rect target = null; 
    int orgWidth = origin.getWidth(); 
    int orgHeight = origin.getHeight(); 
    int bgHeight = bitmap.getHeight(); 
    int bgWidth = bitmap.getWidth(); 
    if (orgWidth * bgHeight > orgHeight * bgWidth) { 
     int newWidth = bgWidth, newHeight = newWidth * orgHeight/orgWidth; 
     target = new Rect(0, (bgHeight - newHeight)/2, newWidth, 
       (bgHeight + newHeight)/2); 
    } else { 
     int newHeight = bgHeight, newWidth = newHeight * orgWidth 
       /orgHeight; 
     target = new Rect((bgWidth - newWidth)/2, 0, 
       (bgWidth + newWidth)/2, newHeight); 
    } 
    canvas.drawBitmap(origin, null, target, new Paint(Paint.DITHER_FLAG 
      | Paint.FILTER_BITMAP_FLAG)); 
    return bitmap; 

} 

回答

0

只是使用此代碼你可能會得到你的答案......

canvas.drawBitmap(Bitmap.createScaledBitmap(origin, 480, 800, true), 0,0, new Paint(Paint.DITHER_FLAG 
     | Paint.FILTER_BITMAP_FLAG)); 
+0

這不是真的工作,如何在背景中的照片移動! – Goman

相關問題