public class AutoScrollTextView extends TextSwitcher implements
ViewSwitcher.ViewFactory {
private static final int FLAG_START_AUTO_SCROLL = 1001;
private static final int FLAG_STOP_AUTO_SCROLL = 1002;
/**
* 輪播時間間隔
*/
private int scrollDuration = 2000;
/**
* 動畫時間
*/
private int animDuration = 1000;
/**
* 文字大小
*/
private float mTextSize = 14;
/**
* 文字Padding
*/
private int mPadding = 20;
/**
* 文字顏色
*/
private int textColor = Color.BLACK;
private OnItemClickListener itemClickListener;
private Context mContext;
/**
* 當前顯示Item的ID
*/
private volatile int currentId = -1;
private CopyOnWriteArrayList<String> textList;
private Handler handler;
public AutoScrollTextView(Context context) {
this(context, null);
mContext = context;
}
public AutoScrollTextView(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
init();
}
@SuppressLint("HandlerLeak")
private void init() {
textList = new CopyOnWriteArrayList<>();
handler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case FLAG_START_AUTO_SCROLL:
if (textList.size() > 0) {
currentId++;
setText(textList.get(currentId % textList.size()));
}
handler.sendEmptyMessageDelayed(FLAG_START_AUTO_SCROLL, scrollDuration);
break;
case FLAG_STOP_AUTO_SCROLL:
handler.removeMessages(FLAG_START_AUTO_SCROLL);
break;
}
}
};
setFactory(this);
Animation in = new TranslateAnimation(0, 0, 300, 0);
in.setDuration(animDuration);
in.setInterpolator(new AccelerateInterpolator());
Animation out = new TranslateAnimation(0, 0, 0, -300);
out.setDuration(animDuration);
out.setInterpolator(new AccelerateInterpolator());
setInAnimation(in);
setOutAnimation(out);
}
/**
* 設置數據源
*
* @param titles
*/
public void setTextList(ArrayList<String> titles) {
textList.clear();
textList.addAll(titles);
currentId = -1;
}
public void setText1(String text) {
if (TextUtils.isEmpty(text)) {
return;
}
stopAutoScroll();
int width = getWidth() - mPadding * 2;
TextPaint paint = new TextPaint();
paint.setTextSize(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, mTextSize, mContext.getResources().getDisplayMetrics()));
if (width < paint.measureText(" ")) {
return;
}
List<String> lineList = new ArrayList<>();
StringBuilder newLine = new StringBuilder();
for (int i = 0; i < text.length(); i++) {
if ('\n' == text.charAt(i)) {
lineList.add(newLine.toString());
newLine.setLength(0);
} else {
newLine.append(text.charAt(i));
if (paint.measureText(newLine.toString()) > width) {
lineList.add(newLine.toString().substring(0, newLine.toString().length() - 1));
i--;
newLine.setLength(0);
} else {
if (i == text.length() - 1) {
lineList.add(newLine.toString());
newLine.setLength(0);
break;
}
}
}
}
textList.clear();
textList.addAll(lineList);
currentId = -1;
}
/**
* 開始輪播
*/
public void startAutoScroll() {
if (textList.isEmpty()) {
return;
}
if (textList.size() == 1) {
setText(textList.get(0));
return;
}
handler.removeCallbacksAndMessages(null);
handler.sendEmptyMessage(FLAG_START_AUTO_SCROLL);
}
/**
* 停止輪播
*/
public void stopAutoScroll() {
handler.sendEmptyMessage(FLAG_STOP_AUTO_SCROLL);
}
@Override
public View makeView() {
TextView t = new TextView(mContext);
t.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
t.setMaxLines(1);
t.setPadding(mPadding, mPadding, mPadding, mPadding);
t.setTextColor(textColor);
t.setTextSize(mTextSize);
t.setClickable(true);
t.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (itemClickListener != null && textList.size() > 0 && currentId != -1) {
itemClickListener.onItemClick(currentId % textList.size());
}
}
});
return t;
}
/**
* 設置點擊事件監聽
*/
public void setOnItemClickListener(OnItemClickListener itemClickListener) {
this.itemClickListener = itemClickListener;
}
/**
* 輪播文本點擊監聽器
*/
public interface OnItemClickListener {
/**
* 點擊回調
*
* @param position 當前點擊ID
*/
public void onItemClick(int position);
}
}
使用 xml:
<com...widget.AutoScrollTextView
android:id="@+id/scroll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center" />
private AutoScrollTextView remindView;
remindView = findViewById(R.id.scroll);
//點擊監聽
remindView.setOnItemClickListener(new AutoScrollTextView.OnItemClickListener() {
@Override
public void onItemClick(int position) {
}
});
remindView.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
@Override
public boolean onPreDraw() {
remindView.getViewTreeObserver().removeOnPreDrawListener(this);
remindView.setText1("這裏來滾動滾動滾動滾動滾動滾動滾動滾動滾動滾動滾動滾動滾動");
remindView.startAutoScroll();
return true;
}
});