php直播源码如何实现TextView竖直滚动,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。
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); } }
关于php直播源码如何实现TextView竖直滚动问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。