Android自定义TextView跑马灯效果

Android自定义TextView跑马灯效果,第1张

概述Android自带的跑马灯效果不太好控制,还必须要满足条件才能有效果,而且速度不受控制。前面我的博客中有一篇就是用Android自带的跑马灯效果的,但是基于不同的使用效果,这里在网上找到了一个更好的方法。沿用了作者

AndroID自带的跑马灯效果不太好控制,还必须要满足条件才能有效果,而且速度不受控制。前面我的博客中有一篇就是用AndroID自带的跑马灯效果的,但是基于不同的使用效果,这里在网上找到了一个更好的方法。沿用了作者的一些方法,但是添加了更好的扩展功能,和大家一起分享。这里面有控制往左往右两个方向的实现。

1、首先是简单的布局main.xml

<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="startFromhead"     androID:text="重置" />     <com.xuhui.customrolllight.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:ellipsize = "marquee" // 跑马灯效果,字数不超过就不动,超过就滚动     androID:textcolor="#000000"     androID:textSize="20dp" >   </com.xuhui.customrolllight.MarqueeText>   </linearLayout> 

2、自定义滚动方法MarqueeText.Java

import androID.content.Context; import androID.graphics.Canvas; import androID.graphics.Paint; import androID.util.AttributeSet; import androID.Widget.TextVIEw;   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); }   public MarqueeText(Context context,AttributeSet attrs) { super(context,attrs); }   public MarqueeText(Context context,AttributeSet attrs,int defStyle) { super(context,attrs,defStyle); }   @OverrIDe protected voID onMeasure(int wIDthMeasureSpec,int heightmeasureSpec) { // Todo auto-generated method stub super.onMeasure(wIDthMeasureSpec,heightmeasureSpec); currentScrollX = this.getWIDth(); }   protected voID onDraw(Canvas canvas) { 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; } postDelayed(this,5); } */ public voID run() { currentScrollX += 2;// 滚动速度.+号表示往左边- scrollTo(currentScrollX,0); if (isstop) { return; } if (getScrollX() >= (textWIDth)) { currentScrollX = -(this.getWIDth());// 当前出现的位置 } postDelayed(this,1); } /*( public voID run() {   // currentScrollX += 3;// 滚动速度.+号表示往左边- // scrollTo(currentScrollX,0);   if (textWIDth>this.getWIDth()) { currentScrollX += 3;// 滚动速度.+号表示往左边- scrollTo(currentScrollX,0); } if (getScrollX() >= (textWIDth)) { // scrollTo(this.getWIDth(),0); currentScrollX = -(this.getWIDth());// 当前出现的位置 } postDelayed(this,5); })这里面实现的是没有省略号的效果。文字没有超出框的长度就不滚,超出就滚*/  // 开始滚动 public voID startScroll() { isstop = false; this.removeCallbacks(this); post(this); }   // 停止滚动 public voID stopScroll() { isstop = true; }   // 从头开始滚动 public voID startFromhead() { currentScrollX = 0; startScroll(); } } 

上面注释掉的代码是实现文字往右边跑

3、下面是主程序MainActivity.java

import androID.app.Activity; import androID.os.Bundle; import androID.vIEw.VIEw;   public class MainActivity extends Activity {   private MarqueeText test; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); test=(MarqueeText) findVIEwByID(R.ID.test);  } public voID start(VIEw v){ test.startScroll(); } public voID stop(VIEw v){ test.stopScroll(); } public voID startFromhead(VIEw v){ test.startFromhead(); } } 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。

总结

以上是内存溢出为你收集整理的Android自定义TextView跑马灯效果全部内容,希望文章能够帮你解决Android自定义TextView跑马灯效果所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1146186.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存