import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.AttributeSet;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.TextView;
![](https://img.16lz.com/image/product/ff3435bbf02e4f2c9b474eef455f6c26.gif)
/**
*
*TODO单行文本跑马灯控件
*
*@authortianlu
*@version1.0
*CreateAt:2010-2-16下午09:35:03
*/
public class AutoScrollTextView extends TextView implements OnClickListener {
publicfinalstaticStringTAG=AutoScrollTextView.class.getSimpleName();
![](https://img.16lz.com/image/product/68629797db704c09a34e3f22928f69ff.gif)
privatefloattextLength=0f;//文本长度
privatefloatviewWidth=0f;
privatefloatstep=0f;//文字的横坐标
privatefloaty=0f;//文字的纵坐标
privatefloattemp_view_plus_text_length=0.0f;//用于计算的临时变量
privatefloattemp_view_plus_two_text_length=0.0f;//用于计算的临时变量
publicbooleanisStarting=false;//是否开始滚动
privatePaintpaint=null;//绘图样式
privateStringtext="";//文本内容
![](https://img.16lz.com/image/product/68629797db704c09a34e3f22928f69ff.gif)
![](https://img.16lz.com/image/product/68629797db704c09a34e3f22928f69ff.gif)
publicAutoScrollTextView(Contextcontext){
super(context);
initView();
}
![](https://img.16lz.com/image/product/68629797db704c09a34e3f22928f69ff.gif)
publicAutoScrollTextView(Contextcontext,AttributeSetattrs){
super(context,attrs);
initView();
}
![](https://img.16lz.com/image/product/68629797db704c09a34e3f22928f69ff.gif)
publicAutoScrollTextView(Contextcontext,AttributeSetattrs,intdefStyle){
super(context,attrs,defStyle);
initView();
}
![](https://img.16lz.com/image/product/68629797db704c09a34e3f22928f69ff.gif)
/**
*初始化控件
*/
privatevoidinitView()
{
setOnClickListener(this);
}
![](https://img.16lz.com/image/product/68629797db704c09a34e3f22928f69ff.gif)
/**
*文本初始化,每次更改文本内容或者文本效果等之后都需要重新初始化一下
*/
publicvoidinit(WindowManagerwindowManager)
{
paint=getPaint();
text=getText().toString();
textLength=paint.measureText(text);
viewWidth=getWidth();
if(viewWidth==0)
{
if(windowManager!=null)
{
Displaydisplay=windowManager.getDefaultDisplay();
viewWidth=display.getWidth();
}
}
step=textLength;
temp_view_plus_text_length=viewWidth+textLength;
temp_view_plus_two_text_length=viewWidth+textLength*2;
y=getTextSize()+getPaddingTop();
}
![](https://img.16lz.com/image/product/68629797db704c09a34e3f22928f69ff.gif)
@Override
publicParcelableonSaveInstanceState()
{
ParcelablesuperState=super.onSaveInstanceState();
SavedStatess=newSavedState(superState);
![](https://img.16lz.com/image/product/68629797db704c09a34e3f22928f69ff.gif)
ss.step=step;
ss.isStarting=isStarting;
![](https://img.16lz.com/image/product/68629797db704c09a34e3f22928f69ff.gif)
returnss;
![](https://img.16lz.com/image/product/68629797db704c09a34e3f22928f69ff.gif)
}
![](https://img.16lz.com/image/product/68629797db704c09a34e3f22928f69ff.gif)
@Override
publicvoidonRestoreInstanceState(Parcelablestate)
{
if(!(stateinstanceofSavedState)){
super.onRestoreInstanceState(state);
return;
}
SavedStatess=(SavedState)state;
super.onRestoreInstanceState(ss.getSuperState());
![](https://img.16lz.com/image/product/68629797db704c09a34e3f22928f69ff.gif)
step=ss.step;
isStarting=ss.isStarting;
![](https://img.16lz.com/image/product/68629797db704c09a34e3f22928f69ff.gif)
}
![](https://img.16lz.com/image/product/68629797db704c09a34e3f22928f69ff.gif)
publicstaticclassSavedStateextendsBaseSavedState{
publicbooleanisStarting=false;
publicfloatstep=0.0f;
SavedState(ParcelablesuperState){
super(superState);
}
![](https://img.16lz.com/image/product/68629797db704c09a34e3f22928f69ff.gif)
@Override
publicvoidwriteToParcel(Parcelout,intflags){
super.writeToParcel(out,flags);
out.writeBooleanArray(newboolean[]{isStarting});
out.writeFloat(step);
}
![](https://img.16lz.com/image/product/68629797db704c09a34e3f22928f69ff.gif)
![](https://img.16lz.com/image/product/68629797db704c09a34e3f22928f69ff.gif)
publicstaticfinalParcelable.Creator<SavedState>CREATOR
=newParcelable.Creator<SavedState>(){
![](https://img.16lz.com/image/product/68629797db704c09a34e3f22928f69ff.gif)
publicSavedState[]newArray(intsize){
returnnewSavedState[size];
}
![](https://img.16lz.com/image/product/68629797db704c09a34e3f22928f69ff.gif)
@Override
publicSavedStatecreateFromParcel(Parcelin){
returnnewSavedState(in);
}
};
![](https://img.16lz.com/image/product/68629797db704c09a34e3f22928f69ff.gif)
privateSavedState(Parcelin){
super(in);
boolean[]b=null;
in.readBooleanArray(b);
if(b!=null&&b.length>0)
isStarting=b[0];
step=in.readFloat();
}
}
![](https://img.16lz.com/image/product/68629797db704c09a34e3f22928f69ff.gif)
/**
*开始滚动
*/
publicvoidstartScroll()
{
isStarting=true;
invalidate();
}
![](https://img.16lz.com/image/product/68629797db704c09a34e3f22928f69ff.gif)
/**
*停止滚动
*/
publicvoidstopScroll()
{
isStarting=false;
invalidate();
}
![](https://img.16lz.com/image/product/68629797db704c09a34e3f22928f69ff.gif)
![](https://img.16lz.com/image/product/68629797db704c09a34e3f22928f69ff.gif)
@Override
publicvoidonDraw(Canvascanvas){
canvas.drawText(text,temp_view_plus_text_length-step,y,paint);
if(!isStarting)
{
return;
}
step+=0.5;
if(step>temp_view_plus_two_text_length)
step=textLength;
invalidate();
![](https://img.16lz.com/image/product/68629797db704c09a34e3f22928f69ff.gif)
}
![](https://img.16lz.com/image/product/68629797db704c09a34e3f22928f69ff.gif)
@Override
publicvoidonClick(Viewv){
if(isStarting)
stopScroll();
else
startScroll();
![](https://img.16lz.com/image/product/68629797db704c09a34e3f22928f69ff.gif)
}
![](https://img.16lz.com/image/product/68629797db704c09a34e3f22928f69ff.gif)
} 实现了宽度的判断,文本自动滚动及开始和停止滚动等功能。
在UI xml中的配置如下:
< cn .tigertian.ui.AutoScrollTextViewandroid:id ="@+id/TextViewNotice"
android:layout_height ="30px" android:layout_width ="fill_parent"
android:text ="@string/test_notice_1" android:textColor ="#000" android:inputType ="text"
android:background ="#EEE" android:textSize ="20px" ></ cn.tigertian.ui.AutoScrollTextView >
在Activity中的使用方法如下:
// 启动公告滚动条
autoScrollTextView = (AutoScrollTextView)findViewById(R.id.TextViewNotice);
autoScrollTextView.init(getWindowManager());
autoScrollTextView.startScroll(); 如果想改变跑马灯的文字内容或者文字效果,则在调用完setText方法之后,需要再调用一下init(WindowManager windowManager)方法,重新进行初始化和相关参数的计算。
- 关于Android的文字排版和换行问题,彻底解决
- Android改变系统自带ProgressDialog的文字大小
- Android 自定义文本框(带图片)
- android 文字或者图片生成.pdf文件
- 捕获Android文本输入框的软键盘完成(Done)按键消息
- Android学习札记48:将TextView中的指定文字转换为表情显示
- android 自定义通知消息设置背景色不生效,导致部分机型显示白色字
- Android下载网络文本
- Android实现TextView部分文本监听单击事件
随机推荐
-
仅需一步!直接在 Windows 下使用 Linux
-
2021年报表软件排名_报表工具推荐
-
M1 芯片 Mac 有哪些优势?几款 Mac 之间有
-
您的客户管理决策是否低于10毫秒?
-
Microsoft Ignite 2021:Azure Disk Storag
-
Elasticsearch 之 API调用
-
python基本语法三
-
免费不限速跨平台文件传输神器—文件疯巢
-
关于Cisco Smart Install存在远程命令执
-
循环单链表及常用操作(C语言描述)