当前位置: 移动技术网 > IT编程>移动开发>Android > Android自定义TextView跑马灯效果

Android自定义TextView跑马灯效果

2019年07月24日  | 移动技术网IT编程  | 我要评论

痴汉电车电影,天天探索,环保手抄报

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(); 
} 
} 


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网