关闭

android很多图片做成帧动画造成内存溢出的解决方法。

标签: android内存溢出动画
1940人阅读 评论(1) 收藏 举报
package com.familydoctor.widget;

import android.os.Handler;
import android.util.Log;
import android.widget.ImageView;

import com.familydoctor.event.BaseEvent;
import com.familydoctor.event.EventCode;
import com.familydoctor.manager.EventManager;

/**
 * Created by kk on 2015/11/19.
 * ImageAnimation animation = new ImageAnimation(Iv_guide(控件 ImageView, images (图片资源 int[], 115 (每张图片的时间间隔));
 */
public class ImageAnimation {

    private Handler handler;//线程处理
    private MovieAction action;//播放器


    //固定循环时间
    public ImageAnimation(ImageView view,int[] frameRes,int duration){
        int len = frameRes.length;
        int [] frameDuration = new int[len];
        for(int i=0;i<len;i++){
            frameDuration[i]=duration;
        }
        this.Init(view,frameRes,frameDuration);
    }

    //非固定循环时间
    public ImageAnimation(ImageView view, int [] frameRes,int[] frameDuration){
        this.Init(view,frameRes,frameDuration);

    }

    private void Init(ImageView view, int [] frameRes,int[] frameDuration){

        if(null==view) {
            Log.e("ImageAnimation", "创建动画失败。");
        }else if(null == frameRes || null == frameDuration ||0 == frameRes.length ||0 == frameDuration.length) {
            Log.e("ImageAnimation", "帧数或资源为空.");
        }else if(frameRes.length != frameDuration.length){
            Log.e("ImageAnimation","帧数与间隔时间不匹配");
        }else {
            handler = new Handler();
            action = new MovieAction(handler, view, frameRes, frameDuration);
        }
    }


}


//动画播放类
class MovieAction implements Runnable{

    private ImageView mView;//画布
    private int [] mFrameRes;//资源
    private int[] mFrameDuration;//间隔
    private int currentFrame;//当前帧
    private int totalFrame;//总帧数

    private Handler mHandler;//线程

    public MovieAction(Handler handler,ImageView view, int [] frameRes,int[] frameDuration){
        this.mView = view;
        this.mFrameRes = frameRes;
        this.mFrameDuration = frameDuration;
        this.mHandler = handler;

        totalFrame = this.mFrameRes.length;
        currentFrame = 0;
        mHandler.postDelayed(this, mFrameDuration[currentFrame]);
    }

    public void destory(){
        this.mHandler.removeCallbacks(this);
    }


    /**
     * Starts executing the active part of the class' code. This method is
     * called when a thread is started that has been created with a class which
     * implements {@code Runnable}.
     */
    @Override
    public void run() {
        mView.setBackgroundResource(mFrameRes[currentFrame]);

        currentFrame++;
        if(++currentFrame<totalFrame) {
            mHandler.postDelayed(this, mFrameDuration[currentFrame]);
        }else{
            System.out.println("destory、、、、、");
            destory();
            EventManager.getInstance().DispatchEvent(new BaseEvent(EventCode.ShowBtn));
        }
    }

}
0
1

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:3106次
    • 积分:127
    • 等级:
    • 排名:千里之外
    • 原创:9篇
    • 转载:1篇
    • 译文:1篇
    • 评论:1条
    文章分类
    最新评论