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

所属分类: 软件编程 / Android 阅读数: 63
收藏 0 赞 0 分享

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.我们的文字已经可以打印显示到屏幕了.

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

更多精彩内容其他人还在看

Retrofit2日志拦截器的使用

这篇文章主要介绍了Retrofit2日志拦截器的使用,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android创建外部lib库及自定义View的图文教程

这篇文章主要给大家介绍了关于Android创建外部lib库及自定义View的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android分享微信小程序失败的一些事小结

这篇文章主要给大家介绍了关于Android分享微信小程序失败一些事,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android分享微信小程序技巧之图片优化

这篇文章主要给大家介绍了关于Android分享微信小程序技巧之图片优化的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android Viewpager实现无限循环轮播图

这篇文章主要为大家详细介绍了Android Viewpager实现无限循环轮播图,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android中的Bitmap序列化失败的解决方法

这篇文章主要介绍了Android中的Bitmap序列化失败的解决方法,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Android自定义通用标题栏CustomTitleBar

这篇文章主要为大家详细介绍了Android自定义通用标题栏CustomTitleBar,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android组合控件自定义标题栏

这篇文章主要为大家详细介绍了Android组合控件自定义标题栏,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android自定义复合控件实现通用标题栏

这篇文章主要为大家详细介绍了Android自定义复合控件实现通用标题栏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

ExpandableListView实现简单二级列表

这篇文章主要为大家详细介绍了ExpandableListView实现简单二级列表,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享
查看更多