22

回転画像アニメーションを実行しようとしています。プログレスバーと同じようにアイコンを回転させる必要がありますが、円の周りを回転する画像が得られます。これが私のアニメーションコードです

<?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"
/>

ここでどこが間違っていますか?ありがとう

4

4 に答える 4

69

これは、レイアウト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ファイルを作成する必要があります。ここでは、という名前の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>

これが私のアクティビティです:

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);

    }


}
于 2012-10-14T15:13:14.927 に答える
32

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);
于 2015-01-10T12:43:40.623 に答える
4

さて、私は間違っていたところに行きました。画像にパディングを施したため、画像が回転するたびに少し横に移動しました。とにかく私はパディングを削除しました、そして今それはうまく働いています。

于 2012-10-16T12:43:19.170 に答える
0

animation フォルダーに次の xml を追加します。

<?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>

これがあなたを助けることを願っています..

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

于 2016-02-10T07:41:11.833 に答える