如何控制图像的大小,菜单?图像、菜单、大小

2023-09-05 10:19:37 作者:不努力谁替你成长

我和图像菜单。我有一双大图像的72x72。 在活动的布局我使用:

 安卓layout_height =55dip
机器人:layout_width =55dip
机器人:scaleType =fitCenter - 这工作得很好。
 

但在菜单项,我不知道该怎么做。

解决方案

  @覆盖
公共布尔onCreateOptionsMenu(菜单菜单)
{
    MenuInflater充气= getMenuInflater();
inflater.inflate(R.menu.menu,菜单);
 menu.findItem(R.id.menu_Help).setIcon(resizeImage(R.drawable.ic_noaction_help,108108));
    返回true;
}

私人绘制对象resizeImage(INT渣油,INT W,INT高)
{
      //将origial位图
      位图BitmapOrg = BitmapFactory.de codeResource(getResources(),渣油);
      INT宽度= BitmapOrg.getWidth();
      INT高= BitmapOrg.getHeight();
      INT newWidth = W;
      INT newHeight = H;
      //计算比例
      浮动scaleWidth =((浮点)newWidth)/宽度;
      浮动scaleHeight =((浮点)newHeight)/身高;
      //创建矩阵用于操作
      字模=新的Matrix();
      matrix.postScale(scaleWidth,scaleHeight);
      位图resizedBitmap = Bitmap.createBitmap(BitmapOrg,0,0,宽度,高度,矩阵,真);
      返回新BitmapDrawable(resizedBitmap);
}
 

如何应用图像菜单修改画布大小 图像大小

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);
}

 
精彩推荐