我有4個圖像序列(png格式),我需要爲我的動畫使用。每個序列大約50幀長,每幀大約300x300,所以我有30 MB的資源加載。什麼是加載它們以避免內存泄漏的最有效方式?我應該去尋找xml可繪製動畫還是有更好的方法?爲動畫加載大型圖像序列的有效方法
我不需要同時在屏幕上顯示它們。只顯示一個動畫。
我有4個圖像序列(png格式),我需要爲我的動畫使用。每個序列大約50幀長,每幀大約300x300,所以我有30 MB的資源加載。什麼是加載它們以避免內存泄漏的最有效方式?我應該去尋找xml可繪製動畫還是有更好的方法?爲動畫加載大型圖像序列的有效方法
我不需要同時在屏幕上顯示它們。只顯示一個動畫。
也許是有點遲了:)但我希望這可以幫助其他用戶。 最後,我用一個簡單的循環來解決它,它加載一個圖像,繪製並摧毀它。
int frames = 50;
//LOADING-REMOVING NEEDED FRAMES
//set animation range
i = currentFrame % frames;
//frame to cache
frameList.add(BitmapsLoader(i)); //custom function to load a specified frame from res
if (frameList.size() == 3) {
//frame to draw
canvas.drawBitmap(frameList.get(1), left, top, null);
//frame to remove
frameList.get(0).recycle();
frameList.remove(0);
}
這會保持內存分配穩定,無論您的動畫有多少幀。很明顯,它在CPU上花費更多,因爲我們不是預先加載所有資源,而是在每一幀中繼續加載它們。儘管有這些事實,我的應用程序運行平穩。 在三星Galaxy S3和Galaxy Tab 4上測試。
非常感謝。 我面臨着同樣的問題,在看到你的答案一段時間後,我通過覆蓋AnimationDrawable來解決它。 因此,如果問題在於你無法加載陣列中的所有圖像,因爲它對於內存來說太大了,所以在你需要的時候加載圖像。 我AnimationDrawable是這樣的:
public abstract class MyAnimationDrawable extends AnimationDrawable {
private Context context;
private int current;
private int reqWidth;
private int reqHeight;
private int totalTime;
public MyAnimationDrawable(Context context, int reqWidth, int reqHeight) {
this.context = context;
this.current = 0;
//In my case size of screen to scale Drawable
this.reqWidth = reqWidth;
this.reqHeight = reqHeight;
this.totalTime = 0;
}
@Override
public void addFrame(Drawable frame, int duration) {
super.addFrame(frame, duration);
totalTime += duration;
}
@Override
public void start() {
super.start();
new Handler().postDelayed(new Runnable() {
public void run() {
onAnimationFinish();
}
}, totalTime);
}
public int getTotalTime() {
return totalTime;
}
@Override
public void draw(Canvas canvas) {
try {
//Loading image from assets, you could make it from resources
Bitmap bmp = BitmapFactory.decodeStream(context.getAssets().open("presentation/intro_000"+(current < 10 ? "0"+current : current)+".jpg"));
//Scaling image to fitCenter
Matrix m = new Matrix();
m.setRectToRect(new RectF(0, 0, bmp.getWidth(), bmp.getHeight()), new RectF(0, 0, reqWidth, reqHeight), Matrix.ScaleToFit.CENTER);
bmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), m, true);
//Calculating the start 'x' and 'y' to paint the Bitmap
int x = (reqWidth - bmp.getWidth())/2;
int y = (reqHeight - bmp.getHeight())/2;
//Painting Bitmap in canvas
canvas.drawBitmap(bmp, x, y, null);
//Jump to next item
current++;
} catch (IOException e) {
e.printStackTrace();
}
}
abstract void onAnimationFinish();
}
玩到現在,你需要做的動畫下一步
//Get your ImageView
View image = MainActivity.this.findViewById(R.id.presentation);
//Create AnimationDrawable
final AnimationDrawable animation = new MyAnimationDrawable(this, displayMetrics.widthPixels, displayMetrics.heightPixels) {
@Override
void onAnimationFinish() {
//Do something when finish animation
}
};
animation.setOneShot(true); //dont repeat animation
//This is just to say that my AnimationDrawable has 72 frames with 50 milliseconds interval
try {
//Always load same bitmap, anyway you load the right one in draw() method in MyAnimationDrawable
Bitmap bmp = BitmapFactory.decodeStream(MainActivity.this.getAssets().open("presentation/intro_00000.jpg"));
for (int i = 0; i < 72; i++) {
animation.addFrame(new BitmapDrawable(getResources(), bmp), 50);
}
} catch (IOException e) {
e.printStackTrace();
}
//Set AnimationDrawable to ImageView
if (Build.VERSION.SDK_INT < 16) {
image.setBackgroundDrawable(animation);
} else {
image.setBackground(animation);
}
//Start animation
image.post(new Runnable() {
@Override
public void run() {
animation.start();
}
});
這是所有和工程確定形成我!
你能否詳細說明你的答案?你有沒有對AnimationDrawable類進行分類,或者你是否以自己的方式實現了它?這是在一個OnDraw方法嗎?我真的想嘗試你的方法,請分享更多的解決方案... –