当前位置:   article > 正文

小说阅读软件阅读界面设计

小说阅读软件阅读界面设计

1、引言

        我把我的小说软件中的阅读界面解析规则和相关内容进行了更新,目前已经支持上一章和下一章的切换,并且能通过目录直接跳转到指定章节,当前被展示的章节目录中对应的章节名会有明显的变色效果,目前这个加载的数据只能识别来自于搜索界面的传递的内容,感兴趣的朋友可以参考我上一篇关于小说解析的文章。

2、代码实现

2.1、主界面布局

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context=".ui.ReadActivity">
  8. <WebView
  9. android:id="@+id/wv_read"
  10. android:layout_width="match_parent"
  11. android:layout_height="match_parent"/>
  12. <LinearLayout
  13. android:id="@+id/ll_wv_src"
  14. android:layout_width="match_parent"
  15. android:layout_height="match_parent"
  16. android:orientation="vertical"
  17. android:visibility="gone"
  18. android:background="#44404040"/>
  19. <LinearLayout
  20. android:id="@+id/ll_lv_back"
  21. android:layout_width="match_parent"
  22. android:layout_height="match_parent"
  23. android:orientation="vertical"
  24. android:layout_marginRight="100dp"
  25. android:visibility="gone"
  26. android:background="#f7e7c5">
  27. <ListView
  28. android:id="@+id/lv_list"
  29. android:layout_marginTop="20dp"
  30. android:layout_width="match_parent"
  31. android:layout_height="match_parent"
  32. android:layout_marginRight="10dp"
  33. android:divider="#00000000"/>
  34. </LinearLayout>
  35. <LinearLayout
  36. android:id="@+id/ll_add"
  37. android:layout_width="wrap_content"
  38. android:layout_height="wrap_content"
  39. android:background="@drawable/shape_shelf"
  40. android:gravity="center"
  41. android:layout_alignParentRight="true"
  42. android:layout_marginTop="100dp"
  43. android:visibility="gone"
  44. android:orientation="horizontal">
  45. <TextView
  46. android:id="@+id/tv_read_add"
  47. android:layout_width="wrap_content"
  48. android:layout_height="wrap_content"
  49. android:text="加入书架"
  50. android:textSize="15sp"
  51. android:textColor="#ffffff"
  52. android:layout_marginHorizontal="20dp"
  53. android:layout_marginVertical="10dp"
  54. android:visibility="visible"/>
  55. </LinearLayout>
  56. <!--用于加载网络数据-->
  57. <WebView
  58. android:id="@+id/wv_load"
  59. android:layout_width="match_parent"
  60. android:layout_height="match_parent"
  61. android:visibility="gone"/>
  62. </RelativeLayout>

2.2、目录界面布局

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="horizontal">
  6. <TextView
  7. android:id="@+id/tv_read_title"
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:textSize="20sp"
  11. android:layout_marginLeft="10dp"
  12. android:textColor="#cabea4"
  13. android:layout_marginVertical="10dp"
  14. android:text="书名"/>
  15. </LinearLayout>

2.3、真正阅读界面布局

        真正的阅读界面我是使用一个本地网页来展示的。

  1. <!DOCTYPE html>
  2. <html lang="zh_CN">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html" charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>阅读页面</title>
  7. <script type="text/javascript">
  8. //被java代码调用显示小说内容
  9. function showContent(data,title) {
  10. var div=document.getElementById("div_read");
  11. div.innerHTML=data;
  12. document.getElementById("span_last").innerHTML="上一章";
  13. document.getElementById("span_list").innerHTML="目录";
  14. document.getElementById("span_next").innerHTML="下一章";
  15. document.getElementById("h1_title").innerText=title;
  16. }
  17. function backStart() {
  18. scrollTo(0,0);//跳转到距离页面顶端0的位置
  19. }
  20. </script>
  21. <style type="text/css">
  22. #body{
  23. background-color: #d7c7a5;
  24. }
  25. div{
  26. font-size: 2ch;
  27. margin-left: 5%;
  28. margin-right: 5%;
  29. }
  30. .span_bottom{
  31. width: 30%;
  32. font-size: 2ch;
  33. position: center;
  34. color: #000000;
  35. }
  36. #span_list{
  37. margin-left: 20%;
  38. margin-right: 20%;
  39. }
  40. #div_read{
  41. color: rgb(85,85,85);
  42. }
  43. </style>
  44. </head>
  45. <body id="body">
  46. <br/>
  47. <center>
  48. <h2 id="h1_title"></h2>
  49. </center>
  50. <div id="div_read"></div>
  51. <br/>
  52. <br/>
  53. <center>
  54. <span id="span_last" class="span_bottom" onclick="window.Android.lastChapter()"></span>
  55. <span id="span_list" class="span_bottom" onclick="window.Android.showList()"></span>
  56. <span id="span_next" class="span_bottom" onclick="window.Android.nextChapter()"></span></center>
  57. <br/>
  58. <br/>
  59. </body>
  60. </html>

2.4、实现代码

        目前实现代码中包含的添加历史阅读记录和添加书架数据的部分功能暂时没有修改,因此这一部分内容请忽略,待我全部修改完成后会将完整的代码提交到我的网站提供下载。

  1. package xyz.dritrtj.read.ui;
  2. import android.annotation.SuppressLint;
  3. import android.content.ContentValues;
  4. import android.content.Intent;
  5. import android.content.pm.ActivityInfo;
  6. import android.database.Cursor;
  7. import android.database.sqlite.SQLiteDatabase;
  8. import android.graphics.Bitmap;
  9. import android.graphics.BitmapFactory;
  10. import android.graphics.Color;
  11. import android.graphics.drawable.GradientDrawable;
  12. import android.os.Bundle;
  13. import android.os.Handler;
  14. import android.os.Message;
  15. import android.view.View;
  16. import android.view.ViewGroup;
  17. import android.webkit.JavascriptInterface;
  18. import android.webkit.WebSettings;
  19. import android.webkit.WebView;
  20. import android.webkit.WebViewClient;
  21. import android.widget.BaseAdapter;
  22. import android.widget.LinearLayout;
  23. import android.widget.ListView;
  24. import android.widget.TextView;
  25. import androidx.annotation.NonNull;
  26. import androidx.appcompat.app.AppCompatActivity;
  27. import xyz.dritrtj.read.R;
  28. import xyz.dritrtj.read.data.Title;
  29. import xyz.dritrtj.read.fragment.BookShelfFragment;
  30. import xyz.dritrtj.read.interfaces.Init;
  31. import xyz.dritrtj.read.utils.DBHelper;
  32. import xyz.dritrtj.read.utils.SetUiSize;
  33. import org.jsoup.Jsoup;
  34. import org.jsoup.nodes.Document;
  35. import org.jsoup.nodes.Element;
  36. import org.jsoup.select.Elements;
  37. import java.io.FileOutputStream;
  38. import java.io.IOException;
  39. import java.io.InputStream;
  40. import java.net.HttpURLConnection;
  41. import java.net.URL;
  42. import java.util.ArrayList;
  43. import java.util.List;
  44. /**
  45. * 本界面不需要适配,后面为目录添加一个水平进出的动画效果更好,然后进一步添加水平方向翻页的效果
  46. */
  47. public class ReadActivity extends AppCompatActivity implements View.OnClickListener, Init {
  48. private LinearLayout ll_lv_back;//目录的布局
  49. private ListView lv_list;
  50. private WebView wv_read;
  51. private String path;//路径
  52. private String originPath;//初始路径路径
  53. private String data;//数据
  54. private int page;//章节索引
  55. private LinearLayout ll_wv_src;//webView的遮挡布局
  56. private String name;//书名
  57. private String author;//作者
  58. private int code;//表示跳转来源
  59. private Intent intent;//用于判断跳转来源
  60. private LinearLayout ll_add;
  61. private String imagePath;//图片路径
  62. private TextView tv_read_add;
  63. private WebView wv_load;
  64. @Override
  65. protected void onCreate(Bundle savedInstanceState) {
  66. super.onCreate(savedInstanceState);
  67. setContentView(R.layout.activity_read);
  68. initView();
  69. setViewSize();
  70. setData();
  71. }
  72. private boolean isShow=true;
  73. @SuppressLint("NonConstantResourceId")
  74. @Override
  75. public void onClick(View v) {
  76. switch (v.getId()){
  77. case R.id.ll_wv_src:
  78. ll_lv_back.setVisibility(View.GONE);
  79. ll_wv_src.setVisibility(View.GONE);
  80. isShow=true;
  81. break;
  82. case R.id.ll_add:
  83. add();
  84. break;
  85. }
  86. }
  87. @SuppressLint("SetJavaScriptEnabled")
  88. @Override
  89. public void initView() {
  90. View decorView=getWindow().getDecorView();//获取当前界面的DecorView
  91. int option=View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;//更改文字颜色为深黑色
  92. decorView.setSystemUiVisibility(option);//设置系统UI元素的可见性
  93. getWindow().setNavigationBarColor(Color.TRANSPARENT);
  94. getWindow().setStatusBarColor(Color.parseColor("#ffffff"));//将状态栏设置为白色
  95. getSupportActionBar().hide();
  96. wv_read = findViewById(R.id.wv_read);
  97. lv_list = findViewById(R.id.lv_list);
  98. ll_lv_back = findViewById(R.id.ll_lv_back);
  99. ll_wv_src = findViewById(R.id.ll_wv_src);
  100. ll_wv_src.setOnClickListener(this);
  101. lv_list.setOnItemClickListener((parent, view, position, id) -> {
  102. if (page!=position){
  103. page=position;
  104. ll_lv_back.setVisibility(View.GONE);
  105. ll_wv_src.setVisibility(View.GONE);
  106. isShow=true;
  107. handler.sendEmptyMessage(3);
  108. }
  109. });
  110. ll_add = findViewById(R.id.ll_add);
  111. ll_add.setOnClickListener(this);
  112. tv_read_add = findViewById(R.id.tv_read_add);
  113. intent = getIntent();
  114. path=intent.getStringExtra("url");
  115. originPath=path;
  116. name=intent.getStringExtra("name");
  117. author=intent.getStringExtra("author");
  118. imagePath=intent.getStringExtra("imagePath");
  119. code=Integer.parseInt(intent.getStringExtra("code"));
  120. WebSettings webSettings = wv_read.getSettings();
  121. //设置支持javaScript脚步语言
  122. webSettings.setJavaScriptEnabled(true);
  123. //设置js调用java支持
  124. wv_read.addJavascriptInterface(new JavaAndJs(),"Android");
  125. //设置客户端-不跳转到默认浏览器中
  126. wv_read.setWebViewClient(new WebViewClient());
  127. wv_read.loadUrl("file:///android_asset/read.html");
  128. wv_load = findViewById(R.id.wv_load);
  129. WebSettings settings = wv_load.getSettings();
  130. settings.setCacheMode(WebSettings.LOAD_NO_CACHE);//设置回退
  131. settings.setSupportZoom(true);//支持缩放
  132. settings.setBuiltInZoomControls(true);//出现缩放工具
  133. settings.setJavaScriptEnabled(true);// 表示webview可以执行服务器端的js代码
  134. settings.setUserAgentString("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 " +
  135. "(KHTML, like Gecko) Chrome/43.0.2357.134 Safari/537.36");
  136. //自适应屏幕
  137. settings.setUseWideViewPort(true);
  138. settings.setLoadWithOverviewMode(true);
  139. //自动缩放
  140. settings.setBuiltInZoomControls(true);
  141. settings.setSupportZoom(true);
  142. //支持获取手势焦点
  143. wv_load.requestFocusFromTouch();
  144. settings.setJavaScriptCanOpenWindowsAutomatically(true);//允许js弹出窗口
  145. wv_load.addJavascriptInterface(new AndroidAndJs(), "Android");
  146. wv_load.setWebViewClient(new WebViewClient(){
  147. @Override
  148. public void onPageFinished(WebView view, String url) {
  149. super.onPageFinished(view, url);
  150. if (url.startsWith("http")){
  151. if (flag==0){//加载目录
  152. jsFunction="javascript:function getDirectory() {\n" +
  153. " var list=document.getElementById('list');\n" +
  154. " var dds=list.getElementsByTagName('dd');\n" +
  155. " if(dds.length>12){\n" +
  156. " var dd;\n" +
  157. " var url;\n" +
  158. " var now_name;\n" +
  159. " var as;\n" +
  160. " var a;\n" +
  161. " for (var i = 12; i < dds.length; i++) {\n" +
  162. " dd=dds[i];\n" +
  163. " as=dd.getElementsByTagName('a');\n" +
  164. " a=as[0];\n" +
  165. " url=a.href;\n" +
  166. " now_name=a.innerHTML;\n" +
  167. " window.Android.setDirectory(url,now_name);\n" +
  168. " }\n" +
  169. " window.Android.setContent();\n" +
  170. " }\n" +
  171. " }";
  172. view.loadUrl(jsFunction);
  173. //调用 js函数
  174. view.loadUrl("javascript:getDirectory();");
  175. }
  176. if (flag==1){
  177. jsFunction="javascript:function getData() {\n" +
  178. " var div=document.getElementById('content');\n" +
  179. " var data=div.innerHTML;\n" +
  180. " window.Android.setData(data);\n" +
  181. " }";
  182. wv_load.loadUrl(jsFunction);
  183. //调用 js函数
  184. wv_load.loadUrl("javascript:getData();");
  185. }
  186. }
  187. }
  188. });
  189. wv_load.loadUrl(path);//加载章节目录
  190. }
  191. private int flag;//操作标志0代表读取目录
  192. private String jsFunction;//js函数
  193. @Override
  194. public void setViewSize() {
  195. SetUiSize.setMarginRightRelative(ll_lv_back,100);
  196. SetUiSize.setMarginTopLinear(lv_list,20);
  197. SetUiSize.setMarginRightLinear(lv_list,10);
  198. SetUiSize.setMarginTopRelative(ll_add,100);
  199. int size= (int) (20/SetUiSize.displayWidthDp*SetUiSize.displayWidth);
  200. GradientDrawable drawable = (GradientDrawable) ll_add.getBackground();
  201. drawable.setCornerRadii(new float[]{size,size,0,0,0,0,size,size});
  202. SetUiSize.setMarginTopLinear(tv_read_add,15);
  203. SetUiSize.setMarginHorizontalLinear(tv_read_add,20);
  204. SetUiSize.setMarginVerticalLinear(tv_read_add,10);
  205. }
  206. @Override
  207. public void setData() {
  208. // loadContent();
  209. }
  210. class JavaAndJs{
  211. @JavascriptInterface
  212. public void showList(){
  213. if (isShow){
  214. handler.sendEmptyMessage(2);
  215. }
  216. }
  217. //上一章
  218. @JavascriptInterface
  219. public void lastChapter(){
  220. if ((page-1)>=0){
  221. page--;
  222. handler.sendEmptyMessage(3);
  223. }
  224. }
  225. //下一章
  226. @JavascriptInterface
  227. public void nextChapter(){
  228. if ((page+1)<=list.size()-1){
  229. page++;
  230. handler.sendEmptyMessage(3);
  231. }
  232. }
  233. }
  234. private List<Title> list=new ArrayList<>();
  235. private ListAdapter adapter=new ListAdapter();
  236. private class ListAdapter extends BaseAdapter{
  237. @Override
  238. public int getCount() {
  239. return list.size();
  240. }
  241. @Override
  242. public Object getItem(int position) {
  243. return list.get(position);
  244. }
  245. @Override
  246. public long getItemId(int position) {
  247. return position;
  248. }
  249. @Override
  250. public View getView(int position, View convertView, ViewGroup parent) {
  251. Holder holder;
  252. if (convertView==null){
  253. convertView=View.inflate(ReadActivity.this,
  254. R.layout.item_read,null);
  255. holder=new Holder();
  256. holder.tv_read_title=convertView.
  257. findViewById(R.id.tv_read_title);
  258. SetUiSize.setTextViewSize(holder.tv_read_title,20);
  259. SetUiSize.setMarginLeftLinear(holder.tv_read_title,10);
  260. SetUiSize.setMarginVerticalLinear(holder.tv_read_title,10);
  261. convertView.setTag(holder);
  262. }else {
  263. holder= (Holder) convertView.getTag();
  264. }
  265. Title title = list.get(position);
  266. holder.tv_read_title.setText(title.chapter);
  267. if (page==position){
  268. holder.tv_read_title.setTextColor(Color.parseColor("#000000"));
  269. }else {
  270. holder.tv_read_title.setTextColor(Color.parseColor("#cabea4"));
  271. }
  272. return convertView;
  273. }
  274. private class Holder{
  275. public TextView tv_read_title;
  276. }
  277. }
  278. /**
  279. * 加载主体数据
  280. * @param path url
  281. */
  282. @SuppressLint("HandlerLeak")
  283. private Handler handler=new Handler(){
  284. @Override
  285. public void handleMessage(@NonNull Message msg) {
  286. super.handleMessage(msg);
  287. switch (msg.what){
  288. case 0:
  289. wv_read.loadUrl("javascript:backStart()");//将页面滑动到开始位置
  290. wv_read.loadUrl("javascript:showContent("+"'"+data+"'"+","+"'"+list.get(page).chapter+"'"+")");
  291. // update();
  292. break;
  293. case 1://加载目录
  294. {
  295. lv_list.setAdapter(adapter);
  296. handler.sendEmptyMessage(3);
  297. }
  298. break;
  299. case 2:
  300. ll_lv_back.setVisibility(View.VISIBLE);
  301. ll_wv_src.setVisibility(View.VISIBLE);
  302. isShow=false;
  303. break;
  304. case 3://加载章节内容
  305. flag=1;
  306. wv_load.loadUrl(list.get(page).path);
  307. break;
  308. }
  309. }
  310. };
  311. private void loadContent(String path){
  312. Document document = null;
  313. try {
  314. document = Jsoup.connect(path).get();
  315. Element content = document.getElementById("content");
  316. String html = content.html();
  317. data = html.substring(0, html.indexOf("<p>")-5);
  318. handler.sendEmptyMessage(0);
  319. } catch (IOException e) {
  320. throw new RuntimeException(e);
  321. }
  322. }
  323. private String tmpStr="";
  324. /**
  325. * 进入主界面时加载目录和页面内容数据
  326. */
  327. private void loadContent() {
  328. new Thread(){
  329. @Override
  330. public void run() {
  331. //4.解析主体内容
  332. Document document;
  333. try {
  334. //1.抓取目录和对应的章节相对路径,后面需要合成绝对路径
  335. document = Jsoup.connect(path).get();
  336. Elements dl = document.getElementsByTag("dl");
  337. Elements a = dl.first().getElementsByTag("a");
  338. Title title;
  339. int index=0;
  340. for (Element element:a){
  341. title=new Title();
  342. title.chapter=new String(element.text());//目录
  343. title.path=new String(element.attr("href"));//相对路径
  344. list.add(title);
  345. if (index==0){
  346. tmpStr="https://www.ibiquge.la"+title.path;
  347. index=-1;
  348. }
  349. }
  350. if (index==-1){
  351. handler.sendEmptyMessageDelayed(1,50);
  352. //2.解析主体内容,判断显示是否加入书架的按钮
  353. if (code==1){//如果是来源于书架,那么重新定位阅读位置
  354. page= Integer.parseInt(intent.getStringExtra("page"));
  355. tmpStr="https://www.ibiquge.la"+list.get(page).path;
  356. }if (code==2){//如果是来源于阅读记录,那么重新定位阅读位置
  357. page= Integer.parseInt(intent.getStringExtra("page"));
  358. tmpStr="https://www.ibiquge.la"+list.get(page).path;
  359. if (select()){
  360. code=1;
  361. }
  362. }else {
  363. select();
  364. }
  365. loadContent(tmpStr);
  366. }
  367. } catch (IOException e) {
  368. throw new RuntimeException(e);
  369. }
  370. }
  371. }.start();
  372. }
  373. /**
  374. * 添加
  375. */
  376. private void add(){
  377. DBHelper dbHelper=new DBHelper(ReadActivity.this,1);
  378. SQLiteDatabase database = dbHelper.getReadableDatabase();
  379. String sql="CREATE TABLE IF NOT EXISTS shelf(\n" +
  380. "\tbook VARCHAR(20) NOT NULL,\n" +
  381. "\tauthor VARCHAR(30) NOT NULL,\n" +
  382. "\tintroduce VARCHAR(200),\n" +
  383. "\tpage VARCHAR(100),\n" +
  384. "\torigin VARCHAR(100) NOT NULL,\n" +
  385. "\tprogress INTEGER NOT NULL\n" +
  386. ");";
  387. database.execSQL(sql);
  388. ContentValues value=new ContentValues();
  389. value.put("book",name);//书名
  390. value.put("author",author);//作者
  391. value.put("origin",originPath);//书源初始路径
  392. value.put("progress",page);//阅读进度
  393. String imageName;
  394. if (code==2){
  395. imageName=intent.getStringExtra("imageName");
  396. }else{
  397. imageName=imagePath.substring(imagePath.lastIndexOf("/") + 1);
  398. }
  399. value.put("page",imageName);//封面名字
  400. database.insert("shelf",null,value);
  401. database.close();
  402. ll_add.setVisibility(View.GONE);
  403. new Thread(){
  404. @Override
  405. public void run() {
  406. Bitmap bitmap = null;
  407. try {
  408. //得到连接
  409. URL url = new URL(imagePath);
  410. HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  411. //设置
  412. connection.setConnectTimeout(5000);
  413. connection.setReadTimeout(5000);
  414. //连接
  415. connection.connect();
  416. //发请求读取返回的数据并封装为bitmap
  417. int responseCode = connection.getResponseCode();
  418. if(responseCode==200) {
  419. InputStream is = connection.getInputStream();//图片文件流
  420. //将is封装为bitmap
  421. bitmap = BitmapFactory.decodeStream(is);
  422. is.close();
  423. if(bitmap!=null) {
  424. //缓存到本地
  425. // /storage/sdcard/Android/data/packageName/files/
  426. String filesPath = getExternalFilesDir(null).getAbsolutePath();
  427. String fileName = imagePath.substring(imagePath.lastIndexOf("/")+1);//xxx.jpg
  428. String filePath = filesPath+"/"+fileName;
  429. bitmap.compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(filePath));
  430. }
  431. }
  432. connection.disconnect();
  433. } catch (Exception e) {
  434. e.printStackTrace();
  435. }
  436. }
  437. }.start();
  438. BookShelfFragment.fragment.handler.sendEmptyMessageDelayed(0,50);
  439. }
  440. /**
  441. * 更新书架
  442. */
  443. private void update(){
  444. DBHelper dbHelper=new DBHelper(ReadActivity.this,1);
  445. SQLiteDatabase database = dbHelper.getReadableDatabase();
  446. String sql="CREATE TABLE IF NOT EXISTS shelf(\n" +
  447. "\tbook VARCHAR(20) NOT NULL,\n" +
  448. "\tauthor VARCHAR(30) NOT NULL,\n" +
  449. "\tintroduce VARCHAR(200),\n" +
  450. "\tpage VARCHAR(100),\n" +
  451. "\torigin VARCHAR(100) NOT NULL,\n" +
  452. "\tprogress INTEGER NOT NULL\n" +
  453. ");";
  454. database.execSQL(sql);
  455. ContentValues value=new ContentValues();
  456. value.put("progress",page);//阅读进度
  457. database.update("shelf",value,"book = ? AND author = ?",
  458. new String[]{name,author});
  459. database.close();
  460. updateHistory();
  461. BookShelfFragment.fragment.handler.sendEmptyMessage(0);
  462. }
  463. /**
  464. * 更新历史记录
  465. */
  466. @SuppressLint("Range")
  467. private void updateHistory(){
  468. DBHelper dbHelper=new DBHelper(ReadActivity.this,1);
  469. SQLiteDatabase database = dbHelper.getReadableDatabase();
  470. String sql="CREATE TABLE IF NOT EXISTS history(\n" +
  471. "\tbook VARCHAR(20) NOT NULL,\n" +
  472. "\tauthor VARCHAR(30) NOT NULL,\n" +
  473. "\tintroduce VARCHAR(200),\n" +
  474. "\tpage VARCHAR(100),\n" +
  475. "\torigin VARCHAR(100) NOT NULL,\n" +
  476. "\tprogress INTEGER NOT NULL\n" +
  477. ");";
  478. database.execSQL(sql);
  479. Cursor shelf = database.query("history", null, "book = ? AND author = ?",
  480. new String[]{name, author}, null, null, null);
  481. if (!shelf.moveToNext()){
  482. ContentValues value=new ContentValues();
  483. value.put("book",name);//书名
  484. value.put("author",author);//作者
  485. value.put("origin",originPath);//书源初始路径
  486. value.put("progress",page);//阅读进度
  487. String imageName = imagePath.substring(imagePath.lastIndexOf("/") + 1);
  488. value.put("page",imageName);//封面名字
  489. database.insert("history",null,value);
  490. new Thread(){
  491. @Override
  492. public void run() {
  493. Bitmap bitmap = null;
  494. try {
  495. //得到连接
  496. URL url = new URL(imagePath);
  497. HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  498. //设置
  499. connection.setConnectTimeout(5000);
  500. connection.setReadTimeout(5000);
  501. //连接
  502. connection.connect();
  503. //发请求读取返回的数据并封装为bitmap
  504. int responseCode = connection.getResponseCode();
  505. if(responseCode==200) {
  506. InputStream is = connection.getInputStream();//图片文件流
  507. //将is封装为bitmap
  508. bitmap = BitmapFactory.decodeStream(is);
  509. is.close();
  510. if(bitmap!=null) {
  511. //缓存到本地
  512. // /storage/sdcard/Android/data/packageName/files/
  513. String filesPath = getExternalFilesDir(null).getAbsolutePath();
  514. String fileName = imagePath.substring(imagePath.lastIndexOf("/")+1);//xxx.jpg
  515. String filePath = filesPath+"/"+fileName;
  516. bitmap.compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(filePath));
  517. }
  518. }
  519. connection.disconnect();
  520. } catch (Exception e) {
  521. e.printStackTrace();
  522. }
  523. }
  524. }.start();
  525. }
  526. ContentValues value=new ContentValues();
  527. value.put("progress",page);//阅读进度
  528. database.update("history",value,"book = ? AND author = ?",
  529. new String[]{name,author});
  530. database.close();
  531. }
  532. /**
  533. * 查询本书是否已经加入书架
  534. */
  535. @SuppressLint("Range")
  536. private boolean select(){
  537. boolean isAdd;
  538. DBHelper dbHelper=new DBHelper(ReadActivity.this,1);
  539. SQLiteDatabase database = dbHelper.getReadableDatabase();
  540. String sql="CREATE TABLE IF NOT EXISTS shelf(\n" +
  541. "\tbook VARCHAR(20) NOT NULL,\n" +
  542. "\tauthor VARCHAR(30) NOT NULL,\n" +
  543. "\tintroduce VARCHAR(200),\n" +
  544. "\tpage VARCHAR(100),\n" +
  545. "\torigin VARCHAR(100) NOT NULL,\n" +
  546. "\tprogress INTEGER NOT NULL\n" +
  547. ");";
  548. database.execSQL(sql);
  549. Cursor shelf = database.query("shelf", null, "book = ? AND author = ?",
  550. new String[]{name, author}, null, null, null);
  551. if (shelf.moveToNext()){
  552. page=shelf.getInt(shelf.getColumnIndex("progress"));
  553. tmpStr=list.get(page).path;
  554. isAdd=true;
  555. }else {
  556. runOnUiThread(() -> ll_add.setVisibility(View.VISIBLE));
  557. isAdd=false;
  558. }
  559. database.close();
  560. return isAdd;
  561. }
  562. private Title title;
  563. private class AndroidAndJs{
  564. /**
  565. * 加载目录数据
  566. * @param url 章节url
  567. * @param name 章节名
  568. */
  569. @JavascriptInterface
  570. public void setDirectory(String url,String name) {
  571. title=new Title();
  572. title.chapter=name;//章节名
  573. title.path=url;//章节路径
  574. list.add(title);
  575. }
  576. /**
  577. * 目录加载完成,加载章节内容
  578. */
  579. @JavascriptInterface
  580. public void setContent() {
  581. handler.sendEmptyMessage(1);//加载显示目录
  582. }
  583. /**
  584. * 显示章节内容
  585. */
  586. @JavascriptInterface
  587. public void setData(String now_data) {
  588. data=now_data;
  589. handler.sendEmptyMessage(0);
  590. }
  591. }
  592. }

3、效果展示

3.1、阅读界面展示

        从阅读界面中可以看出,还有部分来自于网站的广告没有去掉,这个后期再修改。目前阅读界面是使用竖屏的方式进行展示小说内容的,暂时不支持横屏切换阅读,这个功能后期再添加,每加载一个新章节后会将阅读界面滑动到开头的位置。

3.2、底部章节切换按钮展示

        这里我的上下章节的切换按钮和目录展示按钮是放在阅读界面底部的,虽然是竖屏展示内容,但目前并不支持将多个章节内容连接在一起自动切换展示,该该功能后期酌情添加。

 3.3、目录展示效果

        目前目录是从左边弹出的,点击外部区域目录会被隐藏,我使用相对布局的方式在目录背后填充了一层布局,用于判断是否点击目录中的内容,同时也是为了防止对其它部分进行误操作。

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读