Image resizing and setCompoundDrawablesWithIntrinsicBounds

My application contains buttons with images on them set using setCompoundDrawablesWithIntrinsicBounds. I use images from the appablesables folder, but I also use images downloaded from the Internet and stored on the SD card. I found that I need to enlarge the image sizes of the SD cards so that they appear at the same size as the images in the drawings. 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 worked without problems until I upgraded my phone to Android 4.1.1 and noticed that the downloaded images now look much smaller than those that can be extracted from the folder.

I messed up the inDensity value for a small effect, but was more successful at scaling a bitmap based on the btnheight value (only the height of the button the image is sitting 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 kind of work, but the image is still slightly larger than the button on which it sits (which should not be the case, based on the foregoing, as it should scale the image height to 90% of the button height.) I did this as a test. I cannot use this method in my application, since the height of the button changes according to the font size displayed on the buttons, and the user can change this font size in the application settings.

As an aside, I found that, strange (?), By scaling a bitmap twice its original height using

  Bitmap.createScaledBitmap(bm, bm.getWidth() * 2 , bm.getHeight() * 2, true)); 

It is displayed correctly (well, it was displayed at the same size as the pushed icons) in 4.0.3 and 4.1.1, but it behaved as you expected (it was displayed more than the button on which it sits) in 2.1.

If anyone has any thoughts as to why this happens in 4.1.1 and what I can do, my decodeFile bitmaps will be displayed at the same size as my drawable bitmaps, without the need for coding for 4.1 .1 separately, it will be much appreciated!

+4
source share
1 answer

Changing my source code, as shown below, works on 4.1.1, as well as in previous versions on which I tested it ...

  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 ); 
+7
source

All Articles