关于在帧动画 的动态加载

加载本地缓存图片

 

List<String> imgList = new ArrayList<String>();

        imgList.add("本地图片地址");
      imgList.add("本地图片地址");
        for (int i = 0; i < imgList.size(); i++) {
//路径地址变转变成 //Drawable
            Drawable drawable = getDrawable(imgList.get(i));
            if (drawable != null) {
//添加到动画对象中
                anim.addFrame(drawable, 200);
            }
        }
        anim.setOneShot(false);
        imageView.setImageDrawable(anim);
        anim.start();

  工具类

 

    public Drawable getDrawable(String filePath) {
        Bitmap bitmap = readBitMap(filePath);
        if (bitmap != null) {
            BitmapDrawable bitmapDrawable = new BitmapDrawable(getResources(), bitmap);
            return bitmapDrawable;
        }
        return null;
    }


    public static Bitmap readBitMap(String filePath) {
        BitmapFactory.Options opt = new BitmapFactory.Options();
        opt.inPreferredConfig = Bitmap.Config.RGB_565;
        opt.inPurgeable = true;
        opt.inInputShareable = true;
        return decodeBmp(filePath, opt);
    }

    public static Bitmap decodeBmp(String imgPath, BitmapFactory.Options opts) {
        Bitmap dstBmp = null;
        FileInputStream fis;
        try {
            fis = new FileInputStream(imgPath);
            dstBmp = BitmapFactory.decodeStream(fis, null, opts);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (OutOfMemoryError e) {
            opts.inSampleSize += 1;
            opts.inPreferredConfig = Bitmap.Config.RGB_565;
            opts.inPurgeable = true;
            opts.inInputShareable = true;
            opts.inJustDecodeBounds = false;
            dstBmp = decodeBmp(imgPath, opts);
        }
        return dstBmp;
    }

  

posted @ 2016-09-13 17:53  TomCzr  阅读(522)  评论(0编辑  收藏  举报