Android實(shí)現(xiàn)文字消除效果
今天和大家分享一個(gè)如何從右到左消除文本的動(dòng)畫。
先看效果圖:

由于項(xiàng)目和語音識(shí)別相關(guān),有時(shí)候人在不經(jīng)意間交流的無效音頻會(huì)被識(shí)別出來,并展示于界面,為了美觀,客戶要求我們將這些無效的識(shí)別文本用一個(gè)從右到左的動(dòng)畫給清除,于是便有了下述的技術(shù)實(shí)現(xiàn)。
嗯,效果做完后發(fā)現(xiàn)原理及其簡(jiǎ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">
<TextView
android: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:id="@+id/tv_text"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btn_click"
android:text="點(diǎn)擊清除"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btn_click1"
android:text="點(diǎn)擊恢復(fù)"/>
</LinearLayout>
btn_click1是為了演示方便而設(shè)計(jì)的,可不計(jì)考慮。注意TextView中需要:
android:ellipsize="none" android:singleLine="true"
兩個(gè)屬性,該效果只針對(duì)一行的文本。
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)建了一個(gè)值從textView的寬度到2的動(dòng)畫,動(dòng)畫時(shí)長(zhǎng)是400ms,然后讓動(dòng)畫開始
//第一步:創(chuàng)建ValueAnimator實(shí)例
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在運(yùn)動(dòng)時(shí),當(dāng)前運(yùn)動(dòng)點(diǎn)的值
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)建一個(gè)ValueAnimator實(shí)例,添加監(jiān)聽,通過運(yùn)動(dòng)改變TextView的寬度,當(dāng)達(dá)到最小寬度2dp時(shí)將文本設(shè)置為空且不可見,從而實(shí)現(xiàn)該功能。
以上就是Android實(shí)現(xiàn)文字消除效果的詳細(xì)內(nèi)容,更多關(guān)于Android 文字消除效果的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- Android8.1原生系統(tǒng)網(wǎng)絡(luò)感嘆號(hào)消除的方法
- Android開發(fā)實(shí)現(xiàn)消除屏幕鎖的方法
- Android實(shí)現(xiàn)文字動(dòng)態(tài)高亮讀取進(jìn)度效果
- Android 實(shí)現(xiàn)文字左右對(duì)齊
- Android基于AdapterViewFlipper實(shí)現(xiàn)的圖片/文字輪播動(dòng)畫控件
- Android獲取文字高度的三種方法
- Android Button按鈕點(diǎn)擊背景和文字變化操作
- Android實(shí)現(xiàn)文字滾動(dòng)播放效果的代碼
- Android實(shí)現(xiàn)文字下方加橫線
- android命令行模擬輸入事件(文字、按鍵、觸摸等)
相關(guān)文章
Android快速實(shí)現(xiàn)無預(yù)覽拍照功能
這篇文章主要為大家詳細(xì)介紹了Android快速實(shí)現(xiàn)無預(yù)覽拍照功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-06-06
Android學(xué)習(xí)之Span的使用方法詳解
這篇文章主要為大家詳細(xì)介紹了Android中各種Span類的使用方法,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Android有一定的幫助,需要的可以參考一下2022-06-06
Android自定義View實(shí)現(xiàn)APP啟動(dòng)頁倒計(jì)時(shí)效果
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)APP啟動(dòng)頁倒計(jì)時(shí)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02
Android采用雙緩沖技術(shù)實(shí)現(xiàn)畫板
這篇文章主要為大家詳細(xì)介紹了Android采用雙緩沖技術(shù)實(shí)現(xiàn)畫板的相關(guān)資料,思路清晰,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-05-05
Android 點(diǎn)擊ImageButton時(shí)有“按下”的效果的實(shí)現(xiàn)
這篇文章主要介紹了 Android 點(diǎn)擊ImageButton時(shí)有“按下”的效果的實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下2017-03-03
Android實(shí)現(xiàn)屏幕旋轉(zhuǎn)四個(gè)方向準(zhǔn)確監(jiān)聽
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)屏幕旋轉(zhuǎn)四個(gè)方向準(zhǔn)確監(jiān)聽,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07

