public class MyText extends View implements Runnable {
Paint mpaint=new Paint();
private String str="";
private String text="";
private int pointx,pointy=20;
int textWidth;
private boolean isover=false;
public boolean isnetwrong=false;
private Handler handler;
public MyText(Context context,AttributeSet attrs) {
super(context, attrs);
mpaint.setColor(Color.WHITE);
mpaint.setAntiAlias(true);
mpaint.setStyle(Style.STROKE);
mpaint.setTextSize(20);
System.out.println("width===="+this.getWidth());
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
canvas.drawText(str,pointx,pointy,mpaint);
}
public void setStr(String string)
{
str=string;
isover=false;
textWidth = (int) mpaint.measureText(str);
pointx=480;
post(this);
}
public void setHandler(Handler hand)
{
handler=hand;
}
@Override
public void run() {
// TODO Auto-generated method stub
if(isnetwrong)
{
//str="網路異常,請檢查網路設定!";
pointx=120;
this.invalidate();
return;
}
pointx-=2;
if(isover)
{
return;
}
if(pointx<=-(textWidth+240))
{
isover=true;
handler.sendEmptyMessage(LauncherActivity.TXT_SCROLL_OVER);
}
this.invalidate();
postDelayed(this, 20);
}
}
思路:自定义view,设置一字符串,然后在线程一直刷新并更改文字的起始位置,即可成为跑马灯效果,停止时设置isover为true。用单个的跑马灯效果还行,一个界面用多个有待测试。
转载于:https://my.oschina.net/u/865093/blog/204806