文字的轮播,一种是跑马灯效果,文字字数超过了 TextView 可容纳的最大字数,并且为 TextView 设置了ellipsize=“marquee”,这时就可以实现文字轮播的效果了。另外一种效果是显示文字集合,每条数据上下滚动或是左右滚动。这种效果也是本篇的主要内容。
这是封装后的,里面包含一些设置属性的方法,为了使用更简单方便一些。使用的控件是 TextSwithcer。
/**
* Created by miao on 2018/12/6.
*/
public class SlideTextView extends TextSwitcher {
private int index = 0;//textview上下滚动下标
private Handler handler = new Handler();
private boolean isFlipping = false; // 是否启用轮播
private List<String> textList = new ArrayList<>();
private int inAnimId = R.anim.slide_in_bottom;
private int outAnimId = R.anim.slide_out_top;
private TextUtils.TruncateAt ellipsize = TextUtils.TruncateAt.MIDDLE;
private int textSize = 12;
private int textColor = Color.BLACK;
private int gravity = Gravity.CENTER;
private int textPaddingLeft = 25;
private int textPaddingTop = 0;
private int textPaddingRight = 25;
private int textPaddingBottom = 0;
private int duration = 3000;
private Context mContext;
private Runnable runnable = new Runnable() {
@Override
public void run() {
if (!isFlipping) {
return;
}
setText(textList.get(index % textList.size()));
if (index == textList.size()) {
index = 0;
}
index++;
startFlipping();
}
};
public SlideTextView(Context context) {
super(context);
mContext = context;
// setTextSwitcher();
}
public SlideTextView(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
// setTextSwitcher();
}
//开启轮播
public SlideTextView startFlipping() {
if (textList.size() == 1) {
setText(textList.get(0));
index = 0;
}
if (textList.size() > 1) {
handler.removeCallbacks(runnable);
isFlipping = true;
handler.postDelayed(runnable, duration);
}
return this;
}
//关闭轮播
public SlideTextView stopFlipping() {
if (textList.size() > 1) {
isFlipping = false;
handler.removeCallbacks(runnable);
}
return this;
}
/**
* 先设置 text 的各种属性,然后再添加到 textswitcher
* @return
*/
public SlideTextView addTextSwitcher() {
setInAnimation(AnimationUtils.loadAnimation(mContext, inAnimId));
setOutAnimation(AnimationUtils.loadAnimation(mContext, outAnimId));
setFactory(new ViewSwitcher.ViewFactory() {
@Override
public View makeView() {
TextView textView = new TextView(mContext);
textView.setTextSize(textSize);//字号
textView.setTextColor(textColor);
textView.setEllipsize(ellipsize);
textView.setSingleLine();
textView.setGravity(gravity);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
textView.setLayoutParams(params);
textView.setPadding(textPaddingLeft, textPaddingTop, textPaddingRight, textPaddingBottom);
return textView;
}
});
return this;
}
public List<String> getTextList() {
return textList;
}
public SlideTextView setTextList(List<String> textList) {
this.textList = textList;
return this;
}
public int getInAnimId() {
return inAnimId;
}
public SlideTextView setInAnimId(int inAnimId) {
this.inAnimId = inAnimId;
return this;
}
public int getOutAnimId() {
return outAnimId;
}
public SlideTextView setOutAnimId(int outAnimId) {
this.outAnimId = outAnimId;
return this;
}
public TextUtils.TruncateAt getEllipsize() {
return ellipsize;
}
public SlideTextView setEllipsize(TextUtils.TruncateAt ellipsize) {
this.ellipsize = ellipsize;
return this;
}
public int getTextSize() {
return textSize;
}
public SlideTextView setTextSize(int textSize) {
this.textSize = textSize;
return this;
}
public int getTextColor() {
return textColor;
}
public SlideTextView setTextColor(int textColor) {
this.textColor = textColor;
return this;
}
public int getGravity() {
return gravity;
}
public SlideTextView setGravity(int gravity) {
this.gravity = gravity;
return this;
}
public int getTextPaddingLeft() {
return textPaddingLeft;
}
public SlideTextView setTextPaddingLeft(int textPaddingLeft) {
this.textPaddingLeft = textPaddingLeft;
return this;
}
public int getTextPaddingTop() {
return textPaddingTop;
}
public SlideTextView setTextPaddingTop(int textPaddingTop) {
this.textPaddingTop = textPaddingTop;
return this;
}
public int getTextPaddingRight() {
return textPaddingRight;
}
public SlideTextView setTextPaddingRight(int textPaddingRight) {
this.textPaddingRight = textPaddingRight;
return this;
}
public int getTextPaddingBottom() {
return textPaddingBottom;
}
public SlideTextView setTextPaddingBottom(int textPaddingBottom) {
this.textPaddingBottom = textPaddingBottom;
return this;
}
public int getDuration() {
return duration;
}
public SlideTextView setDuration(int duration) {
this.duration = duration;
return this;
}
}
拉下来是如何使用:
mSlideTextView.setTextList(mWarningTextList)
.setTextColor(Color.RED)
.setTextSize(20)
.setTextPaddingLeft(30)
.setTextPaddingRight(30)
.setTextPaddingTop(30)
.setTextPaddingBottom(30)
.setEllipsize(TextUtils.TruncateAt.START)
.setInAnimId(R.anim.slide_in_right)
.setOutAnimId(R.anim.slide_out_left)
// 上面的这些值也可以不设置,就是默认的
// 注意这里的设置顺序,先 add 后 start
.addTextSwitcher()
.startFlipping();
}
还需要的就是两个 anim 文件了,很简单就不再这里贴了。需要的话点击这里查看。