drawable 转 bitmap 并设置 bm 尺寸

2017-12-18  本文已影响26人  that_is_this
private Bitmap drawableToBitmap(Drawable drawable) {
    int w = drawable.getIntrinsicWidth();
    int h = drawable.getIntrinsicHeight();
    Log.i("Wooo", "-drawableToBitmap--" + h + "--w--" + w);
    Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
    Bitmap bitmap = Bitmap.createBitmap(w, h, config);
    Canvas canvas = new Canvas(bitmap);
    drawable.setBounds(0, 0, w, h);
    drawable.draw(canvas);

    int width = bitmap.getWidth();          // 更改 APP 展示图标尺寸
    int height = bitmap.getHeight();
    int newWidth = 180;
    int newHeight = 180;
    float scaleWidth = ((float) newWidth) / width;
    float scaleHeight = ((float) newHeight) / height;
    Matrix matrix = new Matrix();
    matrix.postScale(scaleWidth, scaleHeight);
    Bitmap newbm = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
    return newbm;
}
上一篇下一篇

猜你喜欢

热点阅读