Reputation: 2559
I have menu with images. I have big images 72x72. In activity layout I'm using:
android:layout_height="55dip"
android:layout_width="55dip"
android:scaleType="fitCenter" - this works fine.
But in menu items I don't know how to do the same.
Upvotes: 7
Views: 7981
Reputation: 723
Inside onCreateOptionsMenu
:
getMenuInflater().inflate(R.menu.my_menu, menu);
menu.findItem(R.id.action_overflow).setIcon(resizeImage(R.mipmap.more,72,72));
Upvotes: -1
Reputation: 98
An improvement to Kostadin's answer above is to save memory by avoiding the creation of the first bitmap he calls BitmapOrg
, by using the approach described http://developer.android.com/training/displaying-bitmaps/load-bitmap.html.
Upvotes: 1
Reputation: 2559
@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);
}
Upvotes: 13