您好,登錄后才能下訂單哦!
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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。