10

イメージビュー内で実行しているアニメーションは、イメージ フレームのアスペクト比を維持することを拒否します。SOの次の回答は非常に有益ですが、私にはうまくいかないようです: アスペクト比を維持するためにImageViewで画像をスケーリングする方法

コードは次のとおりです。

private void startAnimation(){
    mImageView.setAdjustViewBounds(true);
    mImageView.setScaleType(ScaleType.CENTER);
    mImageView.setBackgroundResource(R.anim.my_animation);

    AnimationDrawable frameAnimation = (AnimationDrawable) mImageView.getBackground();

     // Start the animation (looped playback by default).
     frameAnimation.start();
}

R.anim.my_animation は単なるアニメーション リストです。

<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/selected"
android:oneshot="false">
<item
    android:drawable="@drawable/photo_1"
    android:duration="100" />
<item
    android:drawable="@drawable/photo__2"
    android:duration="100" />
    ... and so on...
</animation-list>
4

2 に答える 2

12

アニメーション ドローアブルをイメージビューのバックグラウンドに設定する代わりに、src を使用してフォアグラウンドに設定し、そこでアニメーションを再生させます。フレーム アニメーションのすべての画像は、画像ビューに適切なスケール タイプを設定すると、縦横比を維持したままサイズ変更されます。

    private void startAnimation(){
    mImageView.setAdjustViewBounds(true);
    mImageView.setScaleType(ScaleType.CENTER);
    mImageView.setImageDrawable(getResources().getDrawable(R.anim.my_animation)); 

    AnimationDrawable frameAnimation = (AnimationDrawable) mImageView.getDrawable();

     // Start the animation (looped playback by default).
     frameAnimation.start();
}
于 2013-12-24T18:43:33.080 に答える
0

これは少しトリッキーですが、うまくいきます。アニメーション リストに 2 つの写真があります

<animation-list xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/selected" android:oneshot="false">
    <item android:drawable="@drawable/logo1" android:duration="5000" />
    <item android:drawable="@drawable/logo2" android:duration="300" />
</animation-list>

次に、log1/2 と同じサイズですが、完全に透明な 3 番目の画像 (logo0) を追加しました。最後に、この ImageView を使用します。

<ImageView
    android:id="@+id/imageViewLogo"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:adjustViewBounds="true"
    android:layout_margin="5dp"
    android:src="@drawable/logo0"
/>

これで、アニメーションは写真のロゴの縦横比を保持します*。

コードは次のとおりです。

    @Override
    public void onCreate(Bundle savedInstanceState) {
    ...
    imageView = (ImageView) findViewById(R.id.imageViewLogo);
    imageView.setBackgroundResource(R.drawable.logo_animation);
    ...


    public void onWindowFocusChanged (boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
     AnimationDrawable frameAnimation = (AnimationDrawable) imageView.getBackground();
     if(hasFocus) { frameAnimation.start(); } else { frameAnimation.stop(); }
    }

これは非常に単純で、追加のダミー画像をリソースに追加するだけで済みます。追加のコードや複雑な計算は必要ありません。

于 2013-03-11T22:00:06.327 に答える