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

2024-05-30 06:38

本文主要是介绍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

这篇关于Android中实现如win7里边屏幕保护图案中三维文字的效果。的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1015841

相关文章

Linux下删除乱码文件和目录的实现方式

《Linux下删除乱码文件和目录的实现方式》:本文主要介绍Linux下删除乱码文件和目录的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录linux下删除乱码文件和目录方法1方法2总结Linux下删除乱码文件和目录方法1使用ls -i命令找到文件或目录

SpringBoot+EasyExcel实现自定义复杂样式导入导出

《SpringBoot+EasyExcel实现自定义复杂样式导入导出》这篇文章主要为大家详细介绍了SpringBoot如何结果EasyExcel实现自定义复杂样式导入导出功能,文中的示例代码讲解详细,... 目录安装处理自定义导出复杂场景1、列不固定,动态列2、动态下拉3、自定义锁定行/列,添加密码4、合并

mybatis执行insert返回id实现详解

《mybatis执行insert返回id实现详解》MyBatis插入操作默认返回受影响行数,需通过useGeneratedKeys+keyProperty或selectKey获取主键ID,确保主键为自... 目录 两种方式获取自增 ID:1. ​​useGeneratedKeys+keyProperty(推

Spring Boot集成Druid实现数据源管理与监控的详细步骤

《SpringBoot集成Druid实现数据源管理与监控的详细步骤》本文介绍如何在SpringBoot项目中集成Druid数据库连接池,包括环境搭建、Maven依赖配置、SpringBoot配置文件... 目录1. 引言1.1 环境准备1.2 Druid介绍2. 配置Druid连接池3. 查看Druid监控

Linux在线解压jar包的实现方式

《Linux在线解压jar包的实现方式》:本文主要介绍Linux在线解压jar包的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录linux在线解压jar包解压 jar包的步骤总结Linux在线解压jar包在 Centos 中解压 jar 包可以使用 u

Android kotlin中 Channel 和 Flow 的区别和选择使用场景分析

《Androidkotlin中Channel和Flow的区别和选择使用场景分析》Kotlin协程中,Flow是冷数据流,按需触发,适合响应式数据处理;Channel是热数据流,持续发送,支持... 目录一、基本概念界定FlowChannel二、核心特性对比数据生产触发条件生产与消费的关系背压处理机制生命周期

Android ClassLoader加载机制详解

《AndroidClassLoader加载机制详解》Android的ClassLoader负责加载.dex文件,基于双亲委派模型,支持热修复和插件化,需注意类冲突、内存泄漏和兼容性问题,本文给大家介... 目录一、ClassLoader概述1.1 类加载的基本概念1.2 android与Java Class

c++ 类成员变量默认初始值的实现

《c++类成员变量默认初始值的实现》本文主要介绍了c++类成员变量默认初始值,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录C++类成员变量初始化c++类的变量的初始化在C++中,如果使用类成员变量时未给定其初始值,那么它将被

Qt使用QSqlDatabase连接MySQL实现增删改查功能

《Qt使用QSqlDatabase连接MySQL实现增删改查功能》这篇文章主要为大家详细介绍了Qt如何使用QSqlDatabase连接MySQL实现增删改查功能,文中的示例代码讲解详细,感兴趣的小伙伴... 目录一、创建数据表二、连接mysql数据库三、封装成一个完整的轻量级 ORM 风格类3.1 表结构

基于Python实现一个图片拆分工具

《基于Python实现一个图片拆分工具》这篇文章主要为大家详细介绍了如何基于Python实现一个图片拆分工具,可以根据需要的行数和列数进行拆分,感兴趣的小伙伴可以跟随小编一起学习一下... 简单介绍先自己选择输入的图片,默认是输出到项目文件夹中,可以自己选择其他的文件夹,选择需要拆分的行数和列数,可以通过