[转]Android文字跑马灯控件
16lz
2022-05-30
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;
/**
*
*TODO单行文本跑马灯控件
*
*@authortianlu
*@version1.0
*CreateAt:2010-2-16下午09:35:03
*/
public class AutoScrollTextView extends TextView implements OnClickListener {
publicfinalstaticStringTAG=AutoScrollTextView.class.getSimpleName();
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="";//文本内容
publicAutoScrollTextView(Contextcontext){
super(context);
initView();
}
publicAutoScrollTextView(Contextcontext,AttributeSetattrs){
super(context,attrs);
initView();
}
publicAutoScrollTextView(Contextcontext,AttributeSetattrs,intdefStyle){
super(context,attrs,defStyle);
initView();
}
/**
*初始化控件
*/
privatevoidinitView()
{
setOnClickListener(this);
}
/**
*文本初始化,每次更改文本内容或者文本效果等之后都需要重新初始化一下
*/
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();
}
@Override
publicParcelableonSaveInstanceState()
{
ParcelablesuperState=super.onSaveInstanceState();
SavedStatess=newSavedState(superState);
ss.step=step;
ss.isStarting=isStarting;
returnss;
}
@Override
publicvoidonRestoreInstanceState(Parcelablestate)
{
if(!(stateinstanceofSavedState)){
super.onRestoreInstanceState(state);
return;
}
SavedStatess=(SavedState)state;
super.onRestoreInstanceState(ss.getSuperState());
step=ss.step;
isStarting=ss.isStarting;
}
publicstaticclassSavedStateextendsBaseSavedState{
publicbooleanisStarting=false;
publicfloatstep=0.0f;
SavedState(ParcelablesuperState){
super(superState);
}
@Override
publicvoidwriteToParcel(Parcelout,intflags){
super.writeToParcel(out,flags);
out.writeBooleanArray(newboolean[]{isStarting});
out.writeFloat(step);
}
publicstaticfinalParcelable.Creator<SavedState>CREATOR
=newParcelable.Creator<SavedState>(){
publicSavedState[]newArray(intsize){
returnnewSavedState[size];
}
@Override
publicSavedStatecreateFromParcel(Parcelin){
returnnewSavedState(in);
}
};
privateSavedState(Parcelin){
super(in);
boolean[]b=null;
in.readBooleanArray(b);
if(b!=null&&b.length>0)
isStarting=b[0];
step=in.readFloat();
}
}
/**
*开始滚动
*/
publicvoidstartScroll()
{
isStarting=true;
invalidate();
}
/**
*停止滚动
*/
publicvoidstopScroll()
{
isStarting=false;
invalidate();
}
@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();
}
@Override
publicvoidonClick(Viewv){
if(isStarting)
stopScroll();
else
startScroll();
}
}
实现了宽度的判断,文本自动滚动及开始和停止滚动等功能。
在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中TextView嵌套在ScrollView中并自由滚动
- Android实现广告滚动,ViewFlipper
- Android(安卓)使用ContentObserver监听短信的变化,并发送信息给特
- Android(安卓)图片左上角、右上角标签tag
- Android(安卓)Jetpack Compose 之 Text
- GridView 实现水平拖拉效果
- GridView 实现水平拖拉效果
- Android(安卓)自定义文本框(带图片)
- android 版 双色球号码生成