Android自定义TextView跑马灯效果

所属分类: 软件编程 / Android 阅读数: 48
收藏 0 赞 0 分享

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网络编程之获取网络上的Json数据实例

这篇文章主要介绍了Android网络编程之获取网络上的Json数据实例,本文用完整的代码实例讲解了在Android中读取网络中Json数据的方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中的windowSoftInputMode属性详解

这篇文章主要介绍了Android中的windowSoftInputMode属性详解,本文对windowSoftInputMode的9个属性做了详细总结,需要的朋友可以参考下
收藏 0 赞 0 分享

Android网络编程之UDP通信模型实例

这篇文章主要介绍了Android网络编程之UDP通信模型实例,本文给出了服务端代码和客户端代码,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中使用ListView实现漂亮的表格效果

这篇文章主要介绍了Android中使用ListView实现漂亮的表格效果,本文用详细的代码实例创建了一个股票行情表格,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中刷新界面的二种方法

这篇文章主要介绍了Android中刷新界面的二种方法,本文使用Handler、postInvalidate两种方法实现界面刷新,需要的朋友可以参考下
收藏 0 赞 0 分享

Android SDK三种更新失败及其解决方法

这篇文章主要介绍了Android SDK三种更新失败及其解决方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(一)

Android3.0(API level 11)开始,Android设备不再需要专门的菜单键。随着这种变化,Android app应该取消对传统6项菜单的依赖。取而代之的是提供anction bar来提供基本的用户功能
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(二)

这次将继续上一篇文章没有讲完的Menu的学习,上下文菜单(Context menu)和弹出菜单(Popup menu)
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(三)

今天继续昨天没有讲完的Menu的学习,主要是Popup Menu的学习,需要的朋友可以参考下
收藏 0 赞 0 分享

Android显示网络图片实例

这篇文章主要介绍了Android显示网络图片的方法,以实例形式展示了Android程序显示网络图片的方法,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多