Android實(shí)現(xiàn)跑馬燈效果的代碼詳解
Android 實(shí)現(xiàn)跑馬燈效果
Android中實(shí)現(xiàn)跑馬燈效果有多種方式,本篇簡單介紹下:
1: TextView屬性實(shí)現(xiàn)
<TextView android:layout_width="150dp" android:layout_height="wrap_content" android:background="#77000000" android:padding="5dp" android:singleLine="true" android:ellipsize="marquee" android:scrollHorizontally="true" android:focusable="true" android:focusableInTouchMode="true" android:marqueeRepeatLimit="marquee_forever" android:text="這是textview的跑馬燈效果" android:id="@+id/tv1" />
這里需要注意下:
- 需要限制textview的寬度,不能設(shè)置為wrap_content
- 啟動跑馬燈效果需要獲取焦點(diǎn)requestFocus().
2: 代碼實(shí)現(xiàn)
//設(shè)置TextView只顯示一行文本。 tv2.setSingleLine(); //設(shè)置TextView的文本內(nèi)容是否可以水平滾動。 tv2.setHorizontallyScrolling(true); //設(shè)置當(dāng)TextView的文本內(nèi)容超出可顯示范圍時(shí)的省略方式,這里設(shè)置為跑馬燈效果。 tv2.setEllipsize(TextUtils.TruncateAt.MARQUEE); //設(shè)置跑馬燈效果重復(fù)的次數(shù),-1表示無限重復(fù)。 tv2.setMarqueeRepeatLimit(-1); //設(shè)置TextView是否可以獲取焦點(diǎn)。 tv2.setFocusable(true); //設(shè)置TextView在觸摸模式下是否可以獲取焦點(diǎn)。 tv2.setFocusableInTouchMode(true); //請求獲取焦點(diǎn)。 tv2.requestFocus();
3: 自定義 view實(shí)現(xiàn)
這里可以使用動畫的效果實(shí)現(xiàn).
package com.test.marquee; import android.animation.ValueAnimator; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.util.AttributeSet; import android.view.View; import android.view.animation.LinearInterpolator; import androidx.annotation.Nullable; public class MarqueeView extends View { private String text; private Paint paint; private float textWidth; private float textX; private float viewWidth; private ValueAnimator animator; public MarqueeView(Context context) { super(context); init(); } public MarqueeView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); init(); } public MarqueeView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } private void init() { text = “This is a marquee”; paint = new Paint(Paint.ANTI_ALIAS_FLAG); paint.setTextSize(50); paint.setColor(Color.BLACK); textWidth = paint.measureText(text); } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); viewWidth = w; textX = viewWidth; } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawText(text, textX, getHeight() / 2, paint); } public void startMarquee() { animator= ValueAnimator.ofFloat(viewWidth, -textWidth); animator.setDuration(5000); animator.setInterpolator(new LinearInterpolator()); animator.setRepeatCount(ValueAnimator.INFINITE); animator.addUpdateListener(animation -> { textX = (float) animation.getAnimatedValue(); invalidate(); }); animator.start(); } public void stopMarquee() { // 停止動畫 if (animator!=null) animator.cancel(); } }
4: 實(shí)現(xiàn)豎直效果的跑馬燈
package com.test.marquee; import android.content.Context; import android.graphics.Canvas; import android.text.TextUtils; import android.text.method.ScrollingMovementMethod; import android.util.AttributeSet; import androidx.annotation.Nullable; import androidx.appcompat.widget.AppCompatTextView; public class VerticalMarqueeTextView extends AppCompatTextView { private float offsetY; public VerticalMarqueeTextView(Context context) { super(context); init(); } public VerticalMarqueeTextView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); init(); } private void init() { setSingleLine(); setEllipsize(TextUtils.TruncateAt.MARQUEE); setMarqueeRepeatLimit(-1); setFocusable(true); setFocusableInTouchMode(true); setHorizontallyScrolling(true); setMovementMethod(ScrollingMovementMethod.getInstance()); } @Override protected void onDraw(Canvas canvas) { canvas.translate(0, offsetY); super.onDraw(canvas); } @Override public boolean isFocused() { return true; } @Override protected void onAttachedToWindow() { super.onAttachedToWindow(); post(new Runnable() { @Override public void run() { offsetY -= 1; if (offsetY <= -getHeight()) { offsetY = 0; } invalidate(); postDelayed(this, 20); } }); } }
以上就是Android實(shí)現(xiàn)跑馬燈效果的代碼詳解的詳細(xì)內(nèi)容,更多關(guān)于Android跑馬燈的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Android編程實(shí)現(xiàn)帶有圖標(biāo)的ListView并帶有長按菜單效果示例
這篇文章主要介紹了Android編程實(shí)現(xiàn)帶有圖標(biāo)的ListView并帶有長按菜單效果,結(jié)合實(shí)例形式分析了Android帶圖標(biāo)的ListView及菜單功能相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-06-06100 行代碼實(shí)現(xiàn)Flutter自定義TabBar的示例代碼
這篇文章主要介紹了100 行代碼實(shí)現(xiàn)Flutter自定義TabBar的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09Android TV listview及焦點(diǎn)處理
這篇文章主要介紹了Android TV listview及焦點(diǎn)處理的相關(guān)資料,需要的朋友可以參考下2017-06-06Android中實(shí)現(xiàn)毛玻璃效果的3種方法
這篇文章主要介紹了Android中實(shí)現(xiàn)毛玻璃效果的3種方法,本文講解了使用系統(tǒng)提供的方法、自定義的方法、C語言實(shí)現(xiàn)方法等3種方法,需要的朋友可以參考下2015-04-04Android開發(fā)Launcher進(jìn)程啟動流程
這篇文章主要為大家介紹了Android開發(fā)Launcher進(jìn)程啟動流程示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06Android Studio不能獲取遠(yuǎn)程依賴包的完美解決方法
這篇文章主要介紹了Android Studio不能獲取遠(yuǎn)程依賴包的解決方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-11-11Android SharedPreferences數(shù)據(jù)存儲詳解
SharedPreferences是安卓平臺上一個(gè)輕量級的存儲類,用來保存應(yīng)用的一些常用配置,比如Activity狀態(tài),Activity暫停時(shí),將此activity的狀態(tài)保存到SharedPereferences中;當(dāng)Activity重載,系統(tǒng)回調(diào)方法onSaveInstanceState時(shí),再從SharedPreferences中將值取出2022-11-11Android?webView加載數(shù)據(jù)時(shí)內(nèi)存溢出問題及解決
這篇文章主要介紹了Android?webView加載數(shù)據(jù)時(shí)內(nèi)存溢出問題及解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12