3

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

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??

Community
  • 1
  • 1
silwar
  • 6,470
  • 3
  • 46
  • 66

1 Answers1

1

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;
    }
Nik88
  • 1,037
  • 10
  • 21
  • Hi i am creating bitmap from drawable and at the time of applying your code it gives me height and width of bitmap as -1. Is my implementation correct? i am using ((BitmapDrawable)d).getBitmap(); to get bitmap from drawable – silwar Oct 21 '11 at 12:39
  • have you check that bitmap is not null. It is possible that if bitmap is not available than it was return height and width null. – Nik88 Oct 21 '11 at 13:19
  • If the code is helpful to you than please check accept sign at the left corner. Thanks user557157 – Nik88 Oct 21 '11 at 13:19