android图片帧动画造成内存溢出的处理方法

package com.anwen.widget;


import android.os.Handler;

import android.util.Log;

import android.widget.ImageView;


import com.anwen.event.BaseEvent;

import com.anwen.event.EventCode;

import com.anwen.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));

        }

    }


}



转载于:https://my.oschina.net/u/2244722/blog/533051

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值