在Android中调整Drawable的大小

mDu*_*e26 34 android resize image bitmap drawable

我正在为进度对话框设置一个drawable(pbarDialog)但我的问题是我想每次调整drawable的大小但是无法弄清楚如何.

这是一些代码:

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();
Run Code Online (Sandbox Code Playgroud)

Saa*_*ooq 82

以下对我有用:

private Drawable resize(Drawable image) {
    Bitmap b = ((BitmapDrawable)image).getBitmap();
    Bitmap bitmapResized = Bitmap.createScaledBitmap(b, 50, 50, false);
    return new BitmapDrawable(getResources(), bitmapResized);
}
Run Code Online (Sandbox Code Playgroud)

  • 使用Drawable作为参数并将其强制转换为BitmapDrawable!如果你确定它总是一个BitmapDrawable,所以也可以将参数类型更改为BitmapDrawable,如果不是,那么在转换之前检查drawable类型可能会很好. (5认同)
  • 我在第一行代码中收到此错误: java.lang.ClassCastException: android.graphics.drawable.PictureDrawable 无法转换为 android.graphics.drawable.BitmapDrawable (2认同)

Wil*_*ard 20

这是我结束的地方,部分归功于萨阿德的回答:

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;

}
Run Code Online (Sandbox Code Playgroud)


cra*_*ned 9

对于调整大小,这很好很短(上面的代码对我不起作用),在这里找到:

  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);
Run Code Online (Sandbox Code Playgroud)


Cha*_*ham 6

这是上述答案的组合作为 Kotlin 扩展

fun Context.scaledDrawableResources(@DrawableRes id: Int, @DimenRes width: Int, @DimenRes height: Int): Drawable {
    val w = resources.getDimension(width).toInt()
    val h = resources.getDimension(height).toInt()
    return scaledDrawable(id, w, h)
}

fun Context.scaledDrawable(@DrawableRes id: Int, width: Int, height: Int): Drawable {
    val bmp = BitmapFactory.decodeResource(resources, id)
    val bmpScaled = Bitmap.createScaledBitmap(bmp, width, height, false)
    return BitmapDrawable(resources, bmpScaled)
}
Run Code Online (Sandbox Code Playgroud)

用法:

val scaled = context.scaledDrawableResources(R.drawable.ic_whatever, R.dimen.width, R.dimen.height)
imageView.setImageDrawable(scaled)
Run Code Online (Sandbox Code Playgroud)

或者

val scaled = context.scaledDrawable(R.drawable.ic_whatever, 100, 50)
imageView.setImageDrawable(scaled)
Run Code Online (Sandbox Code Playgroud)