您的位置:首页 > 其它

帧动画加载大量图片OOM的解决办法

2016-06-24 14:08 309 查看
这是在别人代码基础上更改的。

代码如下:

public class SceneAnimation {

/**
* target imageView
*/
private ImageView mImageView;

/**
* frame resources
*/
private int[] mFrameRes = null;

/**
* different duration in frame
*/
private int[] mDurations = null;

/**
* specific duration
*/
private long mDuration;

private int mCurrentFrame;

private boolean mStop;

private boolean mRepeat;

private boolean mSpecificDuration;

private Runnable mAction = null;

private OnFramePlayListener mListener;

public interface OnFramePlayListener{

void onFrame(int frame);

void onEnd();
}

public SceneAnimation(ImageView pImageView, int[] pFrameRes, int pDuration) {
initialize(pImageView, pFrameRes);
mDuration = pDuration;
mSpecificDuration = true;
}

public SceneAnimation(ImageView pImageView, int[] pFrameRes, int[] pDurations) {
initialize(pImageView, pFrameRes);
mDurations = pDurations;
mSpecificDuration = false;
}

private void initialize(ImageView pImageView, int[] pFrameRes) {
mImageView = pImageView;
mFrameRes = pFrameRes;
mCurrentFrame = 0;
mStop = false;
mRepeat = false;
}

/**
* @return the total time of animation
*/
public long getTotalTime(){
if(mFrameRes != null) {
if (mSpecificDuration) {
return mDuration * (mFrameRes.length - 1);
} else if(mDurations != null){
long totalTime = 0;
for (int i = 0; i < mDurations.length; i++) {
totalTime += mDurations[i];
}
return totalTime;
}else{
return 0;
}
}
return 0;
}

public void setRepeat(boolean mRepeat) {
this.mRepeat = mRepeat;
}

public void setOnFramePlayListener(OnFramePlayListener mListener) {
this.mListener = mListener;
}

@SuppressWarnings("deprecation")
private void setBackground(View view, Drawable drawable)
{
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN)
{
view.setBackgroundDrawable(drawable);
} else {
view.setBackground(drawable);
}
}

private void doPlay(){
if(null == mImageView)
return;

mImageView.postDelayed(mAction = new Runnable()
{
public void run() {

if(mCurrentFrame < mFrameRes.length && !mStop) {

setBackground(mImageView,
ContextCompat.getDrawable(mImageView.getContext(), mFrameRes[mCurrentFrame]));

if(mListener != null){

mListener.onFrame(mCurrentFrame);
if(mCurrentFrame == mFrameRes.length - 1){
mListener.onEnd();
}

}

mCurrentFrame++;
doPlay();

}else if(mRepeat && !mStop){

startPlay();
}
}

}, getDelayTime());
}

/**
* 由于都是大图片所以动画播放过程中会频繁gc
* 所以动画时间间隔需要缩短,暂时没想到更好的办法,只是
* 把时间间隔缩短为原来的65%比较接近我们期望的动画时长
* @return
*/
private long getDelayTime() {
if(mSpecificDuration)
return (long)(mDuration * 0.65d);
else{
if(mCurrentFrame - 1 < mDurations.length){
return (long)(mDurations[mCurrentFrame - 1] * 0.65d);
}else{
return 0;
}
}
}

public void startPlay(){
if(mFrameRes != null) {
mCurrentFrame = 0;
setBackground(mImageView,
ContextCompat.getDrawable(mImageView.getContext(), mFrameRes[mCurrentFrame]));
mCurrentFrame++;
doPlay();
}
}

/**
* 停止动画,释放资源
*/
public void stopImmediately() {
if(mImageView != null){
mStop = true;
if(mAction != null) {
mImageView.removeCallbacks(mAction);
mAction = null;
}
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: