64

イメージ ビューと Web ビューを含むレイアウトを作成しました。Web ビューは、既定の可視性がなくなったように設定されています。アクティビティが起動すると、最初に画像ビューが表示され、Web ビューがその URL の読み込みを完了すると、それ自体が可視としてマークされ、画像ビューは非表示としてマークされます。

イメージビューが表示されたら、少しだけピッツアを追加するために繰り返し回転させたいと思います。

私はこれまで Android でアニメーションを作成したことがなく、インターネットに問い合わせたときに見つけたすべての投稿が役に立ちませんでした。したがって、私は助けを求めて SO に戻りました。

じゃあこれで始めたら…

    final ImageView splash = (ImageView)findViewById(R.id.splash);

繰り返し回転アニメーションを作成して ImageView に適用するにはどうすればよいですか?

再度、感謝します!

4

9 に答える 9

109

を使用してRotateAnimation、ピボット ポイントを画像の中心に設定します。

RotateAnimation anim = new RotateAnimation(0f, 350f, 15f, 15f);
anim.setInterpolator(new LinearInterpolator());
anim.setRepeatCount(Animation.INFINITE);
anim.setDuration(700);

// Start animating the image
final ImageView splash = (ImageView) findViewById(R.id.splash);
splash.startAnimation(anim);

// Later.. stop the animation
splash.setAnimation(null);
于 2010-01-09T13:20:55.980 に答える
53

画像を中心に回転させる方法:

ImageView view = ... //Initialize ImageView via FindViewById or programatically

RotateAnimation anim = new RotateAnimation(0.0f, 360.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);

//Setup anim with desired properties
anim.setInterpolator(new LinearInterpolator());
anim.setRepeatCount(Animation.INFINITE); //Repeat animation indefinitely
anim.setDuration(700); //Put desired duration per anim cycle here, in milliseconds

//Start animation
view.startAnimation(anim); 
//Later on, use view.setAnimation(null) to stop it.

これにより、画像が中心を中心に回転します(幅/高さの0.5または50%)。私のようにグーグルからここに来て、絶対ピクセルでその中心を定義せずにその中心の周りで画像を回転させたい将来の読者のためにこれを投稿しています。

于 2012-08-17T13:58:51.973 に答える
26

回転アニメーション機能を使用することもできます。ImageView で、事前に決められた時間、特定のアニメーションを実行します。

Animation rotate = AnimationUtils.loadAnimation([context], R.anim.rotate_picture);
splash.startAnimation(rotate);

次に、res/anim に、rotate_picture という内容のアニメーション XML ファイルを作成します。

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

    <rotate 
    android:fromDegrees="0"
    android:toDegrees="360"
    android:duration="5000"
    android:pivotX="50%"
    android:pivotY="50%">
</rotate>
</set>

残念ながら、これは一度しか実行されません。待機中にアニメーションを繰り返すには、どこかにループが必要です。少し実験したところ、プログラムが無限ループに陥ってしまったので、最善の方法がわかりません。編集:クリストファーの答えは、それを適切にループさせる方法に関する情報を提供するため、個別のスレッドに関する私の悪い提案を削除します!

于 2010-01-09T13:08:00.143 に答える
11

一方向 - 画像を N に分割し、毎回少しずつ回転させます。5枚あれば十分だと思います。次に、ドローアブルでこのようなものを作成します

<animation-list   android:id="@+id/handimation" android:oneshot="false" 
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/progress1" android:duration="150" />
    <item android:drawable="@drawable/progress2" android:duration="150" />
    <item android:drawable="@drawable/progress3" android:duration="150" />
 </animation-list> 

コード開始

progress.setVisibility(View.VISIBLE);
AnimationDrawable frameAnimation = (AnimationDrawable)progress.getDrawable();
frameAnimation.setCallback(progress);
frameAnimation.setVisible(true, true);

コードストップ

AnimationDrawable frameAnimation = (AnimationDrawable)progress.getDrawable();
frameAnimation.stop();
frameAnimation.setCallback(null);
frameAnimation = null;
progress.setVisibility(View.GONE);

詳細はこちら

于 2010-01-09T04:28:53.320 に答える
7
imgDics = (ImageView) v.findViewById(R.id.img_player_tab2_dics);
    imgDics.setOnClickListener(onPlayer2Click);
    anim = new RotateAnimation(0f, 360f,
            Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
                            0.5f);
    anim.setInterpolator(new LinearInterpolator());
    anim.setRepeatCount(Animation.INFINITE);
    anim.setDuration(4000);

    // Start animating the image
    imgDics.startAnimation(anim);
于 2013-09-19T06:31:48.193 に答える
5

要素の中に入れます:

android:repeatCount="infinite"
于 2011-04-09T14:32:25.447 に答える
3

.getWidth/2 などを使用するとうまくいかないことがわかりました。画像のピクセル数を取得し、それを自分で 2 で割ってから、最後の 2 つの引数。

たとえば、画像が 120 ピクセル x 120 ピクセルの正方形であるとすると、x と y は 60 ピクセルになります。あなたのコードでは、あなたは正しいでしょう:

RotateAnimation anim = new RotateAnimation(0f, 350f, 60f, 60f);
anim.setInterpolator(new LinearInterpolator());
anim.setRepeatCount(Animation.INFINITE);
anim.setDuration(700);

これで、画像が中心を中心に回転します。

于 2012-04-07T02:12:31.143 に答える