缩小图片的像素减少内存压力

package com.example.administrator.myapplication;


import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;


public class MainActivity extends AppCompatActivity {
    ImageView mImageView;
    Bitmap bitMap;
    static String str;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout);
        mImageView = (ImageView) findViewById(R.id.imageView2);
        try {
            bitMap = decodeSampledBitmapFromResource(getResources(), R.drawable.android1, 200, 310);
        } catch (Exception e) {


        }


        mImageView.setImageBitmap(bitMap);
    }


    public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId, int MyWidth, int MyHeight) throws Exception {


        final BitmapFactory.Options options = new BitmapFactory.Options();
        /**被赋值为true返回的Bitmap为null,虽然Bitmap是null了,但是BitmapFactory.Options的outWidth、
         *outHeight和outMimeType属性都会被赋值。这个技巧让我们可以在加载图片之前就获取到图片的长宽值和MIME类型,从而根据情况对图片进行压缩
         */
        options.inJustDecodeBounds = true;
      
        BitmapFactory.decodeResource(res, resId, options);
        //得到压缩倍数。
        options.inSampleSize = calculateInSampleSize(options, MyWidth, MyHeight);
        //设置为false,就能得到Bitmap.
        options.inJustDecodeBounds = false;


        return BitmapFactory.decodeResource(res, resId, options);


    }


    public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) throws Exception {
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;
        if (height > reqHeight || width > reqWidth) {
            final int heightRatio = Math.round((float) height / (float) reqHeight);
            final int widthRatio = Math.round((float) width / (float) reqWidth);
            inSampleSize = heightRatio < widthRatio ? widthRatio : heightRatio;
        }
        return inSampleSize;
    }

}

下面是处理文件路径型的方法,上面是处理R.xxx.xxx的

public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) throws Exception {
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;
    if (height > reqHeight || width > reqWidth) {
        final int heightRatio = Math.round((float) height / (float) reqHeight);
        final int widthRatio = Math.round((float) width / (float) reqWidth);
        inSampleSize = heightRatio < widthRatio ? widthRatio : heightRatio;
    }
    return inSampleSize;
}
public static Bitmap decodeSampledBitmapFromResource(String pic_path, int MyWidth, int MyHeight) throws Exception {


    final BitmapFactory.Options options = new BitmapFactory.Options();
    /**被赋值为true返回的Bitmap为null,虽然Bitmap是null了,但是BitmapFactory.Options的outWidth、
     *outHeight和outMimeType属性都会被赋值。这个技巧让我们可以在加载图片之前就获取到图片的长宽值和MIME类型,从而根据情况对图片进行压缩
     */
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(pic_path, options);
    //得到压缩倍数。
    options.inSampleSize = calculateInSampleSize(options, MyWidth, MyHeight);
    //设置为false,就能得到Bitmap.
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeFile(pic_path, options);
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值