這篇文章主要介紹Android怎么實現(xiàn)垂直跑馬燈效果,文中介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們一定要看完!
創(chuàng)新互聯(lián)建站擁有一支富有激情的企業(yè)網(wǎng)站制作團(tuán)隊,在互聯(lián)網(wǎng)網(wǎng)站建設(shè)行業(yè)深耕10余年,專業(yè)且經(jīng)驗豐富。10余年網(wǎng)站優(yōu)化營銷經(jīng)驗,我們已為成百上千中小企業(yè)提供了成都網(wǎng)站設(shè)計、成都網(wǎng)站建設(shè)解決方案,按需網(wǎng)站設(shè)計,設(shè)計滿意,售后服務(wù)無憂。所有客戶皆提供一年免費網(wǎng)站維護(hù)!在我們開發(fā)過程中,跑馬燈這個功能非常實用的,在實現(xiàn)這個功能的時候,這個時候我們通常需要找demo來實現(xiàn)這個方法,我從github上面找到這個demo感覺很好用,所以就要實現(xiàn)了這個功能嘍MarqueeView,看這個工具類,因為我找這個類的時候是沒有點擊事件的,所以我給它加了一個點擊事件,看這個工具類
public class MarqueeView extends ViewFlipper { private Context mContext; private Listnotices; private boolean isSetAnimDuration = false; private int contentSize; private int interval = 1000; private int animDuration = 500; private int textSize = 14; private int textColor = 0xffffffff; private int gravity = Gravity.LEFT | Gravity.CENTER_VERTICAL; //點擊事件 private OnItemClickListener onItemClickListener; public MarqueeView(Context context, AttributeSet attrs) { super(context, attrs); init(context, attrs, 0); } private void init(Context context, AttributeSet attrs, int defStyleAttr) { this.mContext = context; if (notices == null) { notices = new ArrayList<>(); } TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.MarqueeViewStyle, defStyleAttr, 0); interval = typedArray.getInteger(R.styleable.MarqueeViewStyle_mvInterval, interval); isSetAnimDuration = typedArray.hasValue(R.styleable.MarqueeViewStyle_mvAnimDuration); animDuration = typedArray.getInteger(R.styleable.MarqueeViewStyle_mvAnimDuration, animDuration); if (typedArray.hasValue(R.styleable.MarqueeViewStyle_mvTextSize)) { textSize = (int) typedArray.getDimension(R.styleable.MarqueeViewStyle_mvTextSize, textSize); textSize = DisplayUtil.px2sp(mContext, textSize); } textColor = typedArray.getColor(R.styleable.MarqueeViewStyle_mvTextColor, textColor); typedArray.recycle(); setFlipInterval(interval); Animation animIn = AnimationUtils.loadAnimation(mContext, R.anim.anim_marquee_in); if (isSetAnimDuration) animIn.setDuration(animDuration); setInAnimation(animIn); Animation animOut = AnimationUtils.loadAnimation(mContext, R.anim.anim_marquee_out); if (isSetAnimDuration) animOut.setDuration(animDuration); setOutAnimation(animOut); } // 根據(jù)公告字符串啟動輪播 public void startWithText(final String notice) { if (TextUtils.isEmpty(notice)) return; getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { getViewTreeObserver().removeGlobalOnLayoutListener(this); startWithFixedWidth(notice, getWidth()); } }); } // 根據(jù)公告字符串列表啟動輪播 public void startWithList(List notices) { setNotices(notices); start(); } // 根據(jù)寬度和公告字符串啟動輪播 private void startWithFixedWidth(String notice, int width) { int noticeLength = notice.length(); int dpW = DisplayUtil.px2dip(mContext, width); int limit = dpW / textSize; if (dpW == 0) { throw new RuntimeException("Please set MarqueeView width !"); } if (noticeLength <= limit) { notices.add(notice); } else { int size = noticeLength / limit + (noticeLength % limit != 0 ? 1 : 0); for (int i = 0; i < size; i++) { int startIndex = i * limit; int endIndex = ((i + 1) * limit >= noticeLength ? noticeLength : (i + 1) * limit); notices.add(notice.substring(startIndex, endIndex)); } } start(); } // 啟動輪播 public boolean start() { if (notices == null || notices.size() == 0) return false; removeAllViews(); for (int i = 0; i < notices.size(); i++) { final TextView textView = createTextView(notices.get(i), i); final int finalI = i; textView.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (onItemClickListener != null) { onItemClickListener.onItemClick(finalI, textView); } } }); addView(textView); } if (notices.size() > 1) { startFlipping(); } return true; } // 創(chuàng)建ViewFlipper下的TextView private TextView createTextView(String text, int position) { TextView tv = new TextView(mContext); tv.setGravity(gravity); tv.setText(text); tv.setTextColor(textColor); tv.setTextSize(textSize); tv.setTag(position); return tv; } public List getNotices() { return notices; } public void setNotices(List notices) { this.notices = notices; } public void setOnItemClickListener(OnItemClickListener onItemClickListener) { this.onItemClickListener = onItemClickListener; } public interface OnItemClickListener { void onItemClick(int position, TextView textView); } }
這就是它實現(xiàn)的方式,我從中加了點擊事件,所以它的用法是這樣的
verticalSwitchTextView1 = (MarqueeView) rootView.findViewById(R.id.vertical_switch_textview1); Listinfo = new ArrayList<>(); info.add("1.能夠適應(yīng)多行長文本的Android TextView的例子"); info.add("2.\"科比,!"); info.add("3. GitHub帳號:zhangyuanchong"); info.add("4.\"理解的也很簡單,"); info.add("5. 破解密鑰"); info.add("6. 實現(xiàn)了兩種方式"); verticalSwitchTextView1.startWithList(info); verticalSwitchTextView1.setOnItemClickListener(new MarqueeView.OnItemClickListener() { @Override public void onItemClick(int position, TextView textView) { position = position + 1; Toast.makeText(getActivity(), "點擊了" + position, Toast.LENGTH_SHORT).show(); } });
這樣就直接實現(xiàn)嘍,其實還是蠻簡單的呢。
以上是“Android怎么實現(xiàn)垂直跑馬燈效果”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!