2012-10-14 28 views
17

私は回転イメージアニメーションをしようとしています。 そのもののアイコンをプログレスバーのようにと同じように回転させる必要がありますが、私は円を中心に回転するイメージを取得しています。ここ は私のアニメーションコードは、私がここで間違っているつもりです回転アニメーションandroid

<?xml version="1.0" encoding="utf-8"?> 
<rotate 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:fromDegrees="0" 
android:toDegrees="360" 
android:interpolator="@android:anim/linear_interpolator" 
android:pivotX="50%" 
android:pivotY="50%" 
android:duration="2500" 
android:repeatCount="infinite" 
android:repeatMode="restart" 
/> 

のですか? おかげ

答えて

5

私が間違っていたところまあ私が得ました。私は自分のイメージにパディングを与え、回転させるたびに少しずつ移動させました。 とにかく、パディングを削除したところ、うまくいきました。

59

これはレイアウトmain.xmlのソースコードです:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <Button 
     android:id="@+id/testButton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="animationText" 
     android:onClick="AnimClick"/> 

    <ImageView 
     android:id="@+id/testImage" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerInParent="true" 
     android:contentDescription="image_desc" 
     android:scaleType="fitCenter" 
     android:src="@drawable/cat2" /> 

</RelativeLayout> 

回転アニメーションを実装するために、我々は、XMLやJavaコードでアニメーションを定義することができます。アニメーションをxmlに書きたい場合は、/ res/animフォルダの下にアニメーションxmlファイルを作成する必要があります。ここでは、rotate_around_center_point.xml

<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shareInterpolator="false" > 

    <rotate 
     android:duration="2500" 
     android:interpolator="@android:anim/linear_interpolator" 
     android:pivotX="50%" 
     android:pivotY="50%" 
     android:repeatCount="infinite" 
     android:repeatMode="restart" 
     android:toDegrees="360" /> 

</set> 

という名前のXMLファイルを作成し、これが私の活動です:

public class MainActivity extends Activity implements OnClickListener { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     Button btn = (Button) findViewById(R.id.testButton); 
     btn.setOnClickListener(this); 

    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.activity_main, menu); 
     return true; 
    } 

    @Override 
    public void onClick(View v) { 
     // TODO Auto-generated method stub 
     ImageView animationTarget = (ImageView) this.findViewById(R.id.testImage); 

     Animation animation = AnimationUtils.loadAnimation(this, R.anim.rotate_around_center_point); 
     animationTarget.startAnimation(animation); 

    } 


} 
+0

ありがとうございます。私は画像にscaletypeを追加しようとしましたが、それでも助けにはなりませんでした。 – orelzion

+0

ありがとう、それは動作します! – validcat

+1

これは受け入れられた答えだったはずです! – Machado

27

あなたはむしろXMLでそれを行うよりも、次のコードを試みることができる:

RotateAnimation rotate = new RotateAnimation(0, 360, 
     Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 
     0.5f); 

rotate.setDuration(4000); 
rotate.setRepeatCount(Animation.INFINITE); 
yourView.setAnimation(rotate); 
+0

あなたは、ループが完了した後、それが繰り返される前に、一時停止(ストール)を避ける方法を知っていますか? – portfoliobuilder

+6

rotate.setInterpolator(new LinearInterpolator())を追加してみてください。 –

+0

ありがとう、それは働いた! – Recomer

0

が、これはあなたを助ける

<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shareInterpolator="false" 
    android:duration="4000" 
    android:fromdegrees="0" 
    android:pivotx="50%" 
    android:pivoty="50%" 
    android:todegrees="360" 
    android:toyscale="0.0"> 
</set> 

希望アニメーションフォルダに次のXMLを追加します。..

詳細については、http://www.blazin.in/2013/09/simple-rotate-animation-in-android.html

関連する問題