【问题标题】:Why does my image from the android gallery not scale down?为什么我来自 android 库的图像没有按比例缩小?
【发布时间】:2012-06-02 04:15:28
【问题描述】:

我想缩小图像以使其适合我的图像按钮的大小。在这段代码中,我打开图库,然后可以选择一张图片。问题是图像并没有按比例缩小,尽管我在我的代码中这么说。 为什么这不起作用?

private static final int NEW_WIDTH = 10;
private static final int NEW_HEIGHT = 10;

protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) { 
    super.onActivityResult(requestCode, resultCode, imageReturnedIntent); 

    switch(requestCode) { 
    case SELECT_PHOTO:
        if(resultCode == RESULT_OK){  
            Uri selectedImage = imageReturnedIntent.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 filePath = cursor.getString(columnIndex);
            cursor.close();


            Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath);
            Bitmap.createScaledBitmap(yourSelectedImage, NEW_WIDTH, NEW_HEIGHT, false);
            mImageButton.setImageBitmap(yourSelectedImage);
        }
    }
}

【问题讨论】:

标签: android image resize


【解决方案1】:

好的,我自己解决了这个问题。

代替:

Bitmap.createScaledBitmap(yourSelectedImage, NEW_WIDTH, NEW_HEIGHT, false);
mImageButton.setImageBitmap(yourSelectedImage);

输入:

Bitmap bMapScaled = Bitmap.createScaledBitmap(yourSelectedImage, NEW_WIDTH, NEW_HEIGHT, true);
mImageButton.setImageBitmap(bMapScaled);

我发现这篇文章很有帮助: http://www.higherpass.com/Android/Tutorials/Working-With-Images-In-Android/3/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-12
    • 1970-01-01
    • 2015-09-16
    • 1970-01-01
    • 2011-06-25
    • 1970-01-01
    • 2013-12-27
    相关资源
    最近更新 更多