我有一系列的靜止圖像和共500多個圖像呈現在可繪製目錄中。我需要製作一個動畫(每秒載入大約20張圖片)。我希望它運行平穩,沒有內存異常。Android:從靜止圖像製作動畫
我想過要做到這一點,圖像2至3秒(40至60圖像)應該加載內存和顯示,然後他們應該放棄(釋放內存),然後圖像接下來的2到3秒應該加載。這種技術可以防止內存異常。它只是一個想法,我不知道它是否是一個好主意。請引導我一些更好的想法與一些代碼去...如果我的想法是更好,可以工作,那麼請告訴我一些幫助代碼來做到這一點。
閱讀答覆,做你的建議後,我已經寫了一些這樣的代碼:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/llMain">
<ViewFlipper android:id="@+id/imageflipper"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView android:id="@+id/ImageView01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="centerInside"
android:layout_gravity="center" />
</ViewFlipper>
</LinearLayout>
這裏是我做的動畫代碼:
public class Animation extends Activity {
ViewFlipper flipper;
int myIndex = 216;
private final Handler handler = new Handler();
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
flipper=(ViewFlipper)findViewById(R.id.imageflipper);
doTheAutoRefresh();
//displayData();
}
private void doTheAutoRefresh() {
handler.postDelayed(new Runnable() {
public void run(){
displayData(); // this is where you put your refresh code
doTheAutoRefresh();
}
}, 30);
}
private void displayData()
{
Resources r = getResources();
if(myIndex > 230){
myIndex = 216;
ImageView myImg = (ImageView)findViewById(R.id.ImageView01);
myImg.setImageResource(r.getIdentifier("drum0" + myIndex, "drawable", "com.vt.animation"));
myIndex += 1;
flipper.showNext();
}
else{
ImageView myImg = (ImageView)findViewById(R.id.ImageView01);
myImg.setImageResource(r.getIdentifier("drum0" + myIndex, "drawable", "com.vt.animation"));
myIndex += 1;
flipper.showNext();
}
}
}
但它非常緩慢。我將刷新時間設置爲30毫秒,但實際上刷新時間並不快,而刷新時間大約爲1秒。任何建議讓它快速感覺像真正的動畫?
謝謝,
@Mudasser Hassan如果服務器端的所有圖像,我必須下載所有圖像,並做這種類型的工作。 (播放視頻或圖像動畫)。我怎麼能做到這一點。我需要像360度旋轉一個單一的圖像顯示所有圖像。像http://www.mathieusavard.info/threesixty/demo.html – 2015-03-04 11:57:48
謝謝。如果所有圖像來自服務器,我們必須下載所有圖像並顯示。你有什麼想法嗎?我正面臨着這個問題。請幫忙。 – 2015-03-22 12:12:38
如果你使用'Handler.post()',代碼會更短。另一個問題是,你在UI線程上解碼圖像,這並不好。更優雅的解決方案是預先解碼背景中的下一個圖像(使用'AsyncTask'),並在200 ms後或解碼後顯示它,如果它需要更多時間。 – Mixaz 2016-09-02 22:03:58