图像不能正确缩放

时间:2014-01-27 07:46:57

标签: java android bitmap scale

我的图片无法按照预期的方式进行扩展。据我所知,如果将图像放入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);

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

可能应该补充一点,我在模拟器中遇到了缩放问题,因为我没有任何其他的机器人,而是我自己进行测试,并且在我自己的手机上它一切都适合,因为这就是我的意思测试它。

我是否遗漏了某些东西,或者模拟器弄乱了我?

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

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 cursor = getContentResolver()。query(selectedImage,                     filePathColumn,null,null,null);             cursor.moveToFirst();             int columnIndex = cursor.getColumnIndex(filePathColumn [0]);             String picturePath = cursor.getString(columnIndex);

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

相关问题