[英]Create animated splash screen using frames on Android
所以這是交易,我搜索了每個問題並在線鏈接,但沒有一個是有幫助的。 我的啟動畫面有.jpg格式的120幀動畫。 我知道jpeg會在內存中轉換為位圖,這就是我得到OutOfMemoryError的原因。 我獲得動畫的最大幀數是10.有沒有辦法逐幀完成,或者我應該嘗試別的東西。 這是我的代碼:
final AnimationDrawable anim = new AnimationDrawable();
anim.setOneShot(true);
for (int i = 1; i <= 120; i++)
{
Drawable logo = getResources().getDrawable(getResources()
.getIdentifier("l"+i, "drawable", getPackageName()));
anim.addFrame(logo, 50);
if (i % 3 == 0)
{
System.gc();
}
}
ImageView myImageView = (ImageView) findViewById(R.id.SplashImageView);
myImageView.setBackgroundDrawable(anim);
myImageView.post(new Runnable()
{
public void run()
{
anim.start();
}
});
我已將120個jpegs放在drawable文件夾下,帶有“l”前綴(例如l1,l2等)。 我每3個jpeg做垃圾收集,但這不會做任何事情。
您可以嘗試使用Handler.postDelayed
在沒有AnimationDrawable
情況下執行此Handler.postDelayed
。 像這樣的東西:
final ImageView image = (ImageView) findViewById(R.id.SplashImageView);
final Handler handler = new Handler();
final Runnable animation = new Runnable() {
private static final int MAX = 120;
private static final int DELAY = 50;
private int current = 0;
@Override
public void run() {
final Resources resources = getResources();
final int id = resources.getIdentifier("l" + current, "drawable", getPackageName());
final Drawable drawable = resources.getDrawable(id);
image.setBackgroundDrawable(drawable);
handler.postDelayed(this, DELAY);
current = (current + 1) % MAX;
}
};
handler.post(animation);
此解決方案需要較少的內存,因為它當時只保留一個可繪制的內存。
您可以使用handler.removeCallbacks(animation);
取消handler.removeCallbacks(animation);
。
如果你想制作一次性動畫,你可以有條件地調用handler.postDelayed
:
if (current != MAX - 1) {
handler.postDelayed(this, DELAY);
}
需要調用̶.̶r̶e̶c̶y̶c̶l̶e̶(̶)̶對位圖你請勿使用̶a̶n̶y̶m̶o̶r̶e̶.̶否則他們不會被垃圾收集̶p̶r̶o̶p̶e̶r̶l̶y̶.̶
另外在清單集中使用大堆為true。 這為您提供了更多的呼吸空間。 >)
我嘗試了所有這些解決方案,每一個都變得更好:D我可以使用更多的幀和更高的分辨率,但仍然不滿足經理:(我發現最好的解決方案是使用視頻而不是幀它就像一個魅力甚至低內存設備我在xperia u上使用256mb ram的視頻編解碼器(H.264 mp4)測試了每個尺寸為480 * 854的150幀
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.