3

私はいくつかの記事を読みましたが、アニメーションリストを使用して問題を修正したものはありません。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;
    }
}
} 

爆発.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>
4

3 に答える 3

0

交換する必要があると思います

explosionAnimation.start();

explosionImage startAnimation(explosionAnimation);

あなたの声明では、「どのビューでアニメーションを開始したいのか!」と指定されていないためです。

于 2012-11-29T04:13:41.547 に答える
0

main.xmlで、画像ビューのsrcを次のように変更しますandroid:src="@drawable/explosion"

次に、Main.javaで

変化する

explosionAnimation=(AnimationDrawable)((ImageView) explosionImage).getDrawable();

それを試してみてください。動作するはずです

于 2012-11-29T04:15:32.093 に答える
0

さて、スレッド内のアクティビティの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();     
}
于 2013-05-23T02:23:13.437 に答える