美文网首页
Bitmap -- mDensity

Bitmap -- mDensity

作者: TomyZhang | 来源:发表于2020-12-19 23:52 被阅读0次

    一、Bitmap 的 mDensity 属性

    Bitmap 内部的 mDensity 属性默认是跟设备的屏幕像素密度相同的,改变 mDensity 的属性值,并不会影响 Bitmap 的宽度跟高度的像素值,但是会影响 Bitmap 在屏幕上显示的物理尺寸。

    1.原图效果

    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image_test);
    Log.d(TAG, "zwm, bitmap density: " + bitmap.getDensity() + ", width: " + bitmap.getWidth() + ", height: " + bitmap.getHeight());
    imageView.setImageBitmap(bitmap);
    imageView.post(new Runnable() {
        @Override
        public void run() {
            Log.d(TAG, "zwm, imageView width: " + imageView.getWidth() + ", height: " + imageView.getHeight());
        }
    });
    
    日志打印:
    2020-12-19 13:33:38.798 22389-22389/com.tomorrow.target30 D/MainActivity: zwm, bitmap density: 420, width: 438, height: 438
    2020-12-19 13:33:39.072 22389-22389/com.tomorrow.target30 D/MainActivity: zwm, imageView width: 438, height: 438
    
    无修改 mDensity 属性值.PNG

    2.mDensity 属性值乘以 2 效果

    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image_test);
    Log.d(TAG, "zwm, bitmap density: " + bitmap.getDensity() + ", width: " + bitmap.getWidth() + ", height: " + bitmap.getHeight());
    bitmap.setDensity(bitmap.getDensity() * 2);
    Log.d(TAG, "zwm, bitmap change density: " + bitmap.getDensity() + ", width: " + bitmap.getWidth() + ", height: " + bitmap.getHeight());
    imageView.setImageBitmap(bitmap);
    imageView.post(new Runnable() {
        @Override
        public void run() {
            Log.d(TAG, "zwm, imageView width: " + imageView.getWidth() + ", height: " + imageView.getHeight());
        }
    });
    
    日志打印:
    2020-12-19 13:40:09.698 25609-25609/com.tomorrow.target30 D/MainActivity: zwm, bitmap density: 420, width: 438, height: 438
    2020-12-19 13:40:09.699 25609-25609/com.tomorrow.target30 D/MainActivity: zwm, bitmap change density: 840, width: 438, height: 438
    2020-12-19 13:40:09.972 25609-25609/com.tomorrow.target30 D/MainActivity: zwm, imageView width: 219, height: 219
    
    mDensity 属性值乘以 2.PNG

    mDensity 属性值乘以 2,宽度跟高度的像素值不变,在屏幕上显示的物理尺寸的宽度跟高度都需要除以 2。

    3.mDensity 属性值除以 2 效果

    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image_test);
    Log.d(TAG, "zwm, bitmap density: " + bitmap.getDensity() + ", width: " + bitmap.getWidth() + ", height: " + bitmap.getHeight());
    bitmap.setDensity(bitmap.getDensity() / 2);
    Log.d(TAG, "zwm, bitmap change density: " + bitmap.getDensity() + ", width: " + bitmap.getWidth() + ", height: " + bitmap.getHeight());
    imageView.setImageBitmap(bitmap);
    imageView.post(new Runnable() {
        @Override
        public void run() {
            Log.d(TAG, "zwm, imageView width: " + imageView.getWidth() + ", height: " + imageView.getHeight());
        }
    });
    
    日志打印:
    2020-12-19 13:47:08.245 30015-30015/com.tomorrow.target30 D/MainActivity: zwm, bitmap density: 420, width: 438, height: 438
    2020-12-19 13:47:08.245 30015-30015/com.tomorrow.target30 D/MainActivity: zwm, bitmap change density: 210, width: 438, height: 438
    2020-12-19 13:47:08.534 30015-30015/com.tomorrow.target30 D/MainActivity: zwm, imageView width: 876, height: 876
    
    mDensity 属性值除以 2.PNG

    mDensity 属性值除以 2,宽度跟高度的像素值不变,在屏幕上显示的物理尺寸的宽度跟高度都需要乘以 2。

    二、Bitmap 的 mDensity 属性对 Canvas#drawBitmap 的影响

    1.结果 Bitmap 与 源 Bitmap 的 mDensity 属性值相同

    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image_test);
    imageView.setImageBitmap(canvasDrawBitmap(bitmap));
    imageView.post(new Runnable() {
        @Override
        public void run() {
            Log.d(TAG, "zwm, imageView width: " + imageView.getWidth() + ", height: " + imageView.getHeight());
        }
    });
    
    private Bitmap canvasDrawBitmap(Bitmap source) {
        Log.d(TAG, "zwm, source density: " + source.getDensity() + ", width: " + source.getWidth() + ", height: " + source.getHeight());
        Bitmap bitmap = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
        Log.d(TAG, "zwm, bitmap density: " + bitmap.getDensity() + ", width: " + bitmap.getWidth() + ", height: " + bitmap.getHeight());
        Canvas canvas = new Canvas(bitmap);
        Log.d(TAG, "zwm, canvas density: " + canvas.getDensity() + ", width: " + bitmap.getWidth() + ", height: " + bitmap.getHeight());
        Paint paint = new Paint();
        paint.setFlags(Paint.FILTER_BITMAP_FLAG);
        canvas.drawBitmap(source, 0, 0, paint);
        Log.d(TAG, "zwm, result density: " + bitmap.getDensity() + ", width: " + bitmap.getWidth() + ", height: " + bitmap.getHeight());
        return bitmap;
    }
    
    日志打印:
    2020-12-19 20:03:48.032 24093-24093/com.tomorrow.target30 D/MainActivity: zwm, source density: 420, width: 438, height: 438
    2020-12-19 20:03:48.033 24093-24093/com.tomorrow.target30 D/MainActivity: zwm, bitmap density: 420, width: 438, height: 438
    2020-12-19 20:03:48.033 24093-24093/com.tomorrow.target30 D/MainActivity: zwm, canvas density: 420, width: 438, height: 438
    2020-12-19 20:03:48.034 24093-24093/com.tomorrow.target30 D/MainActivity: zwm, result density: 420, width: 438, height: 438
    2020-12-19 20:03:48.132 24093-24093/com.tomorrow.target30 D/MainActivity: zwm, imageView width: 438, height: 438
    
    结果 Bitmap 与 源 Bitmap 的 mDensity 属性值相同.PNG

    2.结果 Bitmap 的 mDensity 属性值为源 Bitmap 除以 2

    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image_test);
    imageView.setImageBitmap(canvasDrawBitmap(bitmap));
    imageView.post(new Runnable() {
        @Override
        public void run() {
            Log.d(TAG, "zwm, imageView width: " + imageView.getWidth() + ", height: " + imageView.getHeight());
        }
    });
    
    private Bitmap canvasDrawBitmap(Bitmap source) {
        Log.d(TAG, "zwm, source density: " + source.getDensity() + ", width: " + source.getWidth() + ", height: " + source.getHeight());
        Bitmap bitmap = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
        Log.d(TAG, "zwm, bitmap density: " + bitmap.getDensity() + ", width: " + bitmap.getWidth() + ", height: " + bitmap.getHeight());
        bitmap.setDensity(source.getDensity() / 2);
        Canvas canvas = new Canvas(bitmap);
        Log.d(TAG, "zwm, canvas density: " + canvas.getDensity() + ", width: " + bitmap.getWidth() + ", height: " + bitmap.getHeight());
        Paint paint = new Paint();
        paint.setFlags(Paint.FILTER_BITMAP_FLAG);
        canvas.drawBitmap(source, 0, 0, paint);
        Log.d(TAG, "zwm, result density: " + bitmap.getDensity() + ", width: " + bitmap.getWidth() + ", height: " + bitmap.getHeight());
        return bitmap;
    }
    
    日志打印:
    2020-12-19 20:52:56.846 13089-13089/com.tomorrow.target30 D/MainActivity: zwm, source density: 420, width: 438, height: 438
    2020-12-19 20:52:56.847 13089-13089/com.tomorrow.target30 D/MainActivity: zwm, bitmap density: 420, width: 438, height: 438
    2020-12-19 20:52:56.847 13089-13089/com.tomorrow.target30 D/MainActivity: zwm, canvas density: 210, width: 438, height: 438
    2020-12-19 20:52:56.850 13089-13089/com.tomorrow.target30 D/MainActivity: zwm, result density: 210, width: 438, height: 438
    2020-12-19 20:52:57.048 13089-13089/com.tomorrow.target30 D/MainActivity: zwm, imageView width: 876, height: 876
    
    结果 Bitmap 的 mDensity 属性值为源 Bitmap 除以 2.PNG

    结果 Bitmap 的 mDensity 属性值为源 Bitmap 除以 2,结果 Bitmap 的宽度跟高度的像素值不变,结果 Bitmap 在屏幕上显示的物理尺寸的宽度跟高度都需要乘以 2,源 Bitmap 的所有像素都绘制到了结果 Bitmap 的左上方 1/4 位置。

    3.结果 Bitmap 的 mDensity 属性值为源 Bitmap 乘以 2

    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image_test);
    imageView.setImageBitmap(canvasDrawBitmap(bitmap));
    imageView.post(new Runnable() {
        @Override
        public void run() {
            Log.d(TAG, "zwm, imageView width: " + imageView.getWidth() + ", height: " + imageView.getHeight());
        }
    });
    
    private Bitmap canvasDrawBitmap(Bitmap source) {
        Log.d(TAG, "zwm, source density: " + source.getDensity() + ", width: " + source.getWidth() + ", height: " + source.getHeight());
        Bitmap bitmap = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
        Log.d(TAG, "zwm, bitmap density: " + bitmap.getDensity() + ", width: " + bitmap.getWidth() + ", height: " + bitmap.getHeight());
        bitmap.setDensity(source.getDensity() * 2);
        Canvas canvas = new Canvas(bitmap);
        Log.d(TAG, "zwm, canvas density: " + canvas.getDensity() + ", width: " + bitmap.getWidth() + ", height: " + bitmap.getHeight());
        Paint paint = new Paint();
        paint.setFlags(Paint.FILTER_BITMAP_FLAG);
        canvas.drawBitmap(source, 0, 0, paint);
        Log.d(TAG, "zwm, result density: " + bitmap.getDensity() + ", width: " + bitmap.getWidth() + ", height: " + bitmap.getHeight());
        return bitmap;
    }
    
    日志打印:
    2020-12-19 21:04:12.707 17542-17542/com.tomorrow.target30 D/MainActivity: zwm, source density: 420, width: 438, height: 438
    2020-12-19 21:04:12.708 17542-17542/com.tomorrow.target30 D/MainActivity: zwm, bitmap density: 420, width: 438, height: 438
    2020-12-19 21:04:12.709 17542-17542/com.tomorrow.target30 D/MainActivity: zwm, canvas density: 840, width: 438, height: 438
    2020-12-19 21:04:12.713 17542-17542/com.tomorrow.target30 D/MainActivity: zwm, result density: 840, width: 438, height: 438
    2020-12-19 21:04:12.938 17542-17542/com.tomorrow.target30 D/MainActivity: zwm, imageView width: 219, height: 219
    
    结果 Bitmap 的 mDensity 属性值为源 Bitmap 乘以 2.PNG

    结果 Bitmap 的 mDensity 属性值为源 Bitmap 乘以 2,结果 Bitmap 的宽度跟高度的像素值不变,结果 Bitmap 在屏幕上显示的物理尺寸的宽度跟高度都需要除以 2,源 Bitmap 的仅左上方 1/4 位置像素绘制到了结果 Bitmap 的所有位置。

    相关文章

      网友评论

          本文标题:Bitmap -- mDensity

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