随笔 - 62  文章 - 0  评论 - 114  阅读 - 18万

自定义TextView跑马灯效果,可控制启动,停止,和速度(含源码)

Android自带的跑马灯效果不太好控制,不能控制速度,不能即时停止和启动,而且还受焦点的影响蛋疼不已。由于项目需求需要用的可控制性高的跑马灯效果,所以自己写了一个自定义的TextView

  注意:在布局文件引用本view时,paddingLeft,paddingRigh都必须为0dp,需要增加这两个属性的,大家可以自行修改代码。

    android:ellipsize="marquee" android:singleLine="true" 这两个属性也要加上

 

复制代码
public class MarqueeText extends TextView implements Runnable {
        private int currentScrollX;// 当前滚动的位置
        private boolean isStop = false;
        private int textWidth;
        private boolean isMeasure = false;
 
        public MarqueeText(Context context) {
                super(context);
                // TODO Auto-generated constructor stub
        }
 
        public MarqueeText(Context context, AttributeSet attrs) {
                super(context, attrs);
        }
 
        public MarqueeText(Context context, AttributeSet attrs, int defStyle) {
                super(context, attrs, defStyle);
        }
 
        @Override
        protected void onDraw(Canvas canvas) {
                // TODO Auto-generated method stub
                super.onDraw(canvas);
                if (!isMeasure) {// 文字宽度只需获取一次就可以了
                        getTextWidth();
                        isMeasure = true;
                }
        }
 
        /**
         * 获取文字宽度
         */
        private void getTextWidth() {
                Paint paint = this.getPaint();
                String str = this.getText().toString();
                textWidth = (int) paint.measureText(str);
        }
 
        @Override
        public void run() {
                currentScrollX -= 2;// 滚动速度
                scrollTo(currentScrollX, 0);
                if (isStop) {
                        return;
                }
                if (getScrollX() <= -(this.getWidth())) {
                        scrollTo(textWidth, 0);
                        currentScrollX = textWidth;
//                        return;
                }
                postDelayed(this, 5);
        }
 
        // 开始滚动
        public void startScroll() {
                isStop = false;
                this.removeCallbacks(this);
                post(this);
        }
 
        // 停止滚动
        public void stopScroll() {
                isStop = true;
        }
 
        // 从头开始滚动
        public void startFor0() {
            currentScrollX = 0;
            startScroll();
        }
}
复制代码

布局文件:

复制代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
 
    <Button
        android:id="@+id/start"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="start"
        android:text="走起" />
 
    <Button
        android:id="@+id/stop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="stop"
        android:text="停止" />
 
    <Button
        android:id="@+id/startfor0"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="startFor0"
        android:text="从头开始" />
 
    <simtice.demo.marqueetext.MarqueeText
        android:id="@+id/test"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#339320"
        android:ellipsize="marquee"
        android:singleLine="true"
        android:text="这才是真正的文字跑马灯效果这才是真正的字跑马灯效果这才是真正的"
        android:textColor="#000000"
        android:textSize="20dp" >
    </simtice.demo.marqueetext.MarqueeText>
 
</LinearLayout>
 
MainActivity
复制代码
复制代码
public class MainActivity extends Activity {
        private MarqueeText test;
 
        @Override
        public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
                test = (MarqueeText) this.findViewById(R.id.test);
        }
 
        public void start(View v) {
                test.startScroll();
        }
 
        public void stop(View v) {
                test.stopScroll();
        }
        public void startFor0(View v){
                test.startFor0();
        }
}
复制代码

        

 

原文:http://www.eoeandroid.com/thread-247067-1-1.html

 

 

posted on   nuliniao  阅读(3317)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
< 2013年1月 >
30 31 1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31 1 2
3 4 5 6 7 8 9

点击右上角即可分享
微信分享提示