国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

利用Android中的TextView實現(xiàn)逐字顯示動畫

2019-12-12 05:38:36
字體:
來源:轉載
供稿:網(wǎng)友

前言

Android的TextView只能設置整個TextView的動畫,而不能設置每個文字的動畫。即使是使用TextSwitcher,也很難實現(xiàn)我想要的效果。
 

所以選擇自定義一個。大體思路是:繼承ViewGroup,設置Text的時候,每個文字為一個TextView,每隔一個固定時間,啟動每個TextView的動畫。

 定義一個CTextView,繼承ViewGroup:

實現(xiàn)主要代碼:

public class CTextView extends ViewGroup { } 

向外提供一個方法setText(String text, final Animation animation, int duration),text為要顯示的字符串,animation為每個字符的動畫,duration為字符動畫的播放間隔。

該方法實現(xiàn)如下:

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完整實現(xiàn)如下:

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指定為具體數(shù)值時如andorid        * :layout_width="50dip",或者為FILL_PARENT是,都是控件大小已經(jīng)確定的情況,都是精確尺寸。        *        *        * MeasureSpec.AT_MOST是最大尺寸,        * 當控件的layout_width或layout_height指定為WRAP_CONTENT時        * ,控件大小一般隨著控件的子空間或內(nèi)容進行變化,此時控件尺寸只要不超過父控件允許的最大尺寸即可        * 。因此,此時的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中,調(diào)用CTextView的setText方法,傳入相關參數(shù)即可:

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);   } } 

其中的第二個參數(shù)為動畫,我想要的效果是從透明到不透明,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>  

如果想實現(xiàn)文字逐個從右側飛入:

<?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實現(xiàn)逐字動畫的全部內(nèi)容,實現(xiàn)后效果還是很贊的,感興趣的小伙伴們自己動手實踐起來吧。如果有疑問可以留言討論。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 诏安县| 建昌县| 赞皇县| 余姚市| 平湖市| 通海县| 望奎县| 商都县| 昌平区| 江永县| 阿拉善右旗| 汝阳县| 南宫市| 商洛市| 北流市| 常山县| 淮阳县| 陇南市| 平潭县| 大悟县| 广安市| 久治县| 郴州市| 舞钢市| 香港 | 宝兴县| 苍山县| 乌什县| 会泽县| 秀山| 天祝| 突泉县| 漳浦县| 合川市| 那曲县| 元朗区| 安西县| 边坝县| 富锦市| 柯坪县| 六枝特区|