[英]android animation-list
我已閱讀了幾篇文章,但沒有人使用動畫列表解決了我的問題。 我已經知道我無法在onCreate()中啟動動畫,所以我將它放在onClick()中,但它仍然不起作用...
public class Main extends Activity implements OnClickListener {
/** Called when the activity is first created. */
AnimationDrawable explosionAnimation;
Button btnGo;
ImageView explosionImage;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
explosionImage = (ImageView) findViewById(R.id.balloon);
explosionImage.setBackgroundResource(R.drawable.explosion);
explosionAnimation = (AnimationDrawable) explosionImage.getBackground();
btnGo = (Button)findViewById(R.id.btnGo);
btnGo.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId())
{
case R.id.btnGo:
explosionAnimation.start();
break;
}
}
}
explosion.xml
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="true">
<item android:drawable="@drawable/explode_1" android:duration="200" />
<item android:drawable="@drawable/explode_2" android:duration="200" />
<item android:drawable="@drawable/explode_3" android:duration="200" />
<item android:drawable="@drawable/explode_4" android:duration="200" />
<item android:drawable="@drawable/explode_5" android:duration="200" />
</animation-list>
main.xml中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<ImageView
android:id="@+id/balloon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="30dp"
android:src="@drawable/balloon" />
<Button
android:id = "@+id/btnGo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:text="Play"
/>
</LinearLayout>
我認為你應該更換
explosionAnimation.start();
同
explosionImage startAnimation(explosionAnimation);
因為在你的陳述中,沒有指定“你想在哪個視圖上開始動畫!”
在你的main.xml上將圖像視圖src更改為android:src="@drawable/explosion"
然后在你的Main.java上
更改
explosionAnimation=(AnimationDrawable)((ImageView) explosionImage).getDrawable();
試試吧。 它應該工作
好吧,我建議你把它放在你的活動的onWindowsFocusChange方法中,在一個線程內。 像那樣:
@Override
public void onWindowFocusChanged(boolean hasFocus)
{
super.onWindowFocusChanged(hasFocus);
delayedactivation(R.drawable.animationname, yourview, delay);
}
public void delayedActivation(final int res, final View view, final int delay)
{
final Handler handle = new Handler();
Thread t = new Thread()
{
@Override
public void synchronized run()
{
try
{
handle.postDelayed(new Runnable()
{
@Override
public void run()
{
view.setBackgroundResource(res);
anime = (AnimationDrawable) getResources().getDrawable(res);
anime = (AnimationDrawable)view.getBackground();
anime.start();
}
},delay);
}catch(Exception e)
{
e.printStackTrace();
}
};
};
t.start();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.