这篇文章主要介绍Android怎么实现垂直跑马灯效果,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
公司主营业务:成都网站设计、网站建设、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。创新互联是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。创新互联推出东乡族免费做网站回馈大家。
在我们开发过程中,跑马灯这个功能非常实用的,在实现这个功能的时候,这个时候我们通常需要找demo来实现这个方法,我从github上面找到这个demo感觉很好用,所以就要实现了这个功能喽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); } // 根据公告字符串启动轮播 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()); } }); } // 根据公告字符串列表启动轮播 public void startWithList(List notices) { setNotices(notices); start(); } // 根据宽度和公告字符串启动轮播 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; } // 创建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); } }
这就是它实现的方式,我从中加了点击事件,所以它的用法是这样的
verticalSwitchTextView1 = (MarqueeView) rootView.findViewById(R.id.vertical_switch_textview1); Listinfo = new ArrayList<>(); info.add("1.能够适应多行长文本的Android TextView的例子"); info.add("2.\"科比,!"); info.add("3. GitHub帐号:zhangyuanchong"); info.add("4.\"理解的也很简单,"); info.add("5. 破解密钥"); info.add("6. 实现了两种方式"); 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(); } });
这样就直接实现喽,其实还是蛮简单的呢。
以上是“Android怎么实现垂直跑马灯效果”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注创新互联行业资讯频道!