BitmapFactory之Options

package com.loaderman.customviewdemo;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;

import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import java.io.File;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //测试inJustDecodeBounds属性,获取图片宽高
        findViewById(R.id.injust_decode_bounds_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                testInJustDecodeBounds();
            }
        });

        //采样率
        findViewById(R.id.in_sample_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                testInSample();
            }
        });

        // inScaled
        findViewById(R.id.in_scaled_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                testInScaled();
            }
        });


        //inDensity、inTargetDensity
        findViewById(R.id.in_density_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                testInDensity();
            }
        });
        //inPreferredConfig
        findViewById(R.id.in_preferred_config_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                testInPreferredConfig();
            }
        });

    }


    //测试inJustDecodeBounds属性,获取图片宽高
    private void testInJustDecodeBounds() {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;  
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher, options);
        Toast.makeText(MainActivity.this, "realwidth:" + options.outWidth + "   realheight:" + options.outHeight + " mimeType:" + options.outMimeType
                , Toast.LENGTH_SHORT).show();
    }

    //测试采样率函数
    private void testInSample() {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeResource(getResources(), R.drawable.scenery, options);

        ImageView iv = (ImageView) findViewById(R.id.img);
        int sampleSize = calSampleSize(options, iv.getWidth(), iv.getHeight());
        Toast.makeText(MainActivity.this, "sampleSize" + sampleSize, Toast.LENGTH_SHORT).show();


        BitmapFactory.Options options2 = new BitmapFactory.Options();
        options2.inSampleSize = sampleSize;
        try {
            Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.scenery, options2);
            iv.setImageBitmap(bmp);
        } catch (OutOfMemoryError err) {
            //TODO OOM
        }
    }


    //dstWidth和dstHeight分别为目标ImageView的宽高
    public static int calSampleSize(BitmapFactory.Options options, int dstWidth, int dstHeight) {
        int rawWidth = options.outWidth;
        int rawHeight = options.outHeight;
        int inSampleSize = 1;
        if (rawWidth > dstWidth || rawHeight > dstHeight) {
            float ratioHeight = (float) rawHeight / dstHeight;
            float ratioWidth = (float) rawWidth / dstHeight;
            inSampleSize = (int) Math.min(ratioWidth, ratioHeight);
        }
        return inSampleSize;
    }

    //inPreferredConfig
    public void testInPreferredConfig() {
        Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.scenery);
        String LogStr = "ARGB888_width:" + bmp.getWidth() + "  height:" + bmp.getHeight() + " 内存:" + bmp.getByteCount();
        Toast.makeText(MainActivity.this, LogStr, Toast.LENGTH_SHORT).show();


        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPreferredConfig = Bitmap.Config.RGB_565;//用来设置像素的存储格式
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.scenery, options);
        String LogStr2 = "ARGB565_width:" + bitmap.getWidth() + "  height:" + bitmap.getHeight() + " 内存:" + bitmap.getByteCount();
        Toast.makeText(MainActivity.this, LogStr2, Toast.LENGTH_SHORT).show();
    }


    public void testInScaled() {
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.scenery);
        Toast.makeText(MainActivity.this, "drawableBmp_width:" + bitmap.getWidth() + "  height:" + bitmap.getHeight() + " 内存:" + bitmap.getByteCount(),
                Toast.LENGTH_SHORT).show();


        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inScaled = false;//是否缩放
        Bitmap noScaleBmp = BitmapFactory.decodeResource(getResources(), R.drawable.scenery, options);
        Toast.makeText(MainActivity.this, "drawableBmp_width:" + noScaleBmp.getWidth() + "  height:" + noScaleBmp.getHeight() + " 内存:" + noScaleBmp.getByteCount(), Toast.LENGTH_SHORT).show();
    }


    public void testInDensity() {
        //从Drawable里读取
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inDensity = 1;//用于设置屏幕分辨率
        options.inTargetDensity = 2;//表示真正显示屏幕分辨率
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.scenery, options);

        //直接从文件中读取
        File file = Environment.getExternalStorageDirectory();
        String path = file.getAbsolutePath() + "/scenery.png";
        Bitmap bmp = BitmapFactory.decodeFile(path, options);
        if (bmp == null) {
            Toast.makeText(MainActivity.this, "请确保SD卡根目录存在scenery.png", Toast.LENGTH_SHORT).show();
        } else {
            String toastStr = "fileBmp_width:" + bmp.getWidth() + "  height:" + bmp.getHeight() + " 内存:" + bmp.getByteCount();

            Toast.makeText(MainActivity.this, toastStr, Toast.LENGTH_SHORT).show();
        }

    }
}

 

posted on 2019-01-07 09:25  LoaderMan  阅读(351)  评论(0编辑  收藏  举报

导航