跑马灯效果,大家可以去原作者浏览https://github.com/sfsheng0322/MarqueeView
下面看自定义控件的代码
public class MarqueeVIEw extends VIEwFlipper { private Context mContext; private List<String> notices; private boolean isSetAnimDuration = false; private OnItemClickListener onItemClickListener; private int interval = 2000; private int animDuration = 500; private int textSize = 14; private int textcolor = 0xffffffff; private int gravity = Gravity.left | Gravity.CENTER_VERTICAL; private static final int TEXT_GraviTY_left = 0,TEXT_GraviTY_CENTER = 1,TEXT_GraviTY_RIGHT = 2; 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); int gravityType = typedArray.getInt(R.styleable.MarqueeVIEwStyle_mvGravity,TEXT_GraviTY_left); switch (gravityType) { case TEXT_GraviTY_CENTER: gravity = Gravity.CENTER; break; case TEXT_GraviTY_RIGHT: gravity = Gravity.RIGHT | Gravity.CENTER_VERTICAL; break; } 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<String> 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 int getposition() { return (int) getCurrentVIEw().getTag(); } public List<String> getNotices() { return notices; } public voID setNotices(List<String> notices) { this.notices = notices; } public voID setonItemClickListener(OnItemClickListener onItemClickListener) { this.onItemClickListener = onItemClickListener; } public interface OnItemClickListener { voID onItemClick(int position,TextVIEw textVIEw); }}
跑马灯vIEw是继承VIEwFlipper,可以看到他的结构体
其实VIEwFlipper工作机制很简单,如上图,就是将添加到VIEwFlipper中的子VIEw按照顺序定时的显示是其中一个子VIEw,其他的子VIEw设置为Gone状态
private Context mContext; private List<String> notices; private boolean isSetAnimDuration = false; private OnItemClickListener onItemClickListener; private int interval = 2000; private int animDuration = 500; private int textSize = 14; private int textcolor = 0xffffffff; private int gravity = Gravity.left | Gravity.CENTER_VERTICAL; private static final int TEXT_GraviTY_left = 0,TEXT_GraviTY_RIGHT = 2;
看出vIEw的一些属性,上下文,集合,是否动画延迟,点击事件,跑马灯的时间间隔,动画延迟时间,字体大小颜色,设置位置在靠左垂直中间对齐,文字的位置常量。
修改MarqueeVIEw的构造方法,我们可以在右键generate快速生成。
<declare-styleable name="MarqueeVIEwStyle"> <attr name="mvInterval" format="integer|reference"/> <attr name="mvAnimDuration" format="integer|reference"/> <attr name="mvTextSize" format="dimension|reference"/> <attr name="mvTextcolor" format="color|reference"/> <attr name="mvGravity"> <enum name="left" value="0"/> <enum name="center" value="1"/> <enum name="right" value="2"/> </attr> </declare-styleable>
TypedArray typedArray = getContext().obtainStyledAttributes(attrs,0);
首先获取属性集合,获取一个mv的间隔,默认值2000
isSetAnimDuration = typedArray.hasValue(R.styleable.MarqueeVIEwStyle_mvAnimDuration);
是否设置动画时间的延迟
if (typedArray.hasValue(R.styleable.MarqueeVIEwStyle_mvTextSize)) { textSize = (int) typedArray.getDimension(R.styleable.MarqueeVIEwStyle_mvTextSize,textSize); }
假如设置有自定义文字大小,就获取然后px转成sp
获取控件位置,自由设置
在后面要回收
typedArray.recycle();
setFlipInterval(interval);设置滚屏间隔,单位毫秒
Animation animIn = AnimationUtils.loadAnimation(mContext,R.anim.anim_marquee_out); if (isSetAnimDuration) animOut.setDuration(animDuration); setoutAnimation(animOut);
一进一出的动画效果
// 根据公告字符串启动轮播
public voID startWithText(final String notice)
暴露个公共方法,里面有测量vIEw的getVIEwTreeObserver方法,里面内部类调用了startWithFixeDWIDth(notice,getWIDth());方法
// 根据宽度和公告字符串启动轮播 private voID startWithFixeDWIDth(String notice,endindex)); } } start(); }
转换得到一个dp的宽度,限制字数长度大小。假如字符串小于直接add。假如过长取余得到行数。然后循环,获取那行的头尾,末尾假如2行字数还是比总体长度大就取总体长度,假如小于总体长度,就取那行的末尾下表。
然后开始播放
// 启动轮播 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),textVIEw); } } }); addVIEw(textVIEw); } if (notices.size() > 1) { startFlipPing(); } return true; }
创建部署每行的tv
// 创建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; }
然后将所有的textvIEw add起来,然后开始播放。后面就是做个点击回调,然后set get这个公告的集合。
最后不要忘了在布局顶层加入
xmlns:app="http://schemas.androID.com/apk/res-auto"
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。
总结以上是内存溢出为你收集整理的Android跑马灯MarqueeView源码解析全部内容,希望文章能够帮你解决Android跑马灯MarqueeView源码解析所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)