硬币反转动画实现

来源:互联网 发布:远程桌面连接端口修改 编辑:程序博客网 时间:2024/05/21 06:02
package com.duguang.baseanimation.ui.splash;import android.view.View;import android.view.View.OnClickListener;import android.view.ViewGroup;import android.view.animation.AccelerateInterpolator;import android.view.animation.Animation;import android.view.animation.DecelerateInterpolator;import android.widget.RelativeLayout;import com.duguang.baseanimation.R;import com.duguang.baseanimation.ui.anim.RotateAnimation;import com.duguang.baseanimation.ui.base.BaseActivity;/** * 页面翻转 * @author duguang * */public class RotateActivity extends BaseActivity implements OnClickListener {   private RelativeLayout rl_layout01;   private RelativeLayout rl_layout02;   private ViewGroup mContainer;   @Override   public void setView() {      setContentView(R.layout.activity_splash_rotate);   }   @Override   public void initView() {            mContainer = (ViewGroup) findViewById(R.id.container);      mContainer.setPersistentDrawingCache(ViewGroup.PERSISTENT_ANIMATION_CACHE);              rl_layout01 = (RelativeLayout) findViewById(R.id.rl_layout01);      rl_layout02 = (RelativeLayout) findViewById(R.id.rl_layout02);   }   @Override   public void setListener() {      rl_layout01.setOnClickListener(this);      rl_layout02.setOnClickListener(this);   }   @Override   public void onClick(View v) {      switch (v.getId()) {      case R.id.rl_layout01:         applyRotation(0,0,90);         break;      case R.id.rl_layout02:         applyRotation(-1, 180, 90);         break;      default:         break;      }         }      /**     * Setup a new 3D rotation on the container view.     *     * @param position the item that was clicked to show a picture, or -1 to show the list     * @param start the start angle at which the rotation must begin     * @param end the end angle of the rotation     */    private void applyRotation(int position, float start, float end) {        // Find the center of the container        final float centerX = mContainer.getWidth() / 2.0f;        final float centerY = mContainer.getHeight() / 2.0f;        // Create a new 3D rotation with the supplied parameter        // The animation listener is used to trigger the next animation        final RotateAnimation rotation =                new RotateAnimation(start, end, centerX, centerY, 310.0f, true);        rotation.setDuration(500);        rotation.setFillAfter(true);        rotation.setInterpolator(new AccelerateInterpolator());        rotation.setAnimationListener(new DisplayNextView(position));        mContainer.startAnimation(rotation);    }        /**     * This class listens for the end of the first half of the animation.     * It then posts a new action that effectively swaps the views when the container     * is rotated 90 degrees and thus invisible.     */    private final class DisplayNextView implements Animation.AnimationListener {        private final int mPosition;        private DisplayNextView(int position) {            mPosition = position;        }        public void onAnimationStart(Animation animation) {        }        public void onAnimationEnd(Animation animation) {            mContainer.post(new SwapViews(mPosition));        }        public void onAnimationRepeat(Animation animation) {        }    }    /**     * This class is responsible for swapping the views and start the second     * half of the animation.     */    private final class SwapViews implements Runnable {        private final int mPosition;        public SwapViews(int position) {            mPosition = position;        }        public void run() {            final float centerX = mContainer.getWidth() / 2.0f;            final float centerY = mContainer.getHeight() / 2.0f;            RotateAnimation rotation;                        if (mPosition > -1) {               rl_layout01.setVisibility(View.GONE);               rl_layout02.setVisibility(View.VISIBLE);                rotation = new RotateAnimation(90,180, centerX, centerY, 310.0f, false);            } else {               rl_layout02.setVisibility(View.GONE);               rl_layout01.setVisibility(View.VISIBLE);                rotation = new RotateAnimation(90, 0, centerX, centerY, 310.0f, false);            }            rotation.setDuration(500);            rotation.setFillAfter(true);            rotation.setInterpolator(new DecelerateInterpolator());            mContainer.startAnimation(rotation);        }    }   

}

使用到的布局为**************************

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="@android:color/white"    android:gravity="center" >    <FrameLayout        android:id="@+id/container"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:gravity="center" >        <RelativeLayout            android:id="@+id/rl_layout01"            android:layout_width="match_parent"            android:layout_height="wrap_content"             android:gravity="center">            <ImageView                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_gravity="center"                android:scaleType="matrix"                android:src="@drawable/bg1" >            </ImageView>        </RelativeLayout>        <RelativeLayout            android:id="@+id/rl_layout02"            android:layout_width="match_parent"            android:layout_height="wrap_content"             android:gravity="center"            android:visibility="gone">            <ImageView                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_gravity="center"                android:scaleType="matrix"                android:src="@drawable/bg2" >            </ImageView>        </RelativeLayout>    </FrameLayout></RelativeLayout>







使用到的ratationanimation为

package com.duguang.baseanimation.ui.anim;import android.view.animation.Animation;import android.view.animation.Transformation;import android.graphics.Camera;import android.graphics.Matrix;/** * 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 RotateAnimation 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 mReverse;    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 RotateAnimation(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(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);    }}
























0 0
原创粉丝点击