Android中实现如win7里边屏幕保护图案中三维文字的效果。

具体实现如下:

activity_main.xml中定义一个用来显示文字的TextView:

<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">
     
    <TextView
        android:id="@+id/tv_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="无信号"
        android:textSize="80dp"
        android:layout_centerInParent="true"/>
       
</RelativeLayout>


MainActivity.java中定义主activity。
public class MainActivity extends Activity {    
    private TextView tv_text;  
    private String TAG = "MainActivity";
    Rotate3dAnimation rotateAnim = null;   
 
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        
        //隐藏标题栏
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        //隐藏状态栏
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_main);  
    
        tv_text = (TextView) findViewById(R.id.tv_text);
        if(null == tv_text){
            Log.i("MainActivity", "textview null...");
        }
        startAnimation();
    }  


    public void startAnimation() {  

        //用来获取textview的宽度和高度,否则宽度和高度都为0
        ViewTreeObserver vto = tv_text.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
            
            @Override
            public void onGlobalLayout() {
                // TODO Auto-generated method stub
                tv_text.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                Log.i("MainActivity", "onGlobalLayout txtNumber.width = " + tv_text.getWidth());
                Log.i("MainActivity", "onGlobalLayout txtNumber.height = " + tv_text.getHeight());
               
                rotateAnim = new Rotate3dAnimation(tv_text.getWidth()/2, tv_text.getHeight()/2, Rotate3dAnimation.ROTATE_CLOCKWIZE); 
              
                
                if (rotateAnim != null) {  
                    rotateAnim.setDuration(5000);
                    rotateAnim.setFillAfter(true);  
                    rotateAnim.setInterpolator(new LinearInterpolator());
                    rotateAnim.setRepeatCount(-1);
                    rotateAnim.setRepeatMode(Animation.RESTART);
                    tv_text.startAnimation(rotateAnim);  
                }  
                
            }
        });
    }  
}

//文字的翻转动画

public class Rotate3dAnimation extends Animation{
    
    /** 逆时针旋转*/  
    public static final boolean ROTATE_CLOCKWIZE = true;  
    /**动画顺时针旋转*/  
    public static final boolean ROTATE_ANTICOLCKWIZE = false;  
    /** Z轴上最大深度*/  
    public static final float DEPTH_Z = 310.0f;   
    /** 图片翻转类型*/  
    private final boolean type;  
    /** 翻转中心*/
    private final float centerX;  
    private final float centerY;  
    private Camera camera;  
    /** 用于监听动画进度*/  
    private InterpolatedTimeListener listener;  
 
    public Rotate3dAnimation(float cX, float cY, boolean type) {  //三个参数分别为翻转的中心位置和翻转方向
        centerX = cX;  
        centerY = cY;  
        this.type = type;  
    }  
 
    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;  
    }  
 
    //RotateAnimation.applyTransformation()第一个参数为动画的进度时间值,取值范围为[0.0f,1.0f],
    //第二个参数Transformation记录着动画某一帧中变形的原始数据。
    //该方法在动画的每一帧显示过程中都会被调用。
    protected void applyTransformation(float interpolatedTime, Transformation transformation) {  
 
        if (listener != null) {  
            listener.interpolatedTime(interpolatedTime);  
        }  
        float from = 0.0f, to = 0.0f;  
        if (type == ROTATE_CLOCKWIZE) {  
            from = 0.0f;  
            to = 180.0f;  
        } else if (type == ROTATE_ANTICOLCKWIZE) {  
            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.5f - Math.abs(interpolatedTime - 0.5f)) * DEPTH_Z;  
        final android.graphics.Matrix matrix = transformation.getMatrix();  
        camera.save();  //保存原来的状态
        camera.translate(0.0f, 0.0f, depth);  //平移一段距离
        camera.rotateY(degree);  //设置旋转的角度
        camera.getMatrix(matrix);  //取得变换矩阵
        camera.restore();   //操作完后,恢复到原来的状态
       
        //确保图片的翻转过程一直处于组件的中心点位置  
        //preTranslate是指在setScale前,平移,postTranslate是指在setScale后平移
       //以图片的中心点为旋转中心,如果不加这两句,就是以(0,0)点为旋转中心
        matrix.preTranslate(-centerX, -centerY);  
        matrix.postTranslate(centerX, centerY);   
    }  
 
    /** 动画进度监听器。 */  
    public static interface InterpolatedTimeListener {  
        public void interpolatedTime(float interpolatedTime);  
    }  
}


注:获取控件的宽度和高度,具体请参考:http://my.oschina.net/xiahuawuyu/blog/167949

        文字的翻转动画的实现,具体请参考:http://blog.csdn.net/sodino/article/details/7703980

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值