我正在使用 SurfaceView 绘制位图图像。但问题是当我在 SurfaceView 上设置图像时,它看不到整个图像。
我认为这是因为图像尺寸大于设备屏幕尺寸。
所以我想设置应该用设备大小/SurfaceView 大小设置的位图。
如果图像比 SurfaceView 小,那么它应该是图像大小并且位于 surfaceView 的中心。
最佳答案
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.
关于android - 如何设置位图大小以在 SurfaceView 上正确显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8018316/