美文网首页
drawable 转 bitmap 并设置 bm 尺寸

drawable 转 bitmap 并设置 bm 尺寸

作者: that_is_this | 来源:发表于2017-12-18 14:43 被阅读26次
    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;
    }

    相关文章

      网友评论

          本文标题:drawable 转 bitmap 并设置 bm 尺寸

          本文链接:https://www.haomeiwen.com/subject/vukmwxtx.html