如何设置位图大小以在SurfaceView上显示它

时间:2011-11-05 05:09:39

标签: android android-layout android-emulator bitmap

我使用SurfaceView绘制位图图像。但问题是,当我在SurfaceView上设置图像时,它不会看到整个图像。

我认为它发生了,因为imahe尺寸大于设备屏幕尺寸。

所以我想设置应该使用设备大小/ SurfaceView大小设置的位图。

如果图像小于SurfaceView,那么它应该是图像大小并且位于surfaceView的中心。

1 个答案:

答案 0 :(得分:3)

BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;

FileInputStream fis = new FileInputStream(Filepath);
final int REQUIRED_SIZE=100;//what ever your surface size or require size

int width_tmp=o.outWidth, height_tmp=o.outHeight;
int scale=1;

while(true){
if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE)
break;
width_tmp/=2;
height_tmp/=2;
scale*=2;

}

BitmapFactory.Options op = new BitmapFactory.Options();
op.inSampleSize = scale;
bitmap = BitmapFactory.decodeStream(fis, null, op);

use this bitmap.