Android實現(xiàn)文字消除效果
今天和大家分享一個如何從右到左消除文本的動畫。
先看效果圖:
由于項目和語音識別相關(guān),有時候人在不經(jīng)意間交流的無效音頻會被識別出來,并展示于界面,為了美觀,客戶要求我們將這些無效的識別文本用一個從右到左的動畫給清除,于是便有了下述的技術(shù)實現(xiàn)。
嗯,效果做完后發(fā)現(xiàn)原理及其簡單,僅此記錄一下。
1、layout文件先在這兒貼一下
<?xml version='1.0' encoding='utf-8'?><LinearLayout xmlns:android='http://schemas.android.com/apk/res/android' android:layout_width='match_parent' android:layout_height='match_parent' android:orientation='vertical'> <TextViewandroid:layout_width='wrap_content'android:layout_height='44dp'android:text='百日不到處,青春恰自來。苔花如米小,也學(xué)牡丹開。'android:ellipsize='none'android:singleLine='true'android:background='#ff00ff'android:layout_marginTop='10dp'android: /> <Buttonandroid:layout_width='match_parent'android:layout_height='wrap_content'android:id='@+id/btn_click'android:text='點擊清除'/> <Buttonandroid:layout_width='match_parent'android:layout_height='wrap_content'android: android:text='點擊恢復(fù)'/></LinearLayout>
btn_click1是為了演示方便而設(shè)計的,可不計考慮。注意TextView中需要:
android:ellipsize='none'android:singleLine='true'
兩個屬性,該效果只針對一行的文本。
2、貼一下java代碼
public class MainActivity extends AppCompatActivity { private TextView textView; private Button btn_click; private Button btn_click1; private Handler mHandler; @Override protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mHandler = new Handler();textView = findViewById(R.id.tv_text);btn_click = findViewById(R.id.btn_click);btn_click1 = findViewById(R.id.btn_click1);btn_click.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) {showAsrAnim(); }});btn_click1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) {textView.setVisibility(View.VISIBLE);textView.setText('百日不到處,青春恰自來' +'苔花如米小,也學(xué)牡丹開。'); }}); } private void showAsrAnim() {mHandler.post(new Runnable() { @Override public void run() {//在這里我們利用ValueAnimator.ofInt創(chuàng)建了一個值從textView的寬度到2的動畫,動畫時長是400ms,然后讓動畫開始//第一步:創(chuàng)建ValueAnimator實例ValueAnimator animator = ValueAnimator.ofInt(textView.getWidth(), 2);animator.setInterpolator(new LinearInterpolator());animator.setDuration(4000);//第二步:添加監(jiān)聽animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) {//獲取ValueAnimator在運動時,當(dāng)前運動點的值int width = (int) animation.getAnimatedValue();changeLayout(width);if (width == 2) { textView.setText(''); textView.setVisibility(View.INVISIBLE); ViewGroup.LayoutParams params = textView.getLayoutParams(); params.width = ViewGroup.LayoutParams.WRAP_CONTENT; textView.setLayoutParams(params);} }});animator.start(); }}); } private void changeLayout(int width) {ViewGroup.LayoutParams params = textView.getLayoutParams();params.width = width;textView.setLayoutParams(params); }}}
代碼中已經(jīng)有了注釋,創(chuàng)建一個ValueAnimator實例,添加監(jiān)聽,通過運動改變TextView的寬度,當(dāng)達到最小寬度2dp時將文本設(shè)置為空且不可見,從而實現(xiàn)該功能。
以上就是Android實現(xiàn)文字消除效果的詳細內(nèi)容,更多關(guān)于Android 文字消除效果的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. PHP設(shè)計模式中工廠模式深入詳解2. asp(vbs)Rs.Open和Conn.Execute的詳解和區(qū)別及&H0001的說明3. Ajax實現(xiàn)表格中信息不刷新頁面進行更新數(shù)據(jù)4. JSP數(shù)據(jù)交互實現(xiàn)過程解析5. ThinkPHP5實現(xiàn)JWT Token認證的過程(親測可用)6. .NET中l(wèi)ambda表達式合并問題及解決方法7. ASP.NET MVC遍歷驗證ModelState的錯誤信息8. 解決AJAX返回狀態(tài)200沒有調(diào)用success的問題9. ASP 信息提示函數(shù)并作返回或者轉(zhuǎn)向10. CSS hack用法案例詳解
