安卓中垂直跑马灯的实现_源崇121的博客-程序员资料

在我们开发过程中,跑马灯这个功能非常实用的,在实现这个功能的时候,这个时候我们通常需要找demo来实现这个方法,我从github上面找到这个demo感觉很好用,所以就要实现了这个功能喽MarqueeView,看这个工具类,因为我找这个类的时候是没有点击事件的,所以我给它加了一个点击事件,看这个工具类

public class MarqueeView extends ViewFlipper {

    private Context mContext;
    private List<String> notices;
    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<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 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);
    }

}
这就是它实现的方式,我从中加了点击事件,所以它的用法是这样的

<com.redsun.property.views.MarqueeView
    android:id="@+id/vertical_switch_textview1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:layout_toLeftOf="@+id/total_quantity"
    android:layout_toRightOf="@+id/news_image"
    android:background="@color/white"
    android:ellipsize="end"
    android:maxEms="10"
    android:maxLength="10"
    android:textColor="@color/gray_dark"
    android:textSize="@dimen/font_normal"
    app:mvAnimDuration="1000"
    app:mvInterval="3000"
    app:mvTextColor="@color/black"
    app:mvTextSize="14sp"
    tools:text="弘生活APP改版了"
    />
verticalSwitchTextView1 = (MarqueeView) rootView.findViewById(R.id.vertical_switch_textview1);
List<String> info = 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();
    }
});
这样就直接实现喽,其实还是蛮简单的呢。

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/u010859484/article/details/52851047

智能推荐

GDB调试的一系列博客_weixin_34416754的博客-程序员资料

2019独角兽企业重金招聘Python工程师标准&gt;&gt;&gt; ...

HLSL Intrinsic Functions_hlsl bit_警醒与鞭策的博客-程序员资料

//Cg 3.1 Toolkit Documentation 补充bitCount- return the number of bits set in a bitfield.bitfieldExtract- return an extracted range of bits from a bitfield.bitfieldInsert- returns an extrac...

C++封装互斥锁和基于RAII机制能自动解锁的互斥锁_奇妙之二进制的博客-程序员资料

#include "base/mutex.h"Mutex::Mutex() { pthread_mutex_init(&amp;mutex_, NULL); }Mutex::~Mutex() { pthread_mutex_destroy(&amp;mutex_); }void Mutex::Lock() { pthread_mutex_lock(&amp;mutex_); }void Mutex::Unlock() { pthread_mutex_unlock(&amp;mutex_);

struts2出错java.lang.NoClassDefFoundError: org/apache/commons/lang3/StringUtils_weixin_30364147的博客-程序员资料

http://blog.csdn.net/nero_2012/article/details/7550436转载于:https://www.cnblogs.com/yidijimao/p/5709829.html

Eclipse(STS) 导入本地 spring boot (gradle)多项目_weixin_34148456的博客-程序员资料

1、Import-General-Project from Folder or Archive2、选择需要导入的项目(这里是一个多项目,选择主项目导入就行)3、选中后finish就行了。转载于:https://www.cnblogs.com/tt9527/p/7145982.html...

Java入门需掌握的30个基本概念_码农之屋的博客-程序员资料

我的公众号「码农之屋」(id: Spider1818),分享的内容包括但不限于 Linux、网络、云计算虚拟化、容器Docker、OpenStack、Kubernetes、SDN、OVS、DPDK、Go、Python、C/C++编程技术等内容,欢迎大家关注。​1.OOP中唯一关系的是对象的接口是什么,就像计算机的销售商她不管电源内部结构是怎样的,他只关系能否给你提供电就行了,也就...

随便推点

python比较两个list之间的差异、相同(差集、交集、并集)_python比较两个list 之间元素差异_TravelingLight77的博客-程序员资料

初始化数据 listA = ['zhangsan', 'lisi', 'wangwu']listB = ['zhangsan', 'lisi', 'zhaoliu'] 1、取差集1.1、listA对应listB的差集 set(listA).difference(set(listB))-----set(['wangwu']) 1.2、listB对应listB的差集 set(listB).dif...

还不会CSS水平垂直居中?这里有12种方案_上学居中_前端向朔的博客-程序员资料

今天读书的时候,愕然发现自己居然没有总结过水平垂直居中的方法,在印象中,这个知识点确实是很神奇的存在:极其常见的需求,从理论上来看,它似乎极其简单,在实践中,它往往难如登天,当涉及尺寸不固定的元素时尤其如此。接下来我们就来总结一下该如何实现这...

请求表头headers设置Accept-Encoding为gzip,deflate,br时,python ——requets的get/post返回的结果有可能是乱码_我是个假程序员的博客-程序员资料

爬取某页面的数据时,在我本机环境进行请求,返回的结果是正常的,即不乱码,但是把代码拷贝到其他电脑运行,返回的结果就是乱码了。如下:我本机的请求结果:其他电脑运行的结果:百度了好久,都是说返回结果中文乱码的解决方案,都没有把问题解决(原来是自己问题点没有搜索对,毕竟当时自己也想不到)。后面在技术群中请教大佬后,才知道是headers请求头的Accept-Encoding设置值的问题,即Accept-Encoding='gzip,deflate,br’。因为代码都一样,在自己本机运行都正常,所以压根

SpringBoot集成SpringTask执行定时任务_spring boot dotaskbase_二十六画生的博客的博客-程序员资料

第一步:.yml文件添加配置:testTask: doTask: cron: 0 6 20 ? * * 第二步:新建配置文件spring-task.xml:&amp;lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&amp;gt;&amp;lt;beans xmlns=&quot;http://www.springframework.org/schema/...

极乐技术周报(第二十六期)_APPx应用魔方的博客-程序员资料

极乐技术周报(第二十六期)摘要:1. 十个免费的web前端开发工具;2. 美团点评 Java 后端新人第一年总结&面试经验;3. 我们来看看尤大神谈Vue.js;4.如何基于 PHP-X 快速开发一个 PHP 扩展;5.Java进阶之路 — 从初级 …某程序员对书法十分感兴趣,退休后决定在这方面有所建树。于是花重金购买了上等的文房四宝。一日,饭后突生雅兴,一番磨墨拟纸,并点上了上好的檀香,颇有王羲

二值图图像之间的二进制运算_两张二值图作位运算_RayChiu_Labloy的博客-程序员资料

java实现与运算、或运算、异或运算、取反运算、无符号左右移运算、有符号左右移运算

推荐文章

热门文章

相关标签