public View initView() {
//如果想实现跑马灯 可以这么做 不然直接用TextView也是没有关系的
//设置自己的TextView样式 继承TextView 重写isFoused方法为true
WaterTextView tv = initWaterTextView();
return tv;
}
@NonNull
private WaterTextView initWaterTextView() {
tv = new WaterTextView(getContext());
tv.setText(showText);
tv.setSingleLine(true);
Typeface typeface = Typeface.createFromAsset(mContext.getAssets(), "fonts/biaozhunheitijian.ttf");
tv.setTypeface(typeface);
tv.setEllipsize(TextUtils.TruncateAt.MARQUEE);
//设置重复次数 -1则无线循环
tv.setMarqueeRepeatLimit(1);
tv.setTextColor(this.getResources().getColor(R.color.marquee));
tv.setTextSize(24);
tv.setPadding(20, 15, 20, 15);
tv.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
tv.setX(getRandomInt(getWidth() - tv.getMeasuredWidth()));
tv.setY(getRandomInt(getWidth() - tv.getMeasuredHeight()));
}
private float getRandomInt(int max) {
Random random = new Random();
return random.nextInt(max);
}
});
return tv;
}
public TextSwitcherView(Context context) {
super(context, null);
}
public TextSwitcherView(Context context, AttributeSet attrs) {
super(context, attrs);
this.mContext = context;
init();
}
private void init() {
refresh();
View view = initView();
this.addView(view);
}