【问题标题】:Images dont scale properly图像无法正确缩放
【发布时间】:2014-01-27 07:46:56
【问题描述】:

我的图像不会按应有的方式缩放。据我了解,如果将图像放在 xhdpi 文件夹中,Android 会为我缩小图像,这不是性能最佳,但应该可以工作,不是吗?

这是我如何启动和绘制所有位图的示例:

public Bitmap loadBitmap(int resourceID) {
    Options options = new BitmapFactory.Options();
    options.inPreferredConfig = Bitmap.Config.RGB_565;
    Bitmap tempBmp = null;
    try {
        tempBmp = BitmapFactory.decodeResource(getResources(), resourceID,
                options);
    } catch (OutOfMemoryError e) {

    } catch (Error e) {

    }
    return tempBmp;
}

然后加载它:

bitmap = loadBitmap(R.drawable.example);

绘图:

canvas.drawBitmap(screenBitmap, screenX, screenY, null);

因为这是我正在开发的游戏,所以我在屏幕上有多个这样的游戏。

可能应该补充一点,我在模拟器中遇到了缩放问题,因为除了我自己的 Android 之外我没有任何其他 Android 可以测试,而且在我自己的手机上一切都合适,因为我就是这样测试一下。

是我遗漏了什么还是模拟器在搞乱我?

【问题讨论】:

    标签: java android bitmap scale


    【解决方案1】:

    位图解码还可以根据屏幕 dpi 缩放位图。 尝试使用这个附加参数(设置为 true 或 false):

    Options options = new BitmapFactory.Options();
    options.inScaled = false;
    options.inPreferredConfig = Bitmap.Config.RGB_565;
    

    【讨论】:

    • 您知道例如x=bitmap.getWidth()/2 在重新缩放后是否会具有相同的值还是会改变?我对此没有任何意义,有些事情是按比例的,有些事情不是。有些东西位置正确,有些则不正确。我使用相同的方法
    【解决方案2】:
    public Bitmap decodeAndResizeFile(File f) {
            try {
                // Decode image size
                BitmapFactory.Options o = new BitmapFactory.Options();
                o.inJustDecodeBounds = true;
                BitmapFactory.decodeStream(new FileInputStream(f), null, o);
    
                // The new size we want to scale to
                final int REQUIRED_SIZE = 70;
    
                // Find the correct scale value. It should be the power of 2.
                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 o2 = new BitmapFactory.Options();
                o2.inSampleSize = scale;
                return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
            } catch (FileNotFoundException e) {
            }
            return null;
        }
    

    Uri selectedImage = data.getData(); String[] filePathColumn = { MediaStore.Images.Media.DATA }; 光标 cursor = getContentResolver().query(selectedImage, 文件路径列,空,空,空); cursor.moveToFirst(); int columnIndex = cursor.getColumnIndex(filePathColumn[0]); String picturePath = cursor.getString(columnIndex);

    文件file = new File(picturePath);// 位图 bmpp = decodeAndResizeFile(file);

    【讨论】:

    • 你能编辑和解释一下吗?为什么你将大小设置为 70?
    • 百分比 70% 比例图像
    • 这个方法会根据屏幕分辨率自动缩放吗?很难理解。
    猜你喜欢
    • 2020-10-12
    • 1970-01-01
    • 2011-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-08
    • 2018-04-10
    相关资源
    最近更新 更多