1. 准备 txt文件

txt.png
2. 工具类
/**
* Created on 2021/3/18 9:53
*
* @author Gong Youqiang
*/
public class Utils {
private static final String TAG = "Utils";
/**
* 获取文件
* @return
*/
public static String getFileContent(String filePath ,String fileName) {
String content = "";
String path = filePath + fileName;
File file = new File(path);
try {
InputStream inputStream = new FileInputStream(file);
if (inputStream != null){
InputStreamReader reader = new InputStreamReader(inputStream,"GB2312");
BufferedReader bufferedReader = new BufferedReader(reader);
String line;
while ((line = bufferedReader.readLine()) != null){
content += line + "\n";
}
inputStream.close();
}
} catch (Exception e) {
e.printStackTrace();
}
Log.d(TAG, "getFileContent: content = " + content);
return content;
}
/**
* 获取raw文件夹下的文件内容
* @param context
* @return
*/
public static String getRawTxtFileContent(Context context,int rawResId) {
String content = "";
try {
InputStream inputStream = context.getResources().openRawResource(rawResId);
if (inputStream != null){
InputStreamReader reader = new InputStreamReader(inputStream,"GB2312");
BufferedReader bufferedReader = new BufferedReader(reader);
String line;
while ((line = bufferedReader.readLine()) != null){
content += line + "\n";
}
inputStream.close();
}
} catch (Exception e) {
e.printStackTrace();
}
Log.d(TAG, "getFileContent: content = " + content);
return content;
}
public static String getVersionInfo(Context context) {
PackageManager pm = context.getPackageManager();
String versionInfo = "";
try {
PackageInfo info = pm.getPackageInfo(context.getPackageName(),0);
versionInfo = info.versionName;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return versionInfo;
}
}
3. AutoScrollView.java
public class AutoScrollView extends ScrollView {
private static final String TAG = "AutoScrollView";
private static final int MSA_SCROLL = 1;
private int mPaddingTop = 0;
/**
* 每次滚动的像素点数
*/
private float mScrollPixelNums = 0.0f;
/**
* 刷新的时间间隔
*/
private int mScrollTimeInterval;
private int mHeight;
public AutoScrollView(Context context) {
this(context,null);
Log.d(TAG, "AutoScrollView: 1");
}
public AutoScrollView(Context context, AttributeSet attrs) {
this(context, attrs,0);
Log.d(TAG, "AutoScrollView: 2");
}
public AutoScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr,0);
Log.d(TAG, "AutoScrollView: 3");
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
mHeight = getChildAt(0).getHeight();
//计算刷新时间 :单位(像素)时间(ms) 高度(像素)
//刷新时间 = 高度 /
Log.d(TAG, "onLayout: height = " + mHeight);
}
private Handler mHandler = new Handler(){
@Override
public void handleMessage(@NonNull Message msg) {
super.handleMessage(msg);
Log.d(TAG, "handleMessage: msg.what = " + msg.what);
switch (msg.what){
case MSA_SCROLL:
//假如mPaddingTop等于scrollview,停止执行handler
if (mPaddingTop <= mHeight){
scrollTo(0,mPaddingTop);
mPaddingTop += mScrollPixelNums;
mHandler.removeMessages(MSA_SCROLL);
mHandler.sendEmptyMessageDelayed(MSA_SCROLL,mScrollTimeInterval);
}
break;
}
}
};
/**
* 开始滚动
* @param scrollPixelNums 每次滚动的像素点数量
* @param scrollTimeInterval 每次滚动的时间间隔
*/
public void startScroll(float scrollPixelNums,int scrollTimeInterval){
mScrollPixelNums = scrollPixelNums;
mScrollTimeInterval = scrollTimeInterval;
mHandler.sendEmptyMessageDelayed(MSA_SCROLL,mScrollTimeInterval);
}
public void stopScroll() {
mHandler.removeMessages(MSA_SCROLL);
}
}
- activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">
<com.hk.zxingtest.view.AutoScrollView
android:id="@+id/auto_scrollview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/layout_1">
<TextView
android:id="@+id/tv_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="32dp"
android:layout_marginRight="28dp"
android:layout_marginTop="28dp"
android:layout_marginBottom="31dp"
/>
</com.hk.zxingtest.view.AutoScrollView>
</LinearLayout>
- MainActivity.java
public class MainActivity extends AppCompatActivity {
@BindView(R.id.auto_scrollview)
AutoScrollView mAutoScrollView;
@BindView(R.id.tv_content)
TextView mTvContent;
private String mContent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
initData();
}
private void initData() {
mContent = Utils.getRawTxtFileContent(this,R.raw.rlxz);
mTvContent.setText(mContent);
mAutoScrollView.startScroll(1.0f,50);
}
}
网友评论