2011-08-11 60 views
27

我设置了一个进度对话框(pbarDialog)的drawable,但我的问题是我想每次调整drawable的大小,但无法弄清楚如何。调整大小在Android中可绘制

下面是一些代码:

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(); 
+0

哪个可绘制的你必须调整大小? –

+2

使用Bitmap.createScaledBitmap(src,dstWidth,dstHeight,filter) – Sameer

回答

74

以下为我工作:

private Drawable resize(Drawable image) { 
    Bitmap b = ((BitmapDrawable)image).getBitmap(); 
    Bitmap bitmapResized = Bitmap.createScaledBitmap(b, 50, 50, false); 
    return new BitmapDrawable(getResources(), bitmapResized); 
} 
+1

我在第一行代码中得到了这个错误:java.lang.ClassCastException:android.graphics.drawable.PictureDrawable无法转换为android.graphics.drawable.BitmapDrawable –

+4

使用Drawable作为参数并将其转换为BitmapDrawable!?如果你确定它始终是一个BitmapDrawable,那么也要将参数类型改为BitmapDrawable,否则在投射之前检查drawable类型是很好的。 –

18

这里就是我结束了,这要部分归功于萨阿德的回答是:

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; 

} 
+0

完美,这是我走的路。 – Tony

+0

不错的编码。正是我需要的。谢谢你,兄弟:) –

+0

要小心:前一个可绘制的“图像”包含一个未被回收的位图。调用“image = scaleImage(image,2)”会导致临时泄漏。scaleImage的调用者因此有责任回收原始图像和缩放后的图像......这在小应用程序中可能不是问题,但它是在内存密集型应用中至关重要。 –

5

对于调整大小,这是很好,短(上面的代码不适合我),找到here

ImageView iv = (ImageView) findViewById(R.id.imageView); 
    Bitmap bMap = BitmapFactory.decodeResource(getResources(), R.drawable.picture); 
    Bitmap bMapScaled = Bitmap.createScaledBitmap(bMap, newWidth, newHeight, true); 
    iv.setImageBitmap(bMapScaled); 
+0

这适用于当我想在将它设置为imageBitmap到ImageView之前调整Drawable的大小。谢谢 –

1

也许我的解决方案不是完全覆盖问题,但我需要类似“CustomDrawable”的东西。

换句话说,我想在圆形前面设置一个徽标。所以我创建了一个带有背景的FrameLayout(只是一个彩色圆圈),在这个圆形前面我展示了这个标志。

要调整的标志我通过缩放收缩标志 - 这里是一些代码:

iv = new ImageView(mContext); 

iv.setScaleX(0.75f); // <- resized by scaling 
iv.setScaleY(0.75f); 

// loading the drawable from a getter (replace this with any drawable) 
Drawable drawable = ML.loadIcon(mContext, Integer.parseInt(icon)); 

iv.setImageDrawable(drawable); 

// icon get's shown inside a ListView 
viewHolder.mIvIcon.addView(iv); 

这里是FrameLayout里表示ListView的行内的图标:

<FrameLayout 
    android:id="@+id/iv_card_icon" 
    android:layout_width="48dp" 
    android:layout_height="48dp" 
    android:src="@drawable/circle" 
    android:layout_marginStart="16dp" 
    /> 

见本解决方案作为选项/想法。

相关问题