按照网上的方法 在textview外面嵌套了一个ScrollView 并设置
android:scrollbars="vertical"
android:fillViewport="true"
android:layout_weight="1.0"
属性之后可以手动实现滚动效果但并不是自动的,求大神指点啊!
网上搜到的代码,自定义view
public class AutoScrollText extends TextView {
public final static String TAG = AutoScrollText.class.getSimpleName();
private float textLength = 0f;//文本长度
private float viewWidth = 0f;
private float step = 0f;//文字的横坐标
private float y = 0f;//文字的纵坐标
private float temp_view_plus_text_length = 0.0f;//用于计算的临时变量
private float temp_view_plus_two_text_length = 0.0f;//用于计算的临时变量
public boolean isStarting = false;//是否开始滚动
private Paint paint = null;//绘图样式
private String text = "";//文本内容
public AutoScrollText(Context context) {
super(context);
}
public AutoScrollText(Context context, AttributeSet attrs) {
super(context, attrs);
}
public AutoScrollText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public void init(WindowManager windowManager,int color) {
paint = getPaint();
paint.setColor(color);
text = getText().toString();
textLength = paint.measureText(text);
viewWidth = getWidth();
if (viewWidth == 0) {
if (windowManager != null) {
Display display = 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
public Parcelable onSaveInstanceState() {
Parcelable superState = super.onSaveInstanceState();
SavedState ss = new SavedState(superState);
ss.step = step;
ss.isStarting = isStarting;
return ss;
}
@Override
public void onRestoreInstanceState(Parcelable state) {
if (!(state instanceof SavedState)) {
super.onRestoreInstanceState(state);
return;
}
SavedState ss = (SavedState) state;
super.onRestoreInstanceState(ss.getSuperState());
step = ss.step;
isStarting = ss.isStarting;
}
public static class SavedState extends BaseSavedState {
public boolean isStarting = false;
public float step = 0.0f;
SavedState(Parcelable superState) {
super(superState);
}
@Override
public void writeToParcel(Parcel out, int flags) {
super.writeToParcel(out, flags);
out.writeBooleanArray(new boolean[]{isStarting});
out.writeFloat(step);
}
public static final Parcelable.Creator<SavedState> CREATOR = new Parcelable.Creator<SavedState>() {
public SavedState[] newArray(int size) {
return new SavedState[size];
}
@Override
public SavedState createFromParcel(Parcel in) {
return new SavedState(in);
}
};
private SavedState(Parcel in) {
super(in);
boolean[] b = null;
in.readBooleanArray(b);
if (b != null && b.length > 0)
isStarting = b[0];
step = in.readFloat();
}
}
public void startScroll() {
isStarting = true;
invalidate();
}
public void stopScroll() {
isStarting = false;
invalidate();
}
@Override
public void onDraw(Canvas canvas) {
canvas.drawText(text, temp_view_plus_text_length - step, y, paint);
if (!isStarting) {
return;
}
step += 0.5;//0.5为文字滚动速度。
if (step > temp_view_plus_two_text_length)
step = textLength;
invalidate();
}
}
调用:
void scroll3(View view){
AutoScrollText auto=(AutoScrollText)view.findViewById(R.id.autoTxt);
auto.setText(“aaaaaaaaaaa”);
auto.init(getActivity().getWindowManager(),0xFFFF0000);
auto.startScroll();
}
本人亲测可用
你可以设置个计时器,每过多长时间调用一下ScrollView 滚动方法