TextView本身就有跑马灯的特效,但是不灵活,无法设定文字滚动的速度.下面是重写TextView的onDraw函数实现跑马灯效果.
package com.roy.view_test;
import android.util.*;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.widget.TextView;
import android.util.AttributeSet;
import android.view.*;
import android.view.View.OnClickListener;
public class AutoScrollTextView extends TextView implements OnClickListener {
private boolean isStarting = true;
private String text = "";
private Paint paint = null;
private float x = 0f;
private float y = 0f;
private float textWidth = 0f;
private float viewWidth = 0f;
private float updateSpeed = 2f;
public AutoScrollTextView(Context context) {
super(context);
setOnClickListener(this);
initData();
}
public AutoScrollTextView(Context context, AttributeSet attrs) {
super(context, attrs);
setOnClickListener(this);
initData();
}
public AutoScrollTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setOnClickListener(this);
initData();
}
public void initData(){
paint = getPaint();
text = getText().toString();
textWidth = paint.measureText(text);
viewWidth = getWidth(); //在绘制真正VIEW之前得到的为0
x = 0f;
y = getTextSize() + getPaddingTop();
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
viewWidth = getWidth();
canvas.drawText(text, x, y, paint);
if(!isStarting){
return;
}
x -= updateSpeed;
if (x < 0 && Math.abs(x) >= textWidth) {
x = viewWidth;
}
Log.i("X", String.valueOf(x));
invalidate();
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(isStarting){
stopScroll();
}else{
startScroll();
}
}
public void startScroll(){
isStarting = true;
invalidate();
}
public void stopScroll(){
isStarting = false;
invalidate();
}
public float getSpeed() {
return updateSpeed;
}
public void setSpeed(float updateSpeed) {
if(updateSpeed > 0 && updateSpeed < 10)
this.updateSpeed = updateSpeed;
}
}
下面是布局文件,有相应的原始TextView跑马灯作比较
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/TextView01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
android:textSize="30sp"
android:singleLine="true"
android:focusableInTouchMode="true"
android:marqueeRepeatLimit="marquee_forever"
android:ellipsize="marquee"
android:focusable="true" >
</TextView>
<com.roy.view_test.AutoScrollTextView
android:id="@+id/TextView02"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_marginTop="10dp"
android:text="@string/hello"
android:textSize="30sp"
android:singleLine="true"
/>
<com.roy.view_test.AutoScrollTextView
android:id="@+id/TextView03"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_marginTop="10dp"
android:text="@string/hello1"
android:textSize="30sp"
android:singleLine="true"
/>
</LinearLayout>
下面是MainActivity代码,默认代码,不需其他操作
package com.roy.view_test;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
资源文件
<resources>
<string name="app_name">View_Test</string>
<string name="hello_world">Hello world!</string>
<string name="menu_settings">Settings</string>
<string name="title_activity_main">MainActivity</string>
<string name="hello">这是一段长度很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长的文字</string>
<string name="hello1">这是一段宽度很短的文字</string>
</resources>
参考: http://blog.csdn.net/cao_tao199612/article/details/7475682