Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resize Drawable in Android

I am setting a drawable for a progress dialog (pbarDialog) but my issue is I want to resize the drawable each time but can't figure out how.

Here is some code:

Handler progressHandler = new Handler() {      public void handleMessage(Message msg) {         switch (msg.what) {             // some more code             case UPDATE_PBAR:                 pbarDialog.setIcon(mAppIcon);                 pbarDialog.setMessage(mPbarMsg);                 pbarDialog.incrementProgressBy(mIncrement+1);                 break;         }     } };  pbarDialog.show();  Thread myThread = new Thread(new Runnable() {      public void run() {         // some code         for (int i = 0; i < mApps.size(); i++) {             mAppIcon = mAdapter.getIcons().get(mApps.get(i).getPackageName());             // need to resize drawable here             progressHandler.sendEmptyMessage(UPDATE_PBAR);         }         handler.sendEmptyMessage(DISMISS_PBAR);     }  });  myThread.start(); 
like image 236
mDude26 Avatar asked Aug 11 '11 06:08

mDude26


People also ask

How do I change drawable size on android?

Once you import svg file into Android Studio project, you are going to see <vector> resource then just change the size as you want by width , height attributes. viewportWidth and viewportHeight is to set size for drawing on virtual canvas.

What is ImageView in Android?

Displays image resources, for example Bitmap or Drawable resources. ImageView is also commonly used to apply tints to an image and handle image scaling.


2 Answers

The following worked for me:

private Drawable resize(Drawable image) {     Bitmap b = ((BitmapDrawable)image).getBitmap();     Bitmap bitmapResized = Bitmap.createScaledBitmap(b, 50, 50, false);     return new BitmapDrawable(getResources(), bitmapResized); } 
like image 173
3 revs, 3 users 75% Avatar answered Oct 13 '22 04:10

3 revs, 3 users 75%


Here's where I ended up, thanks in part to Saad's answer:

public Drawable scaleImage (Drawable image, float scaleFactor) {      if ((image == null) || !(image instanceof BitmapDrawable)) {         return image;     }      Bitmap b = ((BitmapDrawable)image).getBitmap();      int sizeX = Math.round(image.getIntrinsicWidth() * scaleFactor);     int sizeY = Math.round(image.getIntrinsicHeight() * scaleFactor);      Bitmap bitmapResized = Bitmap.createScaledBitmap(b, sizeX, sizeY, false);      image = new BitmapDrawable(getResources(), bitmapResized);      return image;  } 
like image 28
William T. Mallard Avatar answered Oct 13 '22 04:10

William T. Mallard