Resize Drawable in Android

前端 未结 6 1997
梦如初夏
梦如初夏 2020-12-02 17:12

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 so

6条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-02 17:34

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

提交回复
热议问题