android - 如何设置位图大小以在 SurfaceView 上正确显示

标签 android android-layout android-emulator bitmap

我正在使用 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/

相关文章:

java - 错误: Cannot find symbol variable when trying to establish imageviews

android - 未知动画名称 : objectAnimator. 使用九个旧机器人。仍然出现错误

android - 打开跟踪文件时出错 : No such file or directory (2)

Android SDK 连接到主机上的 Web 服务器

android - 在 Android Studio 2 模拟器上登录 Google 帐户挂起

android - Android 中使用视频 View 的视频横向支持

android - fragment 的 dispatchTouchEvent 等价物是什么?

java - 如何使用按钮覆盖 TextView 中的文本?

android - ScrollView 不会滚动

android - AppCompat dividerPadding 和 showDividers