简体   繁体   English

如何在运行时设置android:drawableTop之类的按钮中设置drawable(改变大小)

[英]How to set drawable(with changed size) in button like android:drawableTop at Runtime

I am fetching number of images and text related to it from server Now i want to set each image with text(at bottom) in LinearLayout 我从服务器获取与其相关的图像和文本的数量现在我想在LinearLayout中设置每个图像的文本(在底部)

I got the answer of second part of the question from here 我从这里得到了问题第二部分的答案

button.setCompoundDrawables(left, top, right, bottom);

But problem with this is I am getting images of different sizes and want to resize them 但问题是我得到了不同大小的图像,并希望调整它们的大小

I am succeded to resizing Button by using Layout Params but with 我成功通过使用布局参数调整按钮的大小,但是

setCompoundDrawable(left,top,right,bottom); image doesnt get resized 图像没有调整大小

How can i achieve this?? 我怎么能实现这个?

I hope below code was working for you Because Its work fine with me 我希望下面的代码对你有用因为它的工作正常

   Bitmap bitmap = ImageResizeUtility.resizeBitmap(bitmap, 100, 110);

Use this class to resize the images 使用此类可以调整图像大小

public class ImageResizeUtility
{

    public static Bitmap resizeBitmap(final Bitmap bitmap, final int width, final int height)
    {
        final int oldWidth = bitmap.getWidth();
        final int oldHeight = bitmap.getHeight();
        final int newWidth = width;
        final int newHeight = height;

        // calculate the scale
        final float scaleWidth = ((float) newWidth) / oldWidth;
        final float scaleHeight = ((float) newHeight) / oldHeight;

        // create a matrix for the manipulation
        final Matrix matrix = new Matrix();
        // resize the Bitmap
        matrix.postScale(scaleWidth, scaleHeight);
        // if you want to rotate the Bitmap

        // recreate the new Bitmap
        final Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, oldWidth, oldHeight, matrix, true);

        return resizedBitmap;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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