zoukankan      html  css  js  c++  java
  • 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, "real" + 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_" + 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_" + 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_" + 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_" + 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_" + bmp.getWidth() + "  height:" + bmp.getHeight() + " 内存:" + bmp.getByteCount();
    
                Toast.makeText(MainActivity.this, toastStr, Toast.LENGTH_SHORT).show();
            }
    
        }
    }
  • 相关阅读:
    HDU 4472 Count DP题
    HDU 1878 欧拉回路 图论
    CSUST 1503 ZZ买衣服
    HDU 2085 核反应堆
    HDU 1029 Ignatius and the Princess IV
    UVa 11462 Age Sort
    UVa 11384
    UVa 11210
    LA 3401
    解决学一会儿累了的问题
  • 原文地址:https://www.cnblogs.com/loaderman/p/10231453.html
Copyright © 2011-2022 走看看