Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
304 views
in Technique[技术] by (71.8m points)

android - How to control image size in menu?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
@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);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...