2012-06-26 31 views
7

android.myでループを繰り返してイメージビューを回転させたいコードの回転が完全に動作していますrepeat mode.if繰り返しモードを繰り返し設定しますアニメーションが動作しませんが、そして、私はループのアニメーションを回転させたいと思います。イメージアニメーションを回転するアニメーションでループが機能しない

はここに、事前にここに私のJavaクラス

import android.app.Activity; 
import android.os.Bundle; 
import android.view.animation.Animation; 
import android.view.animation.Animation.AnimationListener; 
import android.view.animation.AnimationUtils; 
import android.widget.ImageView; 

public class AnimationActivity extends Activity { 
    /** Called when the activity is first created. */ 
    ImageView my_image; 
    AnimationListener listener; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     listener = new AnimationListener() { 
      @Override public void onAnimationStart(Animation animation) {} 
      @Override public void onAnimationRepeat(Animation animation) {} 
      @Override 
      public void onAnimationEnd(Animation animation) { 
       System.out.println("End Animation!"); 
       //load_animations(); 
      } 
     }; 

     my_image=(ImageView)findViewById(R.id.my_img); 
     load_animations(); 



    } 
    void load_animations() 
    { 
     new AnimationUtils(); 
     Animation rotation = AnimationUtils.loadAnimation(this, R.anim.rotation); 
     rotation.setRepeatCount(-1); 
     rotation.setRepeatMode(2); 
     rotation.setAnimationListener(listener); 
     my_image.startAnimation(rotation); 
    } 

} 

感謝のアニメーションXML

<?xml version="1.0" encoding="utf-8"?> 
<rotate 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:fromDegrees="45" 
    android:toDegrees="45" 
    android:pivotX="50%" 
    android:pivotY="50%" 
    android:duration="100" 
    android:startOffset="0" 
/> 

です!

答えて

10

最後に、私は解決策は、XMLの下にしようとしました:

<?xml version="1.0" encoding="utf-8"?> 
<rotate 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:fromDegrees="0" 
    android:toDegrees="360" 
    android:pivotX="50%" 
    android:pivotY="50%" 
    android:duration="2000" 
    android:repeatMode="reverse" 
    android:repeatCount="infinite" 
    android:startOffset="0" 
/> 

ここでコードが完璧に働いているのマイクラス

import android.app.Activity; 
import android.os.Bundle; 
import android.view.animation.Animation; 
import android.view.animation.Animation.AnimationListener; 
import android.view.animation.AnimationUtils; 
import android.widget.ImageView; 

public class AnimationActivity extends Activity { 
    /** Called when the activity is first created. */ 
    ImageView my_image; 
    AnimationListener listener; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     listener = new AnimationListener() { 
      @Override public void onAnimationStart(Animation animation) {} 
      @Override public void onAnimationRepeat(Animation animation) {} 
      @Override 
      public void onAnimationEnd(Animation animation) { 
       System.out.println("End Animation!"); 
       //load_animations(); 
      } 
     }; 

     my_image=(ImageView)findViewById(R.id.my_img); 
     load_animations(); 



    } 
    void load_animations() 
    { 
     new AnimationUtils(); 
     Animation rotation = AnimationUtils.loadAnimation(this, R.anim.rotation); 
     rotation.setAnimationListener(listener); 
     my_image.startAnimation(rotation); 
    } 

} 

です!

問題が解決しました。

0

インターネットからの回答を調べた後、完全に私のために働くソリューションを見つけました。 (そして、そうです、repeatCountとrepeatModeは、AnimationSetと一緒に使用すると非常にバグがあります)。

anim_rotate_fade.xml:アクティビティで

<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android" 
    android:interpolator="@android:anim/accelerate_decelerate_interpolator" 
    android:ordering="together" > 

    <objectAnimator 
     android:duration="3000" 
     android:propertyName="rotation" 
     android:repeatCount="1" 
     android:valueTo="360" 
     android:valueType="floatType" /> 

    <objectAnimator 
     android:duration="3000" 
     android:propertyName="alpha" 
     android:repeatCount="1" 
     android:repeatMode="reverse" 
     android:valueFrom="0.0" 
     android:valueTo="0.3" 
     android:valueType="floatType" /> 

    <objectAnimator 
     android:duration="3000" 
     android:propertyName="y" 
     android:repeatCount="1" 
     android:repeatMode="reverse" 
     android:valueFrom="380" 
     android:valueTo="430" 
     android:valueType="floatType" /> 

</set> 

: (終了アニメーション後にわずかな遅延を導入することによってそれを解決します)。

研究したいクラスがたくさんありますが、現在は柔軟性の高いobjectAnimatorを使用しています。

  • アニメーション
  • AnimationUtils
  • アニメーター
  • AnimatorInflater
  • AnimatorListener
  • AnimatorListenerAdapter
:私は、アニメーションやAnimationUtilsを使用することをお勧めしません
関連する問題