Android(安卓)ScrollView 实现小说阅读界面自动滚屏
16lz
2021-12-04
前言
最近在做一个阅读器,准备实现一个自动滚屏来方便阅读
实现
在xml中放入一个ScrollView 滑动条,滑动条里面放一个文本框
<RelativeLayout android:id="@+id/relativeLayout" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/auto_scroll" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom='0dp' android:text="Button" /> <ScrollView android:id="@+id/scrollView" android:layout_width="match_parent" android:layout_height="820dp"> <TextView android:id="@+id/tvTxt" android:layout_width="match_parent" android:layout_height="800dp" android:layout_marginStart="10dp" android:layout_marginEnd="10dp" android:text="Hello World!" android:textSize="24sp" /> </ScrollView> </RelativeLayout>
在对应的activity中分别定义一个ScrollView、textview对象
private ScrollView scrollView; private TextView textView; private RelativeLayout relativeLayout; private Button auto_scoll; private Handler mHandler = new Handler();//并初始化scrollView=findViewById(R.id.scrollView); textView = findViewById(R.id.tvTxt); auto_scoll=findViewById(R.id.auto_scroll); relativeLayout=findViewById(R.id.relativeLayout);
用线程来实现自动滚动,点击按钮开始,再次点击停止
private Runnable ScrollRunnable = new Runnable() { @Override public void run() { // TODO Auto-generated method stub int off = relativelayout.getMeasuredHeight() - scrollView.getHeight();// 判断高度// if (off > 0) { scrollView.scrollBy(0,100); if (scrollView.getScaleY() == off) { Thread.currentThread().interrupt(); } else { mHandler.postDelayed(this, 1000); }// } } };
效果
不足
缺陷在于,滚动的速度并不能控制(我当时没找到合适方法),各位看官有好的意见可以留言交流。
更多相关文章
- AppBarLayout中android:fitsSystemWindows="true"无效的解决办法
- android JNI 调用
- 在android使用mina实现网络访问
- android通过chmod命令实现文件权限修改
- Android,采用AIDL编写接口实现IPC 入门
- Android(安卓)APP使用自定义字体实现方法
- Android实现程序前后台切换效果
- Android中Hook Instrumentation 的实现
- Android(安卓)Overscroll