天天看點

Android Api Demos登頂之路(九十八)Text-->Animation->3D Transition

/*
 * 這個demo示範了視圖之間切換的動畫。
 * 1.layoutAnimation用來為layout或者Viewgroup的子View添加動畫效果。
 * 建立layoutAnimation的xml檔案:
 * delay:表示子View出現的時間間隔;animationOrder:子View出現的順序;animation:子View出現的動畫。
 * 在本例中為ListView添加了layoutAnimation="@anim/layoutanimation"動畫,子view從左至右移進
 * 并從下至上依次添加。
 */
public class MainActivity extends Activity implements OnItemClickListener,
        OnClickListener {
    private ViewGroup mContainer;
    private ListView mList;
    private ImageView mImage;
    private boolean clickImage = false;

    private static final String[] PHOTOS_NAMES = new String[] { "Lyon",
            "Livermore", "Tahoe Pier", "Lake Tahoe", "Grand Canyon", "Bodie" };

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

        mContainer = (ViewGroup) findViewById(R.id.container);
        mList = (ListView) findViewById(R.id.list);
        mImage = (ImageView) findViewById(R.id.imageView);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, PHOTOS_NAMES);
        mList.setAdapter(adapter);
        mList.setOnItemClickListener(this);

        mImage.setImageResource(R.drawable.gallery_photo_6);
        mImage.setClickable(true);
        mImage.setFocusable(true);
        mImage.setOnClickListener(this);

        // 因為我們緩存一個較大的視圖,是以在兩個動畫之間我們需要保留緩存
        mContainer
                .setPersistentDrawingCache(ViewGroup.PERSISTENT_ANIMATION_CACHE);
    }

    /*
     * 應用旋轉動畫
     */
    private void applyRotation(float start, float end) {
        float centerX = mContainer.getWidth() / ;
        float centerY = mContainer.getHeight() / ;

        // 定義3d旋轉動畫
        Rotate3dAnimation rotation = new Rotate3dAnimation(start, end, centerX,
                centerY, f, true);
        rotation.setDuration();
        // 動畫結束後填充
        rotation.setFillAfter(true);
        // 設定插入器為加速插入器
        rotation.setInterpolator(new AccelerateInterpolator());
        // 設定對動畫的監聽
        rotation.setAnimationListener(new DisplayNextView());
        mContainer.startAnimation(rotation);
    }

    private class DisplayNextView implements Animation.AnimationListener {

        @Override
        public void onAnimationStart(Animation animation) {

        }

        @Override
        public void onAnimationEnd(Animation animation) {
            // 将Runable對象發送到主線程隊列,用來重新整理主線程
            mContainer.post(new SwapViews());
        }

        @Override
        public void onAnimationRepeat(Animation animation) {

        }

    }

    /*
     * 視圖切換動畫,從動畫的後半部分開始
     */
    private class SwapViews implements Runnable {

        @Override
        public void run() {
            float centerX = mContainer.getWidth() / ;
            float centerY = mContainer.getHeight() / ;
            Rotate3dAnimation rotation;

            if (!clickImage) {
                mList.setVisibility(View.GONE);
                mImage.setVisibility(View.VISIBLE);
                mImage.requestFocus();
                rotation = new Rotate3dAnimation(, , centerX, centerY,
                        f, false);
            }else{
                mList.setVisibility(View.VISIBLE);
                mImage.setVisibility(View.GONE);
                mList.requestFocus();
                clickImage=false;
                rotation = new Rotate3dAnimation(, , centerX, centerY,
                        f, false);
            }
            rotation.setDuration();
            rotation.setFillAfter(true);
            rotation.setInterpolator(new DecelerateInterpolator());
            mContainer.startAnimation(rotation);
        }

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        applyRotation(, );
    }

    @Override
    public void onClick(View v) {
        clickImage=true;
        //System.out.println("click");
        applyRotation(, );
    }

}
           

activity_main.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/container" >

    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/hello_world" 
        android:persistentDrawingCache="animation|scrolling"
        android:layoutAnimation="@anim/layoutanimation"/>
    <ImageView 
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="fitCenter"
        android:visibility="gone"
        />

</FrameLayout>
           

layoutanimation.xml

<?xml version="1.0" encoding="utf-8"?>
<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
        android:delay="30%"
        android:animationOrder="reverse"
        android:animation="@anim/slide_right" />
           

slide_right.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator" >

    <translate
        android:duration="1000"
        android:fromXDelta="-100%p"
        android:toXDelta="0" />

</set>
           

Rotate3dAnimation.java

/**
     * 建立一個可以繞y軸旋轉的動畫效果
     *
     * @param fromDegrees 3D旋轉開始的角度
     * @param toDegrees 結束角度
     * @param centerX 旋轉中心的X坐标
     * @param centerY 旋轉中心的Y坐标
     * @param depthZ 在Z軸方向拉伸的距離
     * @param reverse 是否反向旋轉
     */
    public Rotate3dAnimation(float fromDegrees, float toDegrees,
            float centerX, float centerY, float depthZ, boolean reverse) {
        mFromDegrees = fromDegrees;
        mToDegrees = toDegrees;
        mCenterX = centerX;
        mCenterY = centerY;
        mDepthZ = depthZ;
        mReverse = reverse;
    }

    @Override
    public void initialize(int width, int height, int parentWidth, int parentHeight) {
        super.initialize(width, height, parentWidth, parentHeight);
        mCamera = new Camera();
    }

    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {
        final float fromDegrees = mFromDegrees;
        float degrees = fromDegrees + ((mToDegrees - fromDegrees) * interpolatedTime);

        final float centerX = mCenterX;
        final float centerY = mCenterY;
        final Camera camera = mCamera;

        final Matrix matrix = t.getMatrix();

        camera.save();
        if (mReverse) {
            camera.translate(f, f, mDepthZ * interpolatedTime);
        } else {
            camera.translate(f, f, mDepthZ * (f - interpolatedTime));
        }
        camera.rotateY(degrees);
        camera.getMatrix(matrix);
        camera.restore();

        matrix.preTranslate(-centerX, -centerY);
        matrix.postTranslate(centerX, centerY);
    }
}
           

繼續閱讀