简单的文字轮播,效果图如下(事实上是比较慢的,ps 很烂才弄成了这样):
需要两个步骤:
1.复制工具类到项目中
import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.annotation.SuppressLint;
import android.widget.TextView;
/**
* author: wu
* date: on 2018/10/13.
* describe:自定义TextView,实现轮播
*/
@SuppressLint("AppCompatCustomView")
public class ForcedTextView extends TextView {
public ForcedTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
if (focused) {
super.onFocusChanged(focused, direction, previouslyFocusedRect);
}
}
public void onWindowFocusChanged(boolean hasWindowFocus) {
if (hasWindowFocus) {
super.onWindowFocusChanged(hasWindowFocus);
}
}
public boolean isFocused() {
return true;
}
}
2.再 xml 文件中添加如下代码即可:
<com.example.qd.handlerlizi.ForcedTextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="让我们荡起双桨"
android:singleLine="true"
android:marqueeRepeatLimit="marquee_forever"
android:gravity="center"
android:ellipsize="marquee"
android:textSize="18dp"/>