Android 自定义控件实现显示文字的功能

 更新时间:2016年11月11日 15:59:57   投稿:lqh  
这篇文章主要介绍了Android 自定义控件实现显示文字的功能的相关资料,需要的朋友可以参考下

Android 自定义控件实现显示文字的功能

自定义控件—–逐个显示文字

ONE Goal ,ONE Passion !

前言:

今天要实现的效果时.让我们的文字一个一个显示出来.上效果图吧:

实现原理:

1,拿到要显示的文字.

2,计算文字显示的速率
字体显示的速度 v = 总的字体长度 / 总的显示时间

3,将文字根据速率显示到控件上.

自定义View:

 public class printTextView extends TextView {

  /**
   * 字体显示出来的时间
   */
  private int DURATION = 8000;

  public printTextView(Context context) {
    this(context, null);
  }

  public printTextView(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
  }

  public printTextView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);


  }


  public void printString(String str) {


    if (str != null && str != "") {

      // 字符串的长度
      final int lenght = str.length();


      final char[] c = new char[str.length()];
      //将字符串转换成字符数组
      for (int i = 0; i < str.length(); i++) {
        c[i] = str.charAt(i);
      }


      ValueAnimator animator = ValueAnimator.ofFloat(0, 1);
      animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(ValueAnimator animation) {

          // 字体显示的速度 v = 总的字体长度 / 总的显示时间
          float v = (float) lenght / (float) DURATION;
          // 动画执行速度
          float fraction = (float) animation.getAnimatedValue();
          //动画不同阶段字体应该显示的个数
          int s = (int) (v * fraction * DURATION);

          setText(c, 0, s);

        }
      });
      animator.setDuration(DURATION);
      animator.start();
    }


  }


  }

跑起来:

 

 public class ScaleActivity extends AppCompatActivity {


  private printTextView print_text;
  String str = "我和你吻别,在无人的街.我和你吻别在狂乱的夜.这波给你103分," +
      "多一分宽容,多一分耐心,更重要的是多一分父爱.";

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_scale);
    initView();
  }




    print_text = (printTextView) findViewById(R.id.print_text);
    print_text.printString(str);
  }
  }

R.layout.activity_scale布局文件

 <?xml version="1.0" encoding="utf-8"?>
  <LinearLayout 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:layout_margin="20dp"
  android:orientation="vertical"
  tools:context="com.example.customview.activity.ScaleActivity">
 <com.example.customview.view.printTextView
    android:id="@+id/print_text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
 />
 </LinearLayout>

ok.我们的文字已经可以打印显示到屏幕了.

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

相关文章

  • Android7.0指纹服务FingerprintService实例介绍

    Android7.0指纹服务FingerprintService实例介绍

    这篇文章主要介绍了Android7.0指纹服务FingerprintService介绍,需要的朋友可以参考下
    2018-01-01
  • Android自定义视图中图片的处理

    Android自定义视图中图片的处理

    Android系统提供了ImageView显示普通的静态图片,也提供了AnimationDrawable来开发逐帧动画,还可通过Animation对普通图片使用补间动画。图形、图像处理不仅对Android系统的应用界面非常重要,而且Android系统上的益智类游戏、2D游戏都需要大量的图形、图像处理
    2022-07-07
  • Android中Socket的应用分析

    Android中Socket的应用分析

    这篇文章主要介绍了Android中Socket的应用,结合实例形式分析了Android中socket通信的实现技巧与相关注意事项,需要的朋友可以参考下
    2016-10-10
  • Android编程设计模式之策略模式详解

    Android编程设计模式之策略模式详解

    这篇文章主要介绍了Android编程设计模式之策略模式,结合实例形式详细分析了Android策略模式的概念、原理、实现方法及相关注意事项,需要的朋友可以参考下
    2017-12-12
  • 源码浅析Android中内存泄漏检测工具Leakcanary的使用

    源码浅析Android中内存泄漏检测工具Leakcanary的使用

    大名鼎鼎的 Leakcanary 想必作为 Android 开发都多多少少接触过,新版本的 Leakcanary 也用 Kotlin 重写了一遍,最近详细查看了下源码,就来和大家简单分享一下
    2023-04-04
  • Android系列之Intent传递对象的几种实例方法

    Android系列之Intent传递对象的几种实例方法

    Android系列之Intent传递对象的几种实例方法,需要的朋友可以参考一下
    2013-05-05
  • Android System fastboot 介绍和使用教程

    Android System fastboot 介绍和使用教程

    Fastboot是Android快速升级的一种方法,Fastboot的协议fastboot_protocol.txt在源码目录./bootable/bootloader/legacy下可以找到,本文给大家介绍Android System fastboot 介绍和使用教程,感兴趣的朋友一起看看吧
    2024-01-01
  • Android的VSYNC机制和UI刷新流程示例详解

    Android的VSYNC机制和UI刷新流程示例详解

    这篇文章主要为大家介绍了Android的VSYNC机制和UI刷新流程示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-12-12
  • Android DrawLayout结合ListView用法实例

    Android DrawLayout结合ListView用法实例

    这篇文章主要介绍了Android DrawLayout结合ListView用法实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-09-09
  • Android自定义TitleView标题开发实例

    Android自定义TitleView标题开发实例

    这篇文章主要介绍了Android自定义TitleView标题开发实例的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下
    2016-09-09

最新评论