2011-08-11 56 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" 
    /> 

見本解決方案作爲選項/想法。