利用Android中的TextView實現(xiàn)逐字顯示動畫
前言
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)文字逐個從右側(cè)飛入:
<?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)后效果還是很贊的,感興趣的小伙伴們自己動手實踐起來吧。如果有疑問可以留言討論。
- android實現(xiàn)上下滾動的TextView
- android TextView不用ScrollViewe也可以滾動的方法
- Android編程實現(xiàn)自動調(diào)整TextView字體大小以適應文字長度的方法
- Android開發(fā)技巧之在a標簽或TextView控件中單擊鏈接彈出Activity(自定義動作)
- Android開發(fā):TextView加入滾動條示例
- android動態(tài)布局之動態(tài)加入TextView和ListView的方法
- 基于Android中的 AutoCompleteTextView實現(xiàn)自動填充
- Android AutoCompleteTextView連接數(shù)據(jù)庫自動提示的方法(附demo源碼下載)
- Android編程實現(xiàn)TextView部分顏色變動的方法
- Android實現(xiàn)在TextView文字過長時省略部分或滾動顯示的方法
- Android TextView實現(xiàn)垂直滾動效果的方法
- Android編程實現(xiàn)TextView垂直自動滾動功能【附demo源碼下載】
相關文章
android文件操作——讀取assets和raw文件下的內(nèi)容
本篇文章主要介紹了android文件操作——讀取assets和raw文件下的內(nèi)容,并附簡單實例代碼,需要的朋友可以參考下。2016-10-10Android中okhttp3.4.1+retrofit2.1.0實現(xiàn)離線緩存
這篇文章主要介紹了Android中okhttp3.4.1結合retrofit2.1.0實現(xiàn)離線緩存,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-10-10Android操作SQLite數(shù)據(jù)庫(增、刪、改、查、分頁等)及ListView顯示數(shù)據(jù)的方法詳解
這篇文章主要介紹了Android操作SQLite數(shù)據(jù)庫(增、刪、改、查、分頁等)及ListView顯示數(shù)據(jù)的方法,結合實例形式詳細分析了Android操作SQLite數(shù)據(jù)庫及使用ListView顯示數(shù)據(jù)的相關技巧,需要的朋友可以參考下2016-02-02Android使用CountDownTimer模擬短信驗證倒計時
這篇文章主要為大家詳細介紹了Android使用CountDownTimer模擬短信驗證倒計時,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-07-07Android自定義控件實現(xiàn)優(yōu)雅的廣告輪播圖
這篇文章主要為大家詳細介紹了Android自定義控件實現(xiàn)優(yōu)雅的廣告輪播圖,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-03-03