如何在菜单中控制图像大小? [英] How to control image size in menu?
本文介绍了如何在菜单中控制图像大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有带图片的菜单.我有 72x72 的大图像.在我使用的活动布局中:
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.
推荐答案
@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);
}
这篇关于如何在菜单中控制图像大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文