Scale drawableLeft in button with text

一笑奈何 提交于 2019-12-05 05:54:58

You can resize an image on basis of the dimension of a Button, use getHeight() and getWidth() methods to get the size of the button and use following function to resize image for Button:

Resizing a Bitmap:

public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {

int width = bm.getWidth();

int height = bm.getHeight();

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

// recreate the new Bitmap

Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);

return resizedBitmap;

}

Now use this resized image for your button. Reference

Can U create a button (containing all) like that in graphics.

Or U can try with this>> 1. Take a Layout(Relative or Linear) with width and height same as your button size. 2. Now put the image view and text view within that. 3. Make layout clickable..

yonojoy

You can use a function made of following code in onCreate():

    //get left drawable
    Drawable drawable = btn.getCompoundDrawables()[0];
    int imgHeight = drawable.getIntrinsicHeight();
    int imgWidth = drawable.getIntrinsicWidth();
    //for dpToPixel look here: https://stackoverflow.com/a/6327095/2306907
    float scale = (float) (dpToPixel(40)) / imgHeight;
    Rect rect = drawable.getBounds();
    int newWidth = (int)(imgWidth * scale);
    int newHeight = (int)(imgHeight * scale);
    rect.left = rect.left + (int)(0.5 * (imgWidth - newWidth)); 
    rect.top = rect.top + (int)(0.5 * (imgHeight - newHeight));
    rect.right = rect.left + newWidth;
    rect.bottom = rect.top + newHeight;
    drawable.setBounds(rect);

or have a look at this answer, which tries to give an "universal" solution.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!