import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.io.IOUtils;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import android.content.Context;
import android.content.res.XmlResourceParser;
import android.graphics.BitmapFactory;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.widget.ImageView;
/****
* 此工具类源于stack over flow
* 原文链接:http://stackoverflow.com/questions/8692328/causing-outofmemoryerror-in-frame-by-frame-animation-in-android
* 通过底层C来绘制图片,有效防止OOM
* *******/
public class MyAnimationDrawable {
public static class MyFrame {
byte[] bytes;
int duration;
Drawable drawable;
boolean isReady = false;
}
public interface OnDrawableLoadedListener {
public void onDrawableLoaded(List<MyFrame> myFrames);
}
// 1
/***
* 性能更优
* 在animation-list中设置时间
* **/
public static void animateRawManuallyFromXML(int resourceId,
final ImageView imageView, final Runnable onStart,
final Runnable onComplete) {
loadRaw(resourceId, imageView.getContext(),
new OnDrawableLoadedListener() {
@Override
public void onDrawableLoaded(List<MyFrame> myFrames) {
if (onStart != null) {
onStart.run();
}
animateRawManually(myFrames, imageView, onComplete);
}
});
}
// 2
private static void loadRaw(final int resourceId, final Context context,
final OnDrawableLoadedListener onDrawableLoadedListener) {
loadFromXml(resourceId, context, onDrawableLoadedListener);
}
// 3
private static void loadFromXml(final int resourceId,
final Context context,
final OnDrawableLoadedListener onDrawableLoadedListener) {
new Thread(new Runnable() {
@Override
public void run() {
final ArrayList<MyFrame> myFrames = new ArrayList<MyFrame>();
XmlResourceParser parser = context.getResources().getXml(
resourceId);
try {
int eventType = parser.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
if (eventType == XmlPullParser.START_DOCUMENT) {
} else if (eventType == XmlPullParser.START_TAG) {
if (parser.getName().equals("item")) {
byte[] bytes = null;
int duration = 1000;
for (int i = 0; i < parser.getAttributeCount(); i++) {
if (parser.getAttributeName(i).equals(
"drawable")) {
int resId = Integer.parseInt(parser
.getAttributeValue(i)
.substring(1));
bytes = IOUtils.toByteArray(context
.getResources()
.openRawResource(resId));
} else if (parser.getAttributeName(i)
.equals("duration")) {
duration = parser.getAttributeIntValue(
i, 1000);
}
}
MyFrame myFrame = new MyFrame();
myFrame.bytes = bytes;
myFrame.duration = duration;
myFrames.add(myFrame);
}
} else if (eventType == XmlPullParser.END_TAG) {
} else if (eventType == XmlPullParser.TEXT) {
}
eventType = parser.next();
}
} catch (IOException e) {
e.printStackTrace();
} catch (XmlPullParserException e2) {
// TODO: handle exception
e2.printStackTrace();
}
// Run on UI Thread
new Handler(context.getMainLooper()).post(new Runnable() {
@Override
public void run() {
if (onDrawableLoadedListener != null) {
onDrawableLoadedListener.onDrawableLoaded(myFrames);
}
}
});
}
}).run();
}
// 4
private static void animateRawManually(List<MyFrame> myFrames,
ImageView imageView, Runnable onComplete) {
animateRawManually(myFrames, imageView, onComplete, 0);
}
// 5
private static void animateRawManually(final List<MyFrame> myFrames,
final ImageView imageView, final Runnable onComplete,
final int frameNumber) {
final MyFrame thisFrame = myFrames.get(frameNumber);
if (frameNumber == 0) {
thisFrame.drawable = new BitmapDrawable(imageView.getContext()
.getResources(), BitmapFactory.decodeByteArray(
thisFrame.bytes, 0, thisFrame.bytes.length));
} else {
MyFrame previousFrame = myFrames.get(frameNumber - 1);
((BitmapDrawable) previousFrame.drawable).getBitmap().recycle();
previousFrame.drawable = null;
previousFrame.isReady = false;
}
imageView.setImageDrawable(thisFrame.drawable);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
// Make sure ImageView hasn't been changed to a different Image
// in this time
if (imageView.getDrawable() == thisFrame.drawable) {
if (frameNumber + 1 < myFrames.size()) {
MyFrame nextFrame = myFrames.get(frameNumber + 1);
if (nextFrame.isReady) {
// Animate next frame
animateRawManually(myFrames, imageView, onComplete,
frameNumber + 1);
} else {
nextFrame.isReady = true;
}
} else {
if (onComplete != null) {
onComplete.run();
}
}
}
}
}, thisFrame.duration);
// Load next frame
if (frameNumber + 1 < myFrames.size()) {
new Thread(new Runnable() {
@Override
public void run() {
MyFrame nextFrame = myFrames.get(frameNumber + 1);
nextFrame.drawable = new BitmapDrawable(imageView
.getContext().g
sky947635060
- 粉丝: 0
- 资源: 13
最新资源
- Origin教程007所需练习数据
- 高速脉冲与高速计数指令.pdf
- 1.5T气缸气动式压机机械设计图纸+PPT+说明文档+技术方案资料+其它技术资料100%好用超级好的技术资料.zip
- 120°模温机工程图机械结构设计图纸和其它技术资料和技术方案非常好100%好用.zip
- 毕设和企业适用springboot自动化仓库管理平台类及智能会议管理平台源码+论文+视频.zip
- 毕设和企业适用springboot自动化仓库管理平台类及智能电商平台源码+论文+视频.zip
- 毕设和企业适用springboot自动化仓库管理平台类及智能办公平台源码+论文+视频.zip
- 毕设和企业适用springboot智能云服务平台类及企业供应链平台源码+论文+视频.zip
- 毕设和企业适用springboot自动化仓库管理平台类及智能客服系统源码+论文+视频.zip
- 毕设和企业适用springboot智能云服务平台类及企业数字资产管理平台源码+论文+视频.zip
- 毕设和企业适用springboot智能云服务平台类及企业云管理平台源码+论文+视频.zip
- 毕设和企业适用springboot智能云服务平台类及视频编辑平台源码+论文+视频.zip
- 毕设和企业适用springboot智能云服务平台类及视觉识别平台源码+论文+视频.zip
- 毕设和企业适用springboot智能云服务平台类及视频流平台源码+论文+视频.zip
- 毕设和企业适用springboot智能云服务平台类及数据处理平台源码+论文+视频.zip
- 毕设和企业适用springboot智能云服务平台类及虚拟银行平台源码+论文+视频.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈