加入收藏 | 设为首页 | 会员中心 | 我要投稿 云计算网_泰州站长网 (http://www.0523zz.com/)- 视觉智能、AI应用、CDN、行业物联网、智能数字人!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

Android开发教程:文字翻转动画的达成

发布时间:2021-11-23 14:06:34 所属栏目:PHP教程 来源:互联网
导读:要求: 沿Y轴正方向看,数值减1时动画逆时针旋转,数值加1时动画顺时针旋转。 实现动画的具体细节见RotateAnimation.java。为方便查看动画旋转方向,可以将RotateAnimation.DEBUG值设置为true即可。 RotateAnimation参考自APIDemos的Rotate3DAnimation Rotat

要求:
沿Y轴正方向看,数值减1时动画逆时针旋转,数值加1时动画顺时针旋转。

实现动画的具体细节见"RotateAnimation.java"。为方便查看动画旋转方向,可以将RotateAnimation.DEBUG值设置为true即可。

         
RotateAnimation参考自APIDemos的Rotate3DAnimation

         RotateAnimation的构造函数需有三个参数,分别说明动画组件的中心点位置及旋转方向。

         RotateAnimation.initialize()将初始化动画组件及其父容器的宽高;通常亦可进行另外的初始化工作,本例中用于执行对camera进行实例化赋值。

         RotateAnimation.applyTransformation()第一个参数为动画的进度时间值,取值范围为[0.0f,1.0f],第二个参数Transformation记录着动画某一帧中变形的原始数据。该方法在动画的每一帧显示过程中都会被调用。

         
在翻转过程中,为了避免在动画下半部分时图像为镜面效果影响数字的阅读,应将翻转角度做180度的减法。代码为RotateAnimation.applyTransformation()中的:
 
if (overHalf) {  
    // 翻转过半的情况下,为保证数字仍为可读的文字而非镜面效果的文字,需翻转180度。   
    degree = degree - 180;  
}  
动画翻转到一半后,应更新数字内容。为了得知翻转进度,于RotateAnimation中设计一内部静态接口类"InterpolatedTimeListener",该接口只有一个方法"interpolatedTime(float interpolatedTime)"可以将动画进度传递给监听发起者。
 
Java代码如下,XML请根据效果图自行实现:

 
ActRotate.java
 
package lab.sodino.rotate;  
  
import lab.sodino.rotate.RotateAnimation.InterpolatedTimeListener;  
import Android.app.Activity;  
import android.os.Bundle;  
import android.util.Log;  
import android.view.View;  
import android.view.View.OnClickListener;  
import android.widget.Button;  
import android.widget.TextView;  
  
/**
 * @author Sodino E-mail:sodinoopen@hotmail.com
 * @version Time:2012-6-27 上午07:32:00
 */  
public class ActRotate extends Activity implements OnClickListener, InterpolatedTimeListener {  
    private Button btnIncrease, btnDecrease;  
    private TextView txtNumber;  
    private int number;  
    /** TextNumber是否允许显示最新的数字。 */  
    private boolean enableRefresh;  
  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main);  
  
        btnIncrease = (Button) findViewById(R.id.btnIncrease);  
        btnDecrease = (Button) findViewById(R.id.btnDecrease);  
        txtNumber = (TextView) findViewById(R.id.txtNumber);  
  
        btnIncrease.setOnClickListener(this);  
        btnDecrease.setOnClickListener(this);  
  
        number = 3;  
        txtNumber = (TextView) findViewById(R.id.txtNumber);  
        txtNumber.setText(Integer.toString(number));  
    }  
  
    public void onClick(View v) {  
        enableRefresh = true;  
        RotateAnimation rotateAnim = null;  
        float cX = txtNumber.getWidth() / 2.0f;  
        float cY = txtNumber.getHeight() / 2.0f;  
        if (v == btnDecrease) {  
            number--;  
            rotateAnim = new RotateAnimation(cX, cY, RotateAnimation.ROTATE_DECREASE);  
        } else if (v == btnIncrease) {  
            number++;  
            rotateAnim = new RotateAnimation(cX, cY, RotateAnimation.ROTATE_INCREASE);  
        }  
        if (rotateAnim != null) {  
            rotateAnim.setInterpolatedTimeListener(this);  
            rotateAnim.setFillAfter(true);  
            txtNumber.startAnimation(rotateAnim);  
        }  
    }  
  
    @Override  
    public void interpolatedTime(float interpolatedTime) {  
        // 监听到翻转进度过半时,更新txtNumber显示内容。   
        if (enableRefresh && interpolatedTime > 0.5f) {  
            txtNumber.setText(Integer.toString(number));  
            Log.d("ANDROID_LAB", "setNumber:" + number);  
            enableRefresh = false;  
        }  
    }  
}  
RotateAnimation.java
 
package lab.sodino.rotate;  
  
import android.graphics.Camera;  
import android.graphics.Matrix;  
import android.view.animation.Animation;  
import android.view.animation.Transformation;  
  
/**
 * @author Sodino E-mail:sodinoopen@hotmail.com
 * @version Time:2012-6-27 上午07:32:00
 */  
public class RotateAnimation extends Animation {  
    /** 值为true时可明确查看动画的旋转方向。 */  
    public static final boolean DEBUG = false;  
    /** 沿Y轴正方向看,数值减1时动画逆时针旋转。 */  
    public static final boolean ROTATE_DECREASE = true;  
    /** 沿Y轴正方向看,数值减1时动画顺时针旋转。 */  
    public static final boolean ROTATE_INCREASE = false;  
    /** Z轴上最大深度。 */  
    public static final float DEPTH_Z = 310.0f;  
    /** 动画显示时长。 */  
    public static final long DURATION = 800l;  
    /** 图片翻转类型。 */  
    private final boolean type;  
    private final float centerX;  
    private final float centerY;  
    private Camera camera;  
    /** 用于监听动画进度。当值过半时需更新txtNumber的内容。 */  
    private InterpolatedTimeListener listener;  
  
    public RotateAnimation(float cX, float cY, boolean type) {  
        centerX = cX;  
        centerY = cY;  
        this.type = type;  
        setDuration(DURATION);  
    }  
  
    public void initialize(int width, int height, int parentWidth, int parentHeight) {  
        // 在构造函数之后、getTransformation()之前调用本方法。   
        super.initialize(width, height, parentWidth, parentHeight);  
        camera = new Camera();  
    }  
  
    public void setInterpolatedTimeListener(InterpolatedTimeListener listener) {  
        this.listener = listener;  
    }  
  
    protected void applyTransformation(float interpolatedTime, Transformation transformation) {  
        // interpolatedTime:动画进度值,范围为[0.0f,10.f]   
        if (listener != null) {  
            listener.interpolatedTime(interpolatedTime);  
        }  
        float from = 0.0f, to = 0.0f;  
        if (type == ROTATE_DECREASE) {  
            from = 0.0f;  
            to = 180.0f;  
        } else if (type == ROTATE_INCREASE) {  
            from = 360.0f;  
            to = 180.0f;  
        }  
        float degree = from + (to - from) * interpolatedTime;  
        boolean overHalf = (interpolatedTime > 0.5f);  
        if (overHalf) {  
            // 翻转过半的情况下,为保证数字仍为可读的文字而非镜面效果的文字,需翻转180度。   
            degree = degree - 180;  
        }  
        // float depth = 0.0f;   
        float depth = (0.5f - Math.abs(interpolatedTime - 0.5f)) * DEPTH_Z;  
        final Matrix matrix = transformation.getMatrix();  
        camera.save();  
        camera.translate(0.0f, 0.0f, depth);  
        camera.rotateY(degree);  
        camera.getMatrix(matrix);  
        camera.restore();  
        if (DEBUG) {  
            if (overHalf) {  
                matrix.preTranslate(-centerX * 2, -centerY);  
                matrix.postTranslate(centerX * 2, centerY);  
            }  
        } else {  
            //确保图片的翻转过程一直处于组件的中心点位置   
            matrix.preTranslate(-centerX, -centerY);  
            matrix.postTranslate(centerX, centerY);  
        }  
    }  
  
    /** 动画进度监听器。 */  
    public static interface InterpolatedTimeListener {  
        public void interpolatedTime(float interpolatedTime);  
    }  
}  
 

(编辑:云计算网_泰州站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读