How to control image size in the menu?

I have a menu with images. I have large 72x72 images. In the activity layout, I use:

android:layout_height="55dip"
android:layout_width="55dip"
android:scaleType="fitCenter" - this works fine.

But in the menu items I do not know how to do the same.

+5
source share
3 answers
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
    MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu,menu);
 menu.findItem(R.id.menu_Help).setIcon(resizeImage(R.drawable.ic_noaction_help,108,108));
    return true;
}

private Drawable resizeImage(int resId, int w, int h)
{
      // load the origial Bitmap
      Bitmap BitmapOrg = BitmapFactory.decodeResource(getResources(), resId);
      int width = BitmapOrg.getWidth();
      int height = BitmapOrg.getHeight();
      int newWidth = w;
      int newHeight = h;
      // calculate the scale
      float scaleWidth = ((float) newWidth) / width;
      float scaleHeight = ((float) newHeight) / height;
      // create a matrix for the manipulation
      Matrix matrix = new Matrix();
      matrix.postScale(scaleWidth, scaleHeight);
      Bitmap resizedBitmap = Bitmap.createBitmap(BitmapOrg, 0, 0,width, height, matrix, true);
      return new BitmapDrawable(resizedBitmap);
}
+10
source

Improving Kostadin's answer above to save memory by avoiding creating the first bitmap that he calls BitmapOrgusing the described approach http://developer.android.com/training/displaying-bitmaps/load-bitmap.html .

0
source

onCreateOptionsMenu:

getMenuInflater().inflate(R.menu.my_menu, menu);
menu.findItem(R.id.action_overflow).setIcon(resizeImage(R.mipmap.more,72,72));
0

All Articles