由于几个图像后,AnimationDrawable的内存会爆炸,我需要编写自己的内存。谷歌有一些关于高效帧动画的演示,但它非常复杂(不必要?)。
这是我最初的方法,我可以让一些专家/智能人士参与进来吗?
以下是代码:
private void addTask(Handler handler, final String animationName, final int frameNumber, final AsyncTaskLoadBitmap asyncLoadBitmap, int frameIndex) {
Runnable runnable = new Runnable() {
@Override
public void run() {
int frameIdentifier = getResources().getIdentifier(animationName, "drawable", getPackageName());
Log.v("Runable","adding name");
new AsyncTaskLoadBitmap(imageView,thisActivity).execute(frameIdentifier);
}
};
boolean done = handler.postDelayed(runnable,100 + (100 * frameIndex));
Log.v("PostDelayed","Done");
}
我遇到的问题是让它循环并停止循环。
什么是更好的方法?
答案 0 :(得分:1)
试试这个:
final ImageView iv = new ImageView(this);
setContentView(iv);
final int LOAD = 1;
final int SHOW = 2;
HandlerThread loader = new HandlerThread("frameLoader");
loader.start();
Callback callback = new Callback() {
int idx = 0;
int[] ids = {
R.drawable.ic_launcher, R.drawable.layer0, R.drawable.layer1,
};
Resources res = getResources();
@Override
public boolean handleMessage(Message msg) {
if (msg.what == LOAD) {
Log.d(TAG, "handleMessage LOAD " + Thread.currentThread().getName());
idx++;
if (idx < 18) {
Bitmap btm = BitmapFactory.decodeResource(res, ids[idx % ids.length]);
msg = mUIHandler.obtainMessage(SHOW, btm);
mUIHandler.sendMessage(msg);
}
} else
if (msg.what == SHOW) {
Log.d(TAG, "handleMessage SHOW " + Thread.currentThread().getName());
iv.setImageBitmap((Bitmap) msg.obj);
mBackgroundHandler.sendEmptyMessageDelayed(LOAD, 500);
}
return true;
}
};
mBackgroundHandler = new Handler(loader.getLooper(), callback);
mUIHandler = new Handler(callback);
mBackgroundHandler.sendEmptyMessage(LOAD);