利用Android中的TextView實現逐字顯示動畫_Android

來源:互聯網
上載者:User

前言

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;  /**  * Created by cchen on 2014/9/2.  */ 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) {       /**        * mode共有三種情況,取值分別為MeasureSpec.UNSPECIFIED, MeasureSpec.EXACTLY,        * MeasureSpec.AT_MOST。        *        *        * MeasureSpec.EXACTLY是精確尺寸,        * 當我們將控制項的layout_width或layout_height指定為具體數值時如andorid        * :layout_width="50dip",或者為FILL_PARENT是,都是控制項大小已經確定的情況,都是精確尺寸。        *        *        * MeasureSpec.AT_MOST是最大尺寸,        * 當控制項的layout_width或layout_height指定為WRAP_CONTENT時        * ,控制項大小一般隨著控制項的子空間或內容進行變化,此時控制項尺寸只要不超過父控制項允許的最大尺寸即可        * 。因此,此時的mode是AT_MOST,size給出了父控制項允許的最大尺寸。        *        *        * MeasureSpec.UNSPECIFIED是未指定尺寸,這種情況不多,一般都是父控制項是AdapterView,        * 通過measure方法傳入的模式。        */       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實現逐字動畫的全部內容,實現後效果還是很贊的,感興趣的小夥伴們自己動手實踐起來吧。如果有疑問可以留言討論。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.