16

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

George_BJ
  • 596
  • 2
  • 9
  • 21
  • possible duplicate : http://stackoverflow.com/questions/6899088/adjusting-the-size-of-an-imagebutton-in-android –  Nov 17 '11 at 09:00
  • 2
    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. – George_BJ Nov 17 '11 at 09:03

5 Answers5

21

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>
sfmirtalebi
  • 370
  • 7
  • 16
  • 5
    This does not answer the question. The question is asking for a way to set the size of `android:src` instead of the whole `ImageButton`. E.g. having a `ImageButton` that is 100dp x 100dp with src image at 35dp x 35dp – Weizhi May 25 '17 at 03:55
6

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);
Chris
  • 343
  • 3
  • 9
  • According to the [docs](https://developer.android.com/reference/android/graphics/Bitmap.html#createScaledBitmap) you put the sizes inverted, you need to make like this: `image = Bitmap.createScaledBitmap(image, desiredWidth, desiredHeight, true);` – Felipe Augusto Apr 22 '17 at 21:38
3

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,

Zakynthos
  • 101
  • 1
  • 12
3

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"/>
Extremis II
  • 5,185
  • 2
  • 19
  • 29
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"
/>

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
            )
    );
duggu
  • 37,851
  • 12
  • 116
  • 113
damson
  • 2,635
  • 3
  • 21
  • 29