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

相关文章

基于C++的UDP网络通信系统设计与实现详解

《基于C++的UDP网络通信系统设计与实现详解》在网络编程领域,UDP作为一种无连接的传输层协议,以其高效、低延迟的特性在实时性要求高的应用场景中占据重要地位,下面我们就来看看如何从零开始构建一个完整... 目录前言一、UDP服务器UdpServer.hpp1.1 基本框架设计1.2 初始化函数Init详解

Java中Map的五种遍历方式实现与对比

《Java中Map的五种遍历方式实现与对比》其实Map遍历藏着多种玩法,有的优雅简洁,有的性能拉满,今天咱们盘一盘这些进阶偏基础的遍历方式,告别重复又臃肿的代码,感兴趣的小伙伴可以了解下... 目录一、先搞懂:Map遍历的核心目标二、几种遍历方式的对比1. 传统EntrySet遍历(最通用)2. Lambd

springboot+redis实现订单过期(超时取消)功能的方法详解

《springboot+redis实现订单过期(超时取消)功能的方法详解》在SpringBoot中使用Redis实现订单过期(超时取消)功能,有多种成熟方案,本文为大家整理了几个详细方法,文中的示例代... 目录一、Redis键过期回调方案(推荐)1. 配置Redis监听器2. 监听键过期事件3. Redi

SpringBoot全局异常拦截与自定义错误页面实现过程解读

《SpringBoot全局异常拦截与自定义错误页面实现过程解读》本文介绍了SpringBoot中全局异常拦截与自定义错误页面的实现方法,包括异常的分类、SpringBoot默认异常处理机制、全局异常拦... 目录一、引言二、Spring Boot异常处理基础2.1 异常的分类2.2 Spring Boot默

基于SpringBoot实现分布式锁的三种方法

《基于SpringBoot实现分布式锁的三种方法》这篇文章主要为大家详细介绍了基于SpringBoot实现分布式锁的三种方法,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录一、基于Redis原生命令实现分布式锁1. 基础版Redis分布式锁2. 可重入锁实现二、使用Redisso

SpringBoo WebFlux+MongoDB实现非阻塞API过程

《SpringBooWebFlux+MongoDB实现非阻塞API过程》本文介绍了如何使用SpringBootWebFlux和MongoDB实现非阻塞API,通过响应式编程提高系统的吞吐量和响应性能... 目录一、引言二、响应式编程基础2.1 响应式编程概念2.2 响应式编程的优势2.3 响应式编程相关技术

C#实现将XML数据自动化地写入Excel文件

《C#实现将XML数据自动化地写入Excel文件》在现代企业级应用中,数据处理与报表生成是核心环节,本文将深入探讨如何利用C#和一款优秀的库,将XML数据自动化地写入Excel文件,有需要的小伙伴可以... 目录理解XML数据结构与Excel的对应关系引入高效工具:使用Spire.XLS for .NETC

Nginx更新SSL证书的实现步骤

《Nginx更新SSL证书的实现步骤》本文主要介绍了Nginx更新SSL证书的实现步骤,包括下载新证书、备份旧证书、配置新证书、验证配置及遇到问题时的解决方法,感兴趣的了解一下... 目录1 下载最新的SSL证书文件2 备份旧的SSL证书文件3 配置新证书4 验证配置5 遇到的http://www.cppc

Nginx之https证书配置实现

《Nginx之https证书配置实现》本文主要介绍了Nginx之https证书配置的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起... 目录背景介绍为什么不能部署在 IIS 或 NAT 设备上?具体实现证书获取nginx配置扩展结果验证

SpringBoot整合 Quartz实现定时推送实战指南

《SpringBoot整合Quartz实现定时推送实战指南》文章介绍了SpringBoot中使用Quartz动态定时任务和任务持久化实现多条不确定结束时间并提前N分钟推送的方案,本文结合实例代码给大... 目录前言一、Quartz 是什么?1、核心定位:解决什么问题?2、Quartz 核心组件二、使用步骤1