android动画列表
我读过几篇文章,但没有一篇用动画列表解决我的问题。我已经知道我不能在onCreate()中启动动画,所以我将它放在onClick()中,但它仍然不起作用android动画列表,android,animation,Android,Animation,我读过几篇文章,但没有一篇用动画列表解决我的问题。我已经知道我不能在onCreate()中启动动画,所以我将它放在onClick()中,但它仍然不起作用 public class Main extends Activity implements OnClickListener { /** Called when the activity is first created. */ AnimationDrawable explosionAnimation; Button btnGo; ImageV
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();
与
因为在您的语句中,没有在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();
}
@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();
}