直接自定义 view就行
/**
* @Author : 马占柱
* E-mail : mazhanzhu_3351@163.com
* Time : 2023/11/29 19:14
* Desc : 自定义view处理跑马灯闪烁问题
*/
public class MarqueeTextview extends androidx.appcompat.widget.AppCompatTextView {
public static final String TAG="MarqueeTextview";
public MarqueeTextview(@NonNull Context context) {
super(context,null);
}
public MarqueeTextview(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs,0);
}
public MarqueeTextview(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initView(context);
}
private void initView(Context context) {
this.setEllipsize(TextUtils.TruncateAt.MARQUEE);
this.setSingleLine(true);
this.setMarqueeRepeatLimit(-1);
}
@Override
public void setText(CharSequence text, BufferType type) {
super.setText(text, type);
}
@Override
protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
if(focused) {
super.onFocusChanged(focused, direction, previouslyFocusedRect);
}
}
@Override
public void onWindowFocusChanged(boolean focused) {
if(focused) {
super.onWindowFocusChanged(focused);
}
}
@Override
public boolean isFocused() {
return true;
}
}
最重要的就是这里
isFocused(),强制true,防止窗口频繁刷新导致跑马灯 跑一半突然又开始从头播放
有时候跑马灯view会随着进度条每秒刷新,导致文本没显示全又从头开始滚动了。处理这个问题,目前我的解决办法是:在跑马灯view外层再加一层LinearLayout。
<LinearLayout
android:id="@+id/player_song_name_ll"
android:layout_width="200dp"
android:layout_height="50dp"
android:orientation="horizontal"
android:layout_below="@id/xxxx"
android:layout_marginTop="34dp"
android:layout_centerHorizontal="true">
<xxxx.MarqueeTextView
android:id="@+id/player_song_name_tv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="旅行是一次心灵的觉醒。在旅途中,我们可以放下日常生活的繁忙和忧虑,"
android:textColor="@color/white"
android:textSize="32sp" />
</LinearLayout>