iis服务器助手广告广告
返回顶部
首页 > 资讯 > 移动开发 >利用Android中的TextView实现逐字显示动画
  • 556
分享到

利用Android中的TextView实现逐字显示动画

动画Android 2022-06-06 07:06:02 556人浏览 泡泡鱼
摘要

前言 Android的TextView只能设置整个TextView的动画,而不能设置每个文字的动画。即使是使用TextSwitcher,也很难实现我想要的效果。  

前言

Android的TextView只能设置整个TextView的动画,而不能设置每个文字的动画。即使是使用TextSwitcher,也很难实现我想要的效果。
 

所以选择自定义一个。大体思路是:继承ViewGroup,设置Text的时候,每个文字为一个TextView,每隔一个固定时间,启动每个TextView的动画。

 定义一个CTextView,继承ViewGroup:

实现主要代码:


public class CTextView extends ViewGroup { 
} 

向外提供一个方法

setText(String text, final Animation animation, int duration)
,text为要显示的字符串,animation为每个字符的动画,duration为字符动画的播放间隔。

该方法实现如下:


public void setText(String text, final Animation animation, int duration) { 
  int time = 0; 
  if(text != null && !text.isEmpty()) { 
    char[] characters = text.toCharArray(); 
    for(char c : characters) { 
      final TextView t = new TextView(context); 
      //遍历传入的字符串的每个字符,生成一个TextView,并设置它的动画 
      t.setText(String.valueOf(c)); 
      t.setTextSize(28); 
      Handler h = new Handler(); 
      //每隔duration时间,播放下一个TextView的动画 
      h.postDelayed(new Runnable() { 
        @Override 
        public void run() { 
          addView(t); 
          t.setAnimation(animation); 
        } 
      }, time); 
      time += duration; 
    } 
  } 
} 

CTextView完整实现如下:


import android.content.Context; 
import android.os.Handler; 
import android.util.AttributeSet; 
import android.view.View; 
import android.view.ViewGroup; 
import android.view.animation.Animation; 
import android.widget.TextView; 
 
public class CTextView extends ViewGroup { 
  private Context context; 
  public CTextView(Context context) { 
    super(context); 
    this.context = context; 
  } 
  public CTextView(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    this.context = context; 
  } 
  public CTextView(Context context, AttributeSet attrs, int defStyle) { 
    super(context, attrs, defStyle); 
    this.context = context; 
  } 
  public void setText(String text, final Animation animation, int duration) { 
    int time = 0; 
    if(text != null && !text.isEmpty()) { 
      char[] characters = text.toCharArray(); 
      for(char c : characters) { 
        final TextView t = new TextView(context); 
        //遍历传入的字符串的每个字符,生成一个TextView,并设置它的动画 
        t.setText(String.valueOf(c)); 
        t.setTextSize(28); 
        Handler h = new Handler(); 
        //每隔duration时间,播放下一个TextView的动画 
        h.postDelayed(new Runnable() { 
          @Override 
          public void run() { 
            addView(t); 
            t.setAnimation(animation); 
          } 
        }, time); 
        time += duration; 
      } 
    } 
  } 
  @Override 
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
    int measureWidth = measureWidth(widthMeasureSpec); 
    int measureHeight = measureHeight(heightMeasureSpec); 
    // 计算自定义的ViewGroup中所有子控件的大小 
    measureChildren(widthMeasureSpec, heightMeasureSpec); 
    // 设置自定义的控件MyViewGroup的大小 
    setMeasuredDimension(measureWidth, measureHeight); 
  } 
  @Override 
  protected void onLayout(boolean changed, int l, int t, int r, int b) { 
    int childLeft = 0; 
    // 遍历所有子视图 
    int childCount = getChildCount(); 
    for (int i = 0; i < childCount; i++) { 
      View childView = getChildAt(i); 
      // 获取在onMeasure中计算的视图尺寸 
      int measureHeight = childView.getMeasuredHeight(); 
      int measuredWidth = childView.getMeasuredWidth(); 
      //将他们横向排列 
      childView.layout(childLeft, 0, childLeft + measuredWidth, measureHeight); 
      childLeft += measuredWidth; 
    } 
  } 
  private int measureWidth(int pWidthMeasureSpec) { 
    int result = 0; 
    int widthMode = MeasureSpec.getMode(pWidthMeasureSpec);// 得到模式 
    int widthSize = MeasureSpec.getSize(pWidthMeasureSpec);// 得到尺寸 
    switch (widthMode) { 
       
      case MeasureSpec.AT_MOST: 
      case MeasureSpec.EXACTLY: 
        result = widthSize; 
        break; 
    } 
    return result; 
  } 
  private int measureHeight(int pHeightMeasureSpec) { 
    int result = 0; 
    int heightMode = MeasureSpec.getMode(pHeightMeasureSpec); 
    int heightSize = MeasureSpec.getSize(pHeightMeasureSpec); 
    switch (heightMode) { 
      case MeasureSpec.AT_MOST: 
      case MeasureSpec.EXACTLY: 
        result = heightSize; 
        break; 
    } 
    return result; 
  } 
} 

然后在布局文件中使用该自定义组件:


<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:orientation="vertical" 
  tools:context=".networkTestActivity"> 
  <com.network.cchen.network.CTextView 
    android:id="@+id/cTextView" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
  </com.network.cchen.network.CTextView> 
</LinearLayout> 

在Activity中,调用CTextView的

setText
方法,传入相关参数即可:


import android.app.Activity; 
import android.os.Bundle; 
import android.view.animation.AnimationUtils; 
public class TestActivity extends Activity { 
  @Override 
  public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_network_test); 
    CTextView cTextView = (CTextView) findViewById(R.id.cTextView); 
    cTextView.setText("Hello world", AnimationUtils.loadAnimation(this, R.anim.myanim), 300); 
  } 
} 

其中的第二个参数为动画,我想要的效果是从透明到不透明,myanim.xml:


<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android"> 
  <alpha 
    android:duration="1000" 
    android:fromAlpha="0.0" 
    android:toAlpha="1.0" /> 
</set>  

如果想实现文字逐个从右侧飞入:


<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android"> 
  <translate 
    android:duration="1000" 
    android:fillAfter="true" 
    android:fromXDelta="50%p" 
    android:interpolator="@android:anim/anticipate_interpolator" 
    android:toXDelta="0" /> 
</set> 

总结

以上就是利用Android中的TextView实现逐字动画的全部内容,实现后效果还是很赞的,感兴趣的小伙伴们自己动手实践起来吧。如果有疑问可以留言讨论。

您可能感兴趣的文章:android实现上下滚动的TextViewandroid TextView不用ScrollViewe也可以滚动的方法Android编程实现自动调整TextView字体大小以适应文字长度的方法Android开发技巧之在a标签或TextView控件中单击链接弹出Activity(自定义动作)Android开发:TextView加入滚动条示例android动态布局之动态加入TextView和ListView的方法基于Android中的 AutoCompleteTextView实现自动填充Android AutoCompleteTextView连接数据库自动提示的方法(附demo源码下载)Android编程实现TextView部分颜色变动的方法Android实现在TextView文字过长时省略部分或滚动显示的方法Android TextView实现垂直滚动效果的方法Android编程实现TextView垂直自动滚动功能【附demo源码下载】


--结束END--

本文标题: 利用Android中的TextView实现逐字显示动画

本文链接: https://www.lsjlt.com/news/24247.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • 怎么在Android中利用TextView实现一个数字滚动动画
    怎么在Android中利用TextView实现一个数字滚动动画?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。NumberRollingView是一个自定义的自带数字滚动动画的T...
    99+
    2023-05-31
    android textview 动动
  • Android中怎么利用TextView显示部分文字高亮
    这篇文章将为大家详细讲解有关Android中怎么利用TextView显示部分文字高亮,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。Android  TextView中部分文字高亮显...
    99+
    2023-05-31
    android textview
  • Android应用中怎么将文字逐字显示
    今天就跟大家聊聊有关Android应用中怎么将文字逐字显示,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。可以采用自定义TextView的方式去实现,也可才用定时更新文字显示,思路是让...
    99+
    2023-05-31
    android roi
  • css3中怎么利用animation实现逐帧动画效果
    css3中怎么利用animation实现逐帧动画效果,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。常见用法::hover{&n...
    99+
    2024-04-02
  • CSS3 中怎么利用animation实现逐帧动画效果
    本篇文章给大家分享的是有关CSS3 中怎么利用animation实现逐帧动画效果,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。animatio...
    99+
    2024-04-02
  • Android实现数字跳动效果的TextView方法示例
    前言本文介绍的是Android如何实现数字跳动效果的TextView,主要运用了DancingNumberView,DancingNumberView是一个用于跳动显示文本中数字的控件,继承自TextView,这种控件一般用于显示金额等对用...
    99+
    2023-05-31
    android textview 数字跳动
  • Android中怎么利用Xfermode实现动态文字加载动画
    这篇文章将为大家详细讲解有关Android中怎么利用Xfermode实现动态文字加载动画,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。第一步:我们要熟悉一下这个图16个图形结果,其实现在有1...
    99+
    2023-05-30
    android
  • 在Android项目中利用TextView实现一个自动滚动功能
    在Android项目中利用TextView实现一个自动滚动功能?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。在做android 应用的开发的时候,横向滚动或者要...
    99+
    2023-05-31
    android textview roi
  • js实现一个逐步递增的数字动画
    目录背景实现类似滚轮的效果,容器固定,数字向上滚动利用两个元素实现滚动利用H5的requestAnimationFrame()API实现数字逐步递增的动画效果计时器对比request...
    99+
    2024-04-02
  • 如何使用CSS制作文字实现逐帧动画
    这篇文章主要介绍了如何使用CSS制作文字实现逐帧动画,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。方法步骤HTML部分1、创建html定义一...
    99+
    2024-04-02
  • Android利用ScaleTransition实现吹气球动画
    目录前言ScaleTransition 介绍应用总结前言 这是最后一篇介绍如何使用基本动画组件的文章,我们继续 Transition 的动画,本篇来介绍 ...
    99+
    2024-04-02
  • Android开发TextView内的文字实现自动换行
    目录前言Layout构造方法:拓展具体实现前言 相信这个方法Canvas.drawText大家一定不陌生,TextView就是使用它将文字绘制出来。可是这个方法并没有文字换行的功能,...
    99+
    2024-04-02
  • android中怎么利用TextView实现跑马灯效果
    这篇文章给大家介绍android中怎么利用TextView实现跑马灯效果,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。一、要点设置四个属性android:singleLine="true"andro...
    99+
    2023-05-31
    android textview
  • 怎么在Android中利用TextView实现词组高亮
    这篇文章将为大家详细讲解有关怎么在Android中利用TextView实现词组高亮,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。HighlightTextViewAndroid文本高亮控件,...
    99+
    2023-05-30
    android textview
  • 利用css3实现鼠标经过动画显示详情特效
    本篇内容介绍了“利用css3实现鼠标经过动画显示详情特效”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!  ...
    99+
    2024-04-02
  • Android如何实现带动画效果的可点击展开TextView
    这篇文章将为大家详细讲解有关Android如何实现带动画效果的可点击展开TextView,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。效果图: 收起(默认)效果:点击展开后的效果:源码: 布局:<x...
    99+
    2023-05-30
    android textview
  • Android补间动画的实现示例
    目录1.补间动画的分类和Interpolator2.各种动画的详细讲解3.写个例子来体验下帧动画是通过连续播放图片来模拟动画效果,而补间动画开发者只需指定动画开始,以及动画结束&qu...
    99+
    2023-05-17
    Android 补间动画
  • 如何在Android中利用TextView实现自定义竖排
    这篇文章给大家介绍如何在Android中利用TextView实现自定义竖排,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。测试用的Activity。public class MainActivity extends Ac...
    99+
    2023-05-31
    android textview roi
  • 怎么在Android中利用TextView实现跑马灯效果
    怎么在Android中利用TextView实现跑马灯效果?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。MainActivitypackage com.exampl...
    99+
    2023-06-15
  • CSS3如何实现灯光照射显示文字动画
    这篇文章主要介绍了CSS3如何实现灯光照射显示文字动画,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。<!DOCTYPE html>...
    99+
    2024-04-02
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作