【问题标题】:Android: how to set the height/width of the src image of a ImageButton?Android:如何设置 ImageButton 的 src 图像的高度/宽度?
【发布时间】:2011-11-17 08:56:17
【问题描述】:

Android:如何设置 ImageButton 的图像(src 图像而不是背景)的高度/宽度?​​

我想设置顶层图片(不是背景)图片的高度/宽度,图片的大小应该自定义而不是自动填充按钮

【问题讨论】:

标签: android imagebutton


【解决方案1】:

您可以只使用ImageButtonscaleType 属性。 根据您的需要将其设置为fitXYfitCenter 或其他任何内容。

喜欢这个

<ImageButton
    android:id="@+id/imageButton"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:scaleType="fitCenter"
    android:src="@drawable/some_image">
</ImageButton>

【讨论】:

  • 这没有回答问题。问题是寻求一种方法来设置android:src 的大小而不是整个ImageButton。例如。有一个ImageButton,即 100dp x 100dp,src 图像为 35dp x 35dp
【解决方案2】:

您可以使用 Bitmap.createScaledBitmap 将图像缩放到您想要的大小。

Bitmap image = ... //load image from your source
image = Bitmap.createScaledBitmap(image, desiredHeight, desiredWidth, true);

【讨论】:

  • 根据docs你把尺寸倒过来,你需要这样:image = Bitmap.createScaledBitmap(image, desiredWidth, desiredHeight, true);
【解决方案3】:

xml

<ImageButton
android:id="@+id/picture_id"
android:layout_width="10dp"  //here width with unit. 5 dp exemple
android:layout_height="3dp"  //here height with unit. 3 dp exemple
android:src="@drawable/picture_name"
/>

编辑:(java 代码)

// load the origial BitMap (500 x 500 px)
    Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),
           R.drawable.android);

    int width = bitmapOrg.width();
    int height = bitmapOrg.height();
    int newWidth = 200;
    int newHeight = 200;

    // calculate the scale - in this case = 0.4f
    float scaleWidth = ((float) newWidth) / width;
    float scaleHeight = ((float) newHeight) / height;

    // create a matrix for the manipulation
    Matrix matrix = new Matrix();
    // resize the bit map
    matrix.postScale(scaleWidth, scaleHeight);
    // rotate the Bitmap
    matrix.postRotate(45);

    // recreate the new Bitmap
    Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0,
                      width, height, matrix, true);

    // make a Drawable from Bitmap to allow to set the BitMap
    // to the ImageView, ImageButton or what ever
    BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);

    ImageView imageView = new ImageView(this);

    // set the Drawable on the ImageView
    imageView.setImageDrawable(bmd);

    // center the Image
    imageView.setScaleType(ScaleType.CENTER);

    // add ImageView to the Layout
    linLayout.addView(imageView,
            new LinearLayout.LayoutParams(
                  LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT
            )
    );

【讨论】:

  • 它只是调整整个按钮的宽度和高度,我想调整src图像。
  • 谢谢,这对我来说有点复杂。我转身用另一个图标四处走动。
【解决方案4】:

我遇到了同样的问题...更改图像按钮的“src”图像的大小(不是按钮的大小,只是图像)。

我做了什么--

我将那些“.png”文件从“drawable”文件夹移动到“drawable-hdpi”文件夹。

很奇怪……但它奏效了。

谢谢,

【讨论】:

    【解决方案5】:

    padding 和 scaletype 设置为 centerInside

    填充将帮助您自定义源图像以提供所需的高度和宽度。

     <ImageButton
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:src="@drawable/location"
                android:padding="10dp"
                android:scaleType="centerInside"
                android:background="@drawable/blue_circle_border"/>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-09-05
      • 2012-03-01
      • 2014-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多