软引用SoftReference的使用

package zy.just.com.gallerydemo;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Shader;

import java.lang.ref.SoftReference;
import java.util.Hashtable;

/**
 * Created by print on 2016/4/5.
 */
public class ImageUtils {

    //缓存集合
    private static Hashtable<Integer,SoftReference<Bitmap>> mCacheHashtable=new Hashtable<Integer,SoftReference<Bitmap>>();
    /**
     * 根据id返回处理过的图片
     * @param imageId
     * @return
     */
    public static Bitmap getInsertImage(Resources res,int imageId){

        //1.获取原图
        Bitmap bitmap = BitmapFactory.decodeResource(res, imageId);
        //2.生成倒影图片
        //创建矩阵
        Matrix m = new Matrix();
        m.setScale(1f,-1f);//两个参数分别表示水平翻转和垂直翻转。(1表示不翻转)
        //后半张图片
        Bitmap createBitmap = Bitmap.createBitmap(bitmap, 0, bitmap.getHeight() / 2, bitmap.getWidth(), bitmap.getHeight() / 2, m, false);

        //3.把两张图片合成一张
        Bitmap  zuheBitmap= Bitmap.createBitmap(bitmap.getWidth(), (int) (bitmap.getHeight() * 1.5 + 5), Bitmap.Config.ARGB_8888);
        //指定画板画在合成图上
        Canvas c = new Canvas(zuheBitmap);
        //把原图画在合成图的上边
        c.drawBitmap(bitmap,0,0,null);
        c.drawBitmap(createBitmap,0,bitmap.getHeight()+5,null);//把倒影图片画在合成图片上
        //4.添加遮罩效果
        Paint paint = new Paint();
        //设置画笔颜色LinearGradient:线性渲染器
        //LinearGradientShader.TileMode.CLAMP:
        //渲染时,只要设置高度y的变化
        LinearGradient shader = new LinearGradient(0,bitmap.getHeight()+5,0,zuheBitmap.getHeight(),0x70ffffff,0x00ffffff, Shader.TileMode.CLAMP);
        paint.setShader(shader);
        //设置模式是遮罩,取交集
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
        c.drawRect(0,bitmap.getHeight(),bitmap.getWidth(),zuheBitmap.getHeight(),paint);
        return zuheBitmap;
    }


    /**
     * 根据id返回一个已经处理过的图片
     * @param res
     * @param imageId
     * @return
     */
    public static Bitmap getImageBitmap(Resources res,int imageId){

        //先去集合中取当前imageID,判断是否已经拿过图片,如果集合中有,说明已经拿过。直接使用集合中的图片返回。
        SoftReference<Bitmap> bitmapSoftReference = mCacheHashtable.get(imageId);
        if(bitmapSoftReference!=null){
            Bitmap bitmap = bitmapSoftReference.get();//这边需要注意的是这个Bitmap对象可能被回收了
            //判断bitmap是否被回收
            if(bitmap!=null){
                //从集合中取(内存中)
                return bitmap;
            }
        }
        //如果集合中没有,就调用getInsertImage得到一张图片,并需要在集合中保留这张图片,最后返回当前图片。
        Bitmap insertImage = getInsertImage(res, imageId);//重新加载图片
        //在集合中存一份,便于以后从集合中去取。
        mCacheHashtable.put(imageId,new SoftReference<Bitmap>(insertImage));
        return insertImage;
    }
}

猜你喜欢

转载自blog.csdn.net/zhangying1994/article/details/51079926