Android利用Camera實現圖片的旋轉動畫

jopen 11年前發布 | 36K 次閱讀 Android Android開發 移動開發

MainActivity如下:

    package cc.testrotatephoto;  
    import android.os.Bundle;  
    import android.view.View;  
    import android.view.View.OnClickListener;  
    import android.view.animation.AccelerateInterpolator;  
    import android.view.animation.Animation;  
    import android.view.animation.Animation.AnimationListener;  
    import android.widget.Button;  
    import android.widget.ImageView;  
    import android.widget.RelativeLayout;  
    import android.app.Activity;  
    /** 
     * Demo描述: 
     * 利用Camera實現圖片的旋轉動畫 
     *  
     * 注意事項: 
     * 1 Rotate3dAnimation在APIDemo中的位置Views/Animation/3D Transition 
     * 2 待有空時候再仔細研究Rotate3dAnimation 
     *  
     * 參考資料: 
     * 1 http://blog.csdn.net/guolin_blog/article/details/10766017 
     * 2 http://blog.csdn.net/mapdigit/article/details/7804654 
     * 3 http://wang-peng1.iteye.com/blog/572886 
     *   Thank you very much 
     */  
    public class MainActivity extends Activity {  
        private RelativeLayout mRelativeLayout;  
        private ImageView mImageView;  
        private Button mButton;  
        private int count=0;  
        @Override  
        protected void onCreate(Bundle savedInstanceState) {  
            super.onCreate(savedInstanceState);  
            setContentView(R.layout.main);  
            init();  
        }  

        private void init(){  
            mRelativeLayout=(RelativeLayout) findViewById(R.id.relativeLayout);  
            mImageView=(ImageView) findViewById(R.id.imageView);  
            mButton=(Button) findViewById(R.id.button);  
            mButton.setOnClickListener(new OnClickListener() {  
                @Override  
                public void onClick(View view) {  
                    float centerX = mRelativeLayout.getWidth() / 2f;  
                    float centerY = mRelativeLayout.getHeight() / 2f;  
                    // 構建3D旋轉動畫對象,旋轉角度為0到90度  
                    Rotate3dAnimation rotation = new Rotate3dAnimation(0, 90, centerX, centerY,310.0f, true);  
                    // 動畫持續時間500毫秒  
                    rotation.setDuration(500);  
                    // 動畫完成后保持完成狀態  
                    rotation.setFillAfter(true);  
                    rotation.setInterpolator(new AccelerateInterpolator());  
                    // 設置動畫的監聽器  
                    if (count%2==0) {  
                        rotation.setAnimationListener(new RotateToTheSecondImageViewAnimationListener());  
                    }else{  
                        rotation.setAnimationListener(new RotateToTheFirstImageViewAnimationListener());  
                    }  
                    mRelativeLayout.startAnimation(rotation);  
                    count++;  
                }  
            });  
        }  


        class RotateToTheFirstImageViewAnimationListener implements AnimationListener {  
            @Override  
            public void onAnimationStart(Animation animation) {  
            }  

            @Override  
            public void onAnimationEnd(Animation animation) {  
                mImageView.setImageResource(R.drawable.c);  

                // 獲取布局的中心點位置,作為旋轉的中心點  
                float centerX = mRelativeLayout.getWidth() / 2f;  
                float centerY = mRelativeLayout.getHeight() / 2f;  

                // 構建3D旋轉動畫對象,旋轉角度為90到0度  
                Rotate3dAnimation rotation = new Rotate3dAnimation(90, 0, centerX, centerY,310.0f, false);  
                // 動畫持續時間500毫秒  
                rotation.setDuration(500);  
                // 動畫完成后保持完成狀態  
                rotation.setFillAfter(true);  
                rotation.setInterpolator(new AccelerateInterpolator());  
                mRelativeLayout.startAnimation(rotation);  
            }  

            @Override  
            public void onAnimationRepeat(Animation animation) {  
            }  

        }  


        class RotateToTheSecondImageViewAnimationListener implements AnimationListener {  

            @Override  
            public void onAnimationStart(Animation animation) {  
            }  


            @Override  
            public void onAnimationEnd(Animation animation) {  
                //動畫完成后展示另外的圖片  
                mImageView.setImageResource(R.drawable.b);  

                // 獲取布局的中心點位置,作為旋轉的中心點  
                float centerX = mRelativeLayout.getWidth() / 2f;  
                float centerY = mRelativeLayout.getHeight() / 2f;  
                // 構建3D旋轉動畫對象,旋轉角度為270到360度  
                Rotate3dAnimation rotation = new Rotate3dAnimation(270, 360, centerX, centerY,310.0f, false);  
                // 動畫持續時間500毫秒  
                rotation.setDuration(500);  
                // 動畫完成后保持完成狀態  
                rotation.setFillAfter(true);  
                rotation.setInterpolator(new AccelerateInterpolator());  
                mRelativeLayout.startAnimation(rotation);  
            }  

            @Override  
            public void onAnimationRepeat(Animation animation) {  
            }  

        }  

    }  
Rotate3dAnimation如下:
    package cc.testrotatephoto;  

    import android.graphics.Camera;  
    import android.graphics.Matrix;  
    import android.view.animation.Animation;  
    import android.view.animation.Transformation;  
    /** 
     * An animation that rotates the view on the Y axis between two specified angles. 
     * This animation also adds a translation on the Z axis (depth) to improve the effect. 
     */  
    public class Rotate3dAnimation extends Animation {  
        private final float mFromDegrees;  
        private final float mToDegrees;  
        private final float mCenterX;  
        private final float mCenterY;  
        private final float mDepthZ;  
        private final boolean isReverse;  
        private Camera mCamera;  

        /** 
         * Creates a new 3D rotation on the Y axis. The rotation is defined by its 
         * start angle and its end angle. Both angles are in degrees. The rotation 
         * is performed around a center point on the 2D space, definied by a pair 
         * of X and Y coordinates, called centerX and centerY. When the animation 
         * starts, a translation on the Z axis (depth) is performed. The length 
         * of the translation can be specified, as well as whether the translation 
         * should be reversed in time. 
         * 
         * @param fromDegrees the start angle of the 3D rotation 
         * @param toDegrees the end angle of the 3D rotation 
         * @param centerX the X center of the 3D rotation 
         * @param centerY the Y center of the 3D rotation 
         * @param reverse true if the translation should be reversed, false otherwise 
         */  
        public Rotate3dAnimation(float fromDegrees, float toDegrees,  
                float centerX, float centerY, float depthZ, boolean reverse) {  
            mFromDegrees = fromDegrees;  
            mToDegrees = toDegrees;  
            mCenterX = centerX;  
            mCenterY = centerY;  
            mDepthZ = depthZ;  
            isReverse = 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 (isReverse) {  
                camera.translate(0.0f, 0.0f, mDepthZ * interpolatedTime);  
            } else {  
                camera.translate(0.0f, 0.0f, mDepthZ * (1.0f - interpolatedTime));  
            }  
            camera.rotateY(degrees);  
            camera.getMatrix(matrix);  
            camera.restore();  

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

main.xml如下:

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

    <ImageView  
        android:id="@+id/imageView"  
        android:layout_width="fill_parent"  
        android:layout_height="350dip"  
        android:src="@drawable/c" />  

    <Button  
        android:id="@+id/button"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_below="@id/imageView"  
        android:layout_centerHorizontal="true"  
        android:layout_marginTop="30dip"  
        android:text="button" />  

</RelativeLayout> 

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!