[英]Display Animation using animation drawable
我已經在android開發者網站上閱讀了如何將圖像序列轉換為動畫的內容。 但是什么也不顯示,圖像和動畫都不顯示。
package org.example.anim;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.widget.ImageView;
public class MainActivity extends Activity {
AnimationDrawable frameAnimation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView img = (ImageView)findViewById(R.id.spinning_wheel_image);
img.setBackgroundResource(R.drawable.spin_animation);
// Get the background, which has been compiled to an AnimationDrawable
frameAnimation = (AnimationDrawable) img.getBackground();
// Start the animation (looped playback by default).
}
public void onWindowFocusChanged()
{
frameAnimation.start();
}
}
ImageView progressSpinner = (ImageView) findViewById(R.id.progressSpinner);
// Set the background of the image - In this case an animation
// (/res/anim folder)
progressSpinner.setBackgroundResource(R.anim.progress_animation);
// Get the image background and attach the AnimationDrawable to it.
progressAnimation = (AnimationDrawable) progressSpinner.getBackground();
progressAnimation.start();
將圖像從可繪制圖像移動到anim文件夾,然后在onCreate中嘗試此代碼。
如果您添加了多個圖像,並且spin_animation是這樣的列表:
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="true">
<item android:drawable="@drawable/rocket_thrust1" android:duration="200" />
<item android:drawable="@drawable/rocket_thrust2" android:duration="200" />
<item android:drawable="@drawable/rocket_thrust3" android:duration="200" />
</animation-list>
然后代碼必須工作,我認為您沒有正確生成此列表。也請參見此鏈接以獲取更多有關動畫可繪制的幫助
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.