Android 3D旋转动画

来源:互联网 发布:淘宝客服欢迎语 编辑:程序博客网 时间:2024/04/28 06:28

 

/**

 *
 * 转载请标明出处:http://blog.csdn.net/u013598111/article/details/50176855

   @author:【JunTao_sun】
 *
 *
*/




设置了 加速度和减速度插值器的效果,如上图显示。

重点的代码注释了。


<span style="font-size:18px;">public class MainActivity extends Activity {private ImageView image1;private Button btn;private ImageView image;private RotateAnimation rote;   //记录那一个View 是显示状态即 开始旋转的viewImageView mStartAnimView = null;int mDuration = 1000;float mCenterX = 0.0f;float mCenterY = 0.0f;float mDepthZ = 0.0f;int mIndex = 0;private View container;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);btn = (Button) this.findViewById(R.id.btn);image = (ImageView) this.findViewById(R.id.id_image);image1 = (ImageView) this.findViewById(R.id.id_image1);container = this.findViewById(R.id.container);InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);imm.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);mStartAnimView = image; btn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {                 //得到图片的中心点                mCenterX = image.getWidth() / 2;                  mCenterY = image.getHeight() / 2;                                                                      applyRotation(mStartAnimView);  }});}/** * 开启自定义旋转动画 * @param animView  */private void applyRotation(View animView) {//得到图片的中心点float centerX = mCenterX;          float centerY = mCenterY;         roAnimotion rotation = new roAnimotion(0, 90, mCenterX, mCenterY,100.0f, true);rotation.setDuration(mDuration);//设置动画后位子不变rotation.setFillAfter(true);//设置插值器rotation.setInterpolator(new AccelerateInterpolator());//设置动画监听rotation.setAnimationListener(new ShowNextView());animView.startAnimation(rotation);} private final class ShowNextView implements Animation.AnimationListener {public void onAnimationStart(Animation animation) {}public void onAnimationEnd(Animation animation) {            //将线程post到UI线程的  线程队列执行 刷新动画container.post(new SwapViews());}public void onAnimationRepeat(Animation animation) {}}private final class SwapViews implements Runnable {@Overridepublic void run() {image.setVisibility(View.GONE);image1.setVisibility(View.GONE);mIndex++;//控制-90的ImageView 赋给mStartAnimViewif (0 == mIndex % 2) {mStartAnimView = image;} else {mStartAnimView = image1;}                        //显示该ImageViewmStartAnimView.setVisibility(View.VISIBLE);mStartAnimView.requestFocus();//设置自定义3D旋转动画roAnimotion ro = new roAnimotion(-90, 0, mCenterX, mCenterY, 100.0f, false);ro.setDuration(3000);ro.setFillAfter(true);ro.setInterpolator(new DecelerateInterpolator());mStartAnimView.startAnimation(ro);}}}</span>


下边是自定义3D动画类:

主要复写两个方法 一个是初始化 initialize 方法,

另一个 是控件 动画期间 在每次 显示下一帧的时候 执行的   applyTransformation方法。

<span style="font-size:18px;">package com.example.rotote;import android.graphics.Camera;import android.graphics.Matrix;import android.view.animation.Animation;import android.view.animation.Transformation;public class roAnimotion extends Animation {//Camera 类  可以控制3个轴 xyz显示private Camera mCamera;//开始角度private float mFromDegrees;//选择到目标角度private float mToDegrees;//控件中心Xprivate float mCenterX;//控件中心点Yprivate float mCenterY;//Z轴的缩放 private float mDepthZ;//标记 Z轴是否大到小 还是小到大 变化private boolean mReverse;public roAnimotion(int fromDegrees, int toDegrees, float mCenterX2,float mCenterY2, float mDepthZ2, boolean reverse) { mFromDegrees = fromDegrees;          mToDegrees = toDegrees;          mCenterX = mCenterX2;          mCenterY = mCenterY2;          mDepthZ = mDepthZ2;          mReverse = reverse;}//动画初始化@Overridepublic void initialize(int width, int height, int parentWidth,int parentHeight) {// TODO Auto-generated method stubsuper.initialize(width, height, parentWidth, parentHeight);mCamera = new Camera();}/*  * interpolatedTime的值是 0~1  *  */@Overrideprotected void applyTransformation(float interpolatedTime, Transformation t) {final float fromDegrees = mFromDegrees; //得到角度        float degrees = fromDegrees + ((mToDegrees - fromDegrees) * interpolatedTime);            final Camera camera = mCamera;           //得到Transformation 的matrix 记录没一次变化的数值        final Matrix matrix = t.getMatrix();            camera.save();          //Z轴的变化        if (mReverse) {              camera.translate(0.0f, 0.0f, mDepthZ * interpolatedTime);          } else {              camera.translate(0.0f, 0.0f, mDepthZ * (1.0f - interpolatedTime));          }          //旋转是从控件的0 0点旋转        camera.rotateY(degrees);          camera.getMatrix(matrix);                camera.restore();          //动画前移动        matrix.preTranslate(-mCenterX, -mCenterY);          //动画后移动        matrix.postTranslate(mCenterX, mCenterY); }}</span>

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context=".MainActivity" >    <FrameLayout        android:id="@+id/container"        android:layout_width="match_parent"        android:layout_height="wrap_content" >        <ImageView            android:id="@+id/id_image"            android:layout_width="200dp"            android:layout_height="200dp"            android:src="@drawable/ic_4" />        <ImageView            android:id="@+id/id_image1"            android:layout_width="200dp"            android:layout_height="200dp"            android:src="@drawable/ic_4"            android:visibility="gone" />    </FrameLayout>    <Button        android:id="@+id/btn"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_below="@+id/frame"        android:layout_marginTop="200dp"        android:text="点击" /></RelativeLayout>






0 0
原创粉丝点击