Android 中的缩略图的加载

//Android 中的缩略图加载

/*
    1、使用inJustDecodeBounds,读Bitmap的长和宽
    2、根据bitmap的长宽和目标缩略图的长和宽。计算出inSampleSize的大小
    3、使用inSampleSize,载入一个大一点的缩略图A
    4、使用createScaseBitmap,将缩略图A,生成我们需要的缩略图B
    5、回收缩略图A
*/

/*
    Notice
        createScaseBitmap如果原图和目标缩略图大小一致,那么不会生成一个新的Bitmap直接返回bitmap,因此,回收的时候,要判断缩略图A是否就是缩略图B,如果说是的话,不要回收
*/


//代码:
public class BitmapUtils{
            //计算inSampleSize的大小
            private static int calculateInSampleSize(BitmapFactory.Options options,int reqWidth,int reqHeight){
                final int height = options.outHeight;
                final int width = options.outWidth;
                int inSampleSize = 1;
                if(height>reqHeight||width>reqWidth){
                    final int halfHeight = height/2;
                    final int halfWidth = width/2;
                    while((halfHeight/inSampleSize)>reqHeight&&(halfWidth/inSampleSize)>reqWidth){
                        inSampleSize*=2;
                    }
                }
                return inSampleSize;
            }

            //创建新的Bitmap 图片对象
            private static Bitmap createScaleBitmap(Bitmap src,int dstWidth.int dstHeight){
                Bitmap dst=Bitmap.createScaleBitmap(src,dstWidth,dstHeight,false);
                if(src!=dst){ //如果没有缩放,那么不回收
                    src.recycle(); //释放Bitmap的native像素数组
                }
                return dst;
            }

            //从Resources中加载图片
            public static Bitmap decodeSampleBitmapFromResource(Resource res,int resId,int reqWidth,int reqHeight){
                final BitmapFactory.Options options= new BitmapFactory.Options();
                options.inJustDecodeBounds = true;
                BitmapFactory.decodeResource(res,resId,options);  //读取图片长宽
                options.inSampleSize = calculateInSampleSize(options,reqWidth,reqHeight);//计算inSampleSize
                options.inJustDecodeBounds = false;
                Bitmap src = BitmapFactory.decodeResource(res,resId,options); //载入一个稍大的缩略图
                return createScaleBitmap(src,reqWidht,reqHeight);  //进一步得到目标大小的缩略图
            }

            //从sd卡上加载图片
            public static Bitmap decodeSampleBitmapFromFd(String pathName,int reqWidth,int reqHeight){
                final BitmapFactory.Options options= new BitmapFactory.Options();
                options.inJustDecodeBounds =  true;
                BitmapFactory.decodeFile(pathName,options);  //获取图片的长宽
                options.inSampleSize = calculateSampleSize(options,reqWidth,reqHeight);
                options.inJustDecodeBounds= false;
                Bitmap src =  BitmapFactory.decodeFile(pathName,options);
                return createScaleBitmap(src,reqWidth,reqHeight);
            }
        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值