Android: 如何设置 ImageButton 的 src 图像的高度/宽度?

16

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

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


可能是重复问题:https://dev59.com/L2w05IYBdhLWcg3w_W1N - user319198
2
我想设置顶层图像(不是背景)的高度/宽度,图像的大小应该是自定义的,而不是自动填充按钮。 - George_BJ
5个回答

21
您可以直接使用ImageButtonscaleType属性。 根据需要将其设置为fitXYfitCenter或任何其他值。 像这样:
<ImageButton
    android:id="@+id/imageButton"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:scaleType="fitCenter"
    android:src="@drawable/some_image">
</ImageButton>

5
这并没有回答问题。问题是要求一种方法来设置 android:src 图像的大小,而不是整个 ImageButton 的大小。例如,拥有一个大小为100dp x 100dp的 ImageButton,其 src 图像大小为35dp x 35dp。 - Weizhi

6
你可以使用Bitmap.createScaledBitmap将图像缩放到所需的大小。
Bitmap image = ... //load image from your source
image = Bitmap.createScaledBitmap(image, desiredHeight, desiredWidth, true);

根据文档,您把尺寸倒置了,需要这样做:image = Bitmap.createScaledBitmap(image, desiredWidth, desiredHeight, true); - Felipe Augusto

3

我曾经遇到过同样的问题...解决方法是调整图像按钮中'src'图片的大小(不是按钮的大小,只是图片大小)。

我的做法是--

将那些'.png'文件从'drawable'文件夹移动到'drawable-hdpi'文件夹中。

很奇怪...但是它起作用了。

谢谢,


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

它只是调整整个按钮的宽度和高度,我想要调整源图像。 - George_BJ
谢谢,这对我来说有点复杂。我转而使用另一个图标来操作。 - George_BJ

3

paddingscaletype设置为centerInside

Padding可以帮助您自定义源图像,以提供所需的高度和宽度。

 <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"/>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接