问题
My app contains buttons with images on them, set by using setCompoundDrawablesWithIntrinsicBounds. I use images from the app's drawables folder, but also use images downloaded from the web and stored on SD card. I found that I needed to upscale the SD card images so they'd render as the same size as the images in drawables. I did this using:
Options opts = new BitmapFactory.Options();
opts.inDensity = 160;
Bitmap bm = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory() +
context.getResources().getString(R.string.savefolder) + iconfile, opts);
myIcon = new BitmapDrawable(context.getResources(), bm);
btn.setCompoundDrawablesWithIntrinsicBounds(myIcon, null, null, null );
This has worked with no problems, until I updated my phone to Android 4.1.1 and noticed that the downloaded images were now appearing at a much smaller size than those from the drawable folder.
I messed around with the inDensity value to little effect, but had more success with scaling the bitmap based on a btnheight value (just the height of the button the image sits on):
int intoffset=bm.getHeight() - bm.getWidth();
myIcon = new BitmapDrawable(context.getResources(),
Bitmap.createScaledBitmap(bm, btnheight - (((btnheight/100)*10) +
intoffset) , btnheight - ((btnheight/100)*10), true));
This sort of works, but the image is still a little bigger than the button it sits on (which shouldn't be the case, based on the above, as it should scale the image height to 90% of the button height.) I did this as a test. I can't use this method in my app as the button height changes in accordance with the font size displayed on the buttons, and the user can change this font size in the app preferences.
As an aside, I found that, oddly(?), by scaling the bitmap to twice it's original height using
Bitmap.createScaledBitmap(bm, bm.getWidth() * 2
, bm.getHeight() * 2, true));
It rendered correctly, (well, it was displayed at the same size as the drawable icons) in 4.0.3 and 4.1.1, but behaved as you'd expect (rendered bigger than the button it sits on) in 2.1.
If anyone has any insights as to why this happens in 4.1.1, and what I can do so my decodeFile bitmaps render at the same size as my drawable bitmaps, without having to code for 4.1.1 separately, it would be much appreciated!
回答1:
Modifying my original code to be as below works on 4.1.1 as well as the previous versions I tested it on...
Options opts = new BitmapFactory.Options();
DisplayMetrics dm = new DisplayMetrics();
context.getWindowManager().getDefaultDisplay().getMetrics(dm);
int dpiClassification = dm.densityDpi;
opts.inDensity = dm.DENSITY_MEDIUM;
opts.inTargetDensity = dpiClassification;
opts.inScaled =true;
Bitmap bm = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory() +
context.getResources().getString(R.string.savefolder) + iconfile, opts);
myIcon = new BitmapDrawable(context.getResources(), bm);
btn.setCompoundDrawablesWithIntrinsicBounds(myIcon, null, null, null );
来源:https://stackoverflow.com/questions/14430893/resize-images-and-setcompounddrawableswithintrinsicbounds