简体   繁体   中英

Android: how to set the height/width of the src image of a 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

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>

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

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,

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)

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM