按照网上的方法 在textview外面嵌套了一个ScrollView 并设置
android:scrollbars="vertical"
android:fillViewport="true"
android:layout_weight="1.0"
属性之后可以手动实现滚动效果但并不是自动的,求大神指点啊!
如何实现TextView纵向滚动?纵向跑马灯效果
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
2条回答 默认 最新
- 小菜陈 2016-08-06 08:30关注
网上搜到的代码,自定义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();} 本人亲测可用
解决 无用评论 打赏 举报
悬赏问题
- ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
- ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
- ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
- ¥30 3天&7天&&15天&销量如何统计同一行
- ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
- ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
- ¥15 vs2019中数据导出问题
- ¥20 云服务Linux系统TCP-MSS值修改?
- ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
- ¥20 怎么在stm32门禁成品上增加查询记录功能