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

发布于 2024-12-16 14:37:26 字数 101 浏览 1 评论 0原文

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

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

Android: how to set the height/width of the image(src image intead of the background) of a ImageButton?

I want to set the height/width of the top level image(not the background) image, the size of image should be customized instead of fullfill the button automatically

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

面如桃花 2024-12-23 14:37:26

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

像这样

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

You can just Use the scaleType attribute of ImageButton.
Set it to fitXY or fitCenter or anything else according to your need.

like this

<ImageButton
    android:id="@+id/imageButton"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:scaleType="fitCenter"
    android:src="@drawable/some_image">
</ImageButton>
夜巴黎 2024-12-23 14:37:26

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

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

You can use Bitmap.createScaledBitmap to scale the image to the size you want.

Bitmap image = ... //load image from your source
image = Bitmap.createScaledBitmap(image, desiredHeight, desiredWidth, true);
带上头具痛哭 2024-12-23 14:37:26

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

我做了什么——

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

奇怪...但它有效。

谢谢,

I had same problem... change the size of 'src' image of image button(not the size of button just image only).

what i did--

I moved those '.png' file from 'drawable' folder to 'drawable-hdpi' folder.

weird... but it worked.

thanks,

胡大本事 2024-12-23 14:37:26

填充和缩放类型设置为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"/>

set padding and scaletype to centerInside

Padding will help you customize the source image to provide the required height and width.

 <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"/>
残疾 2024-12-23 14:37:26

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代码)基于https://thinkandroid.wordpress.com/2009/12/25/resizing-a-bitmap/

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

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

EDIT: (java code) based on https://thinkandroid.wordpress.com/2009/12/25/resizing-a-bitmap/

// 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
            )
    );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文