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) im
Solution 1:
You can just Use the scaleType
attribute of ImageButton
.
Set it to fitXY
or fitCenter
or anything else according to your need.
like this
<ImageButtonandroid:id="@+id/imageButton"android:layout_width="100dp"android:layout_height="100dp"android:scaleType="fitCenter"android:src="@drawable/some_image"></ImageButton>
Solution 2:
You can use Bitmap.createScaledBitmap to scale the image to the size you want.
Bitmapimage= ... //load image from your source
image = Bitmap.createScaledBitmap(image, desiredHeight, desiredWidth, true);
Solution 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)BitmapbitmapOrg= BitmapFactory.decodeResource(getResources(),
R.drawable.android);
intwidth= bitmapOrg.width();
intheight= bitmapOrg.height();
intnewWidth=200;
intnewHeight=200;
// calculate the scale - in this case = 0.4ffloatscaleWidth= ((float) newWidth) / width;
floatscaleHeight= ((float) newHeight) / height;
// create a matrix for the manipulationMatrixmatrix=newMatrix();
// resize the bit map
matrix.postScale(scaleWidth, scaleHeight);
// rotate the Bitmap
matrix.postRotate(45);
// recreate the new BitmapBitmapresizedBitmap= 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 everBitmapDrawablebmd=newBitmapDrawable(resizedBitmap);
ImageViewimageView=newImageView(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,
newLinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT
)
);
Solution 4:
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,
Solution 5:
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"/>
Post a Comment for "Android: How To Set The Height/width Of The Src Image Of A Imagebutton?"