1

いくつかの画像が表示された画面があり、そのうちの 1 つは、ポートレートで見栄えのするきちんとしたアニメーションが表示されています。

縦向きの素晴らしいアニメーション - rotate_anim_port.png -ここに

風景の中ではそれほど素晴らしいものではありません。

横向きのアニメーション - 上のrotate_anim_land.png -。

この方法で中心から外れた回転アニメーションを実装しています。これは、同じサイズのドローアブル リソースをフレームビューに積み重ねて、それらすべてを比較的同じ場所に保持しながら、フレームビューのサイズに合わせてスケーリングできるためです。

RotateAnimation(fromDegrees, toDegrees, pivotXType, pivotXValue, pivotYType, pivotYValue)がそのpivotValuesの基になっているビューがビュー内の画像と同じサイズではないため、見栄えが悪くなります。縦向きの場合、画像はビューとほぼ同じサイズですが、横向きでは違いが明らかです。

私は、ビューとその内部に描画された画像の両方から位置を取得しようとして失敗し、適切なpivotValuesを計算してコードに渡しました。

アクティビティのコードは次のとおりです。

package com.namespace;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;

public class AnimationDemoActivity extends Activity {

// piviotValues if the view is the exact size of the drawable.
final float pivotXType = 0.3280274656679151f;
final float pivotYValue = 0.5060137457044674f;

private Handler mHandler = new Handler();
ImageView outside, inside;
RotateAnimation anim, anim2;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    outside = (ImageView) findViewById(R.id.imageView1);
    inside = (ImageView) findViewById(R.id.imageView2);

    outside.setVisibility(ImageView.INVISIBLE);
    inside.setVisibility(ImageView.INVISIBLE);

    // somewhere in here, code to recalculate pivotValues to adjust for the
    // size of the actual view the images are inside of

    anim = new RotateAnimation(0, 359, Animation.RELATIVE_TO_SELF,
            pivotXType, Animation.RELATIVE_TO_SELF, pivotYValue);
    anim.setInterpolator(new LinearInterpolator());
    anim.setRepeatCount(Animation.INFINITE);
    anim.setDuration(2000);

    anim2 = new RotateAnimation(0, 359, Animation.RELATIVE_TO_SELF,
            pivotXType, Animation.RELATIVE_TO_SELF, pivotYValue);
    anim2.setInterpolator(new LinearInterpolator());
    anim2.setRepeatCount(Animation.INFINITE);
    anim2.setDuration(750);

    mHandler.postDelayed(doRotation, 2500);
}

private Runnable doRotation = new Runnable() {
    public void run() {
        outside.setVisibility(ImageView.VISIBLE);
        inside.setVisibility(ImageView.VISIBLE);
        outside.startAnimation(anim);
        inside.startAnimation(anim2);
    }
};
}

レイアウトは次のとおりです。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff"
android:orientation="vertical"
android:padding="10sp" >

<ImageSwitcher
    android:id="@+id/imageSwitcher1"
    android:layout_width="fill_parent"
    android:layout_height="0dip"
    android:layout_weight="1"
    android:gravity="center" >
</ImageSwitcher>

<ImageSwitcher
    android:id="@+id/imageSwitcher2"
    android:layout_width="fill_parent"
    android:layout_height="0dip"
    android:layout_weight="1"
    android:gravity="center" >
</ImageSwitcher>

<FrameLayout
    android:layout_width="fill_parent"
    android:layout_height="0dip"
    android:layout_weight="1"
    android:gravity="center" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/dwdg_outside_arrow" >
    </ImageView>

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/dwdg_inside_arrow" >
    </ImageView>
</FrameLayout>

</LinearLayout>

ここに実行可能な apk - AnimationDemo.apk と圧縮されたプロジェクト - AnimationDemo.zip があります。

このようにするのは気が狂っていますか?私は完全に何かを見落としていますか?どうする?

4

1 に答える 1

1

私はそれを理解しました。もちろん、それは1行の修正でした。ImageViewsに属性android:adjustViewBounds="true"を追加すると、ビューがドローアブル リソースのサイズに縮小され、アニメーションが適切に機能するようになります。

于 2012-02-03T16:03:47.590 に答える