我有6张图像作为动画加载在一起,它们存储在mFrame中。在调用较小的方法加载它们之前,下面的方法准备这些图像。我有时在尝试加载它时会遇到OutOfMemoryError。我用另一种方法加载图像:mImageView.setImageDrawable(mAnimHardRef); mAnimHardRef.start();
private void loadAnimationAsync() {
AsyncTask.execute(new Runnable() {
@Override
public void run() {
long time = System.currentTimeMillis();
final AnimationDrawable drawable = new AnimationDrawable();
drawable.setOneShot(true);
for(int frRes : mFrames) {
Bitmap temp = BitmapFactory.decodeResource(mActivity.getResources(), frRes);
temp = Bitmap.createScaledBitmap(temp, temp.getWidth() / 6, temp.getHeight() / 6, false);
drawable.addFrame(new BitmapDrawable(temp), 60);
}
mAnimSoftRef = new SoftReference<AnimationDrawable>(drawable);
mAnimHardRef = drawable;
if (mShowAnim) mActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
if (mShowAnim) showAnimationNow();
}
});
Log.d("Joshua", "BG load time: " + (System.currentTimeMillis() - time));
}
});
}