Android 自定義控件實(shí)現(xiàn)顯示文字的功能
Android 自定義控件實(shí)現(xiàn)顯示文字的功能
自定義控件—–逐個(gè)顯示文字
ONE Goal ,ONE Passion !
前言:
今天要實(shí)現(xiàn)的效果時(shí).讓我們的文字一個(gè)一個(gè)顯示出來.上效果圖吧:
實(shí)現(xiàn)原理:
1,拿到要顯示的文字.
2,計(jì)算文字顯示的速率
字體顯示的速度 v = 總的字體長度 / 總的顯示時(shí)間
3,將文字根據(jù)速率顯示到控件上.
自定義View:
public class printTextView extends TextView { /** * 字體顯示出來的時(shí)間 */ 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()]; //將字符串轉(zhuǎn)換成字符數(shù)組 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 = 總的字體長度 / 總的顯示時(shí)間 float v = (float) lenght / (float) DURATION; // 動畫執(zhí)行速度 float fraction = (float) animation.getAnimatedValue(); //動畫不同階段字體應(yīng)該顯示的個(gè)數(shù) 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.我們的文字已經(jīng)可以打印顯示到屏幕了.
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
在Android線程池里運(yùn)行代碼任務(wù)實(shí)例
這篇文章主要介紹了在Android線程池里運(yùn)行代碼任務(wù)實(shí)例,同時(shí)介紹了線程池中停止任務(wù)的方法,需要的朋友可以參考下2014-06-06簡單實(shí)用的Android studio 調(diào)試技巧
這篇文章主要介紹了簡單實(shí)用的Android studio 調(diào)試技巧的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-07-07Android開發(fā)中使用Volley庫發(fā)送HTTP請求的實(shí)例教程
這篇文章主要介紹了Android開發(fā)中使用Volley庫發(fā)送HTTP請求的實(shí)例教程,包括創(chuàng)建Volley單例的基本知識與取消Request請求的技巧等,需要的朋友可以參考下2016-05-05Android實(shí)現(xiàn)拍照、錄像、錄音代碼范例
這篇文章主要介紹了Android實(shí)現(xiàn)拍照、錄像、錄音代碼的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2016-10-10Kotlin by lazy關(guān)鍵字深入探究實(shí)現(xiàn)原理
這篇文章主要介紹了by lazy,在kotlin中使用是很常見的,用于實(shí)現(xiàn)懶加載某個(gè)數(shù)據(jù)。而這兩個(gè)單詞不是一體的,其中by是kotlin中的關(guān)鍵字,用于實(shí)現(xiàn)委托;lazy是一個(gè)方法,他的返回值是委托的具體對象2022-11-11Android之仿美團(tuán)加載數(shù)據(jù)幀動畫
本文主要介紹了Android仿美團(tuán)加載數(shù)據(jù)幀動畫的實(shí)例方法。具有很好的參考價(jià)值。下面跟著小編一起來看下吧2017-04-04詳解ListView中多種item的實(shí)現(xiàn)方式
這篇文章主要給大家介紹了關(guān)于ListView中多種item的實(shí)現(xiàn)方式,文中通過示例代碼介紹的很詳細(xì),有需要的朋友們可以參考借鑒,下面來一起看看吧。2016-12-12