首页 编程 软件学院 查看内容

android自定义控件创建翻页接口详细代码

2016-7-27 23:33 1165 0

摘要: 本文分享的这个类的目的是为在看书翻页时,需要进行的动作提供接口,利用android自定义控件创建翻页接口,具体内容如下 BookPage.java ?12345678910111213141516171819202122232425262728293031323334353637383940 ...

本文分享的这个类的目的是为在看书翻页时,需要进行的动作提供接口,利用android自定义控件创建翻页接口,具体内容如下

BookPage.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package com.horse.util;
import java.text.DecimalFormat;
import java.util.Vector;
 
import com.horse.bean.Chapter;
 
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.text.format.Time;
 
/**
 * 这个类的目的是为在看书翻页时,需要进行的动作提供接口。
 * 包括翻向下一页,翻向上一页。在翻到每章最后一页时,如果后面还有章节就继续翻向下一章节,没有就向用户显示已读完。
 * 在翻向上一章节时,如果前面还有章节,就翻到上一章节,没有就向用户显示,这已经是第一章节。
 *
 * 在直觉上认为这个应该只设置成一个接口,因为只需向视图层提供动作接口,也就是本类应属于模型层。则其设置为一个借口可能也合适。
 * 但是如果设置成一个接口,那么接口的实现类,有多个都要保存的数据。那么为了代码重用,抽象类可能比接口更加合适。 上面是个人分析,可能不是很合适。
 *
 * @author MJZ
 *
 */
public class BookPage {
 // configuration information
 private int screenWidth; // 屏幕宽度
 private int screenHeight; // 屏幕高度
 private int fontSize; // 字体大小
 private int lineHgight; //每行的高度
 private int marginWidth = 15; // 左右与边缘的距离
 private int marginHeight = 15; // 上下与边缘的距离
 private int textColor; // 字体颜色
 private Bitmap bgBitmap; // 背景图片
 private int bgColor; // 背景颜色
 
 private Paint paint;
 private Paint paintBottom;
 private int visibleWidth; // 屏幕中可显示文本的宽度
 private int visibleHeight;
 private Chapter chapter; // 需要处理的章节对象
 private Vector linesVe; // 将章节內容分成行,并将每页按行存储到vector对象中
 private int lineCount; // 一个章节在当前配置下一共有多少行
 
 private String content;
 private int chapterLen; // 章节的长度
 // private int curCharPos; // 当前字符在章节中所在位置
 private int charBegin; // 每一页第一个字符在章节中的位置
 private int charEnd; // 每一页最后一个字符在章节中的位置
 private boolean isfirstPage;
 private boolean islastPage;
 
 private Vector> pagesVe;
 int pageNum;
 
 /**
 * 在新建一个BookPage对象时,需要向其提供数据,以支持屏幕翻页功能。
 *
 * @param screenWidth
 *      屏幕宽度,用来计算每行显示多少字
 * @param screenHeight
 *      屏幕高度,用来计算每页显示多少行
 * @param chapter
 *      章节对象
 */
 public BookPage(int screenWidth, int screenHeight, Chapter chapter) {
 this.screenHeight = screenHeight;
 this.screenWidth = screenWidth;
 this.chapter = chapter;
 init();
 }
 
 /**
 * 初始最好按照定义变量的顺序来初始化,统一。在将来需要修改某个变量的时候,容易找到。 对代码维护应该也很有用吧。
 */
 protected void init() {
 bgBitmap = null;
 bgColor = 0xffff9e85;
 textColor = Color.BLACK;
 content = chapter.getContent();
 chapterLen = content.length();
 // curCharPos = 0;
 charBegin = 0;
 charEnd = 0;
 fontSize = 30;
 lineHgight = fontSize + 8;
 linesVe = new Vector();
 
 paint = new Paint(Paint.ANTI_ALIAS_FLAG);
 paint.setTextAlign(Align.LEFT);
 paint.setTextSize(fontSize);
 paint.setColor(textColor);
  
 paintBottom = new Paint(Paint.ANTI_ALIAS_FLAG);
 paintBottom.setTextAlign(Align.LEFT);
 paintBottom.setTextSize(fontSize / 2);
 paintBottom.setColor(textColor);
 
 visibleWidth = screenWidth - marginWidth * 2;
 visibleHeight = screenHeight - marginHeight * 2;
 lineCount = visibleHeight / lineHgight - 2;
 isfirstPage = true;
 islastPage = false;
 pagesVe = new Vector>();
 pageNum = -1;
 slicePage();
 }
 
 public Vector getCurPage() {
 return linesVe;
 }
 
 protected void slicePage() {
 pagesVe.clear();
 int curPos = 0;
 while (curPos < chapterLen) {
  Vector lines = new Vector();
  charBegin = curPos;
  while (lines.size() < lineCount && curPos < chapterLen) {
  int i = content.indexOf("\n", curPos);
  String paragraphStr = content.substring(curPos, i);
  // curCharPos += i;
  if (curPos == i)
   lines.add("");
 
  while (paragraphStr.length() > 0) {
   int horSize = paint.breakText(paragraphStr, true,
    visibleWidth, null);
   lines.add(paragraphStr.substring(0, horSize));
   paragraphStr = paragraphStr.substring(horSize);
   curPos += horSize;
   if (lines.size() > lineCount)
   break;
  }
  // 如果是把一整段读取完的话,需要给当前位置加1
  if (paragraphStr.length() == 0)
   curPos += "\n".length();
  }
  pagesVe.add(lines);
 }
 
 }
 
 /**
 * 翻到下一页
 */
 public boolean nextPage() {
 if (isLastPage()) {
  if (!nextChapter()) // 如果已经到本书末尾,那么不能继续执行翻页代码
  return false;
 }
 /*
  * Vector lines = new Vector(); charBegin = charEnd;
  * while (lines.size() < lineCount && charEnd < chapterLen) { int i =
  * content.indexOf("\n", charEnd); String paragraphStr =
  * content.substring(charEnd, i); // curCharPos += i; if (charEnd == i)
  * lines.add("");
  *
  * while (paragraphStr.length() > 0) { int horSize =
  * paint.breakText(paragraphStr, true, visibleWidth, null);
  * lines.add(paragraphStr.substring(0, horSize)); paragraphStr =
  * paragraphStr.substring(horSize); charEnd += horSize; if (lines.size()
  * > lineCount) break; } // 如果是把一整段读取完的话,需要给当前位置加1 if
  * (paragraphStr.length() == 0) charEnd += "\n".length(); } linesVe =
  * lines;
  */
 linesVe = pagesVe.get(++pageNum);
 return true;
 }
 
 /**
 * 翻到上一页
 */
 public boolean prePage() {
 if (isFirstPage()) {
  if (!preChapter()) // 如果已经到本书第一章,就不能继续执行翻页代码
  return false;
 }
 /*
  * Vector lines = new Vector(); String backStr =
  * content.substring(0, charBegin); charEnd = charBegin;
  *
  * while (lines.size() < lineCount && charBegin > 0) { int i =
  * backStr.lastIndexOf("\n"); if(i == -1) i = 0; String paragraphStr =
  * backStr.substring(i, charBegin); Vector vet = new
  * Vector(lines);
  *
  * // if(charBegin == i)lines.add("");
  *
  * while (paragraphStr.length() > 0) { int horSize =
  * paint.breakText(paragraphStr, true, visibleWidth, null);
  * lines.add(paragraphStr.substring(0, horSize)); paragraphStr =
  * paragraphStr.substring(horSize); charBegin -= horSize; if
  * (lines.size() > lineCount) break; }
  *
  * backStr = content.substring(0, charBegin); int j = -1; for (String
  * line : vet) lines.insertElementAt(line, ++j); } linesVe = lines;
  */
 linesVe = pagesVe.get(--pageNum);
 return true;
 }
 
 /**
 * 跳到下一章,若返回值为false,则当前章节已经为最后一章
 */
 public boolean nextChapter() {
 int order = chapter.getOrder();
 Chapter tempChapter = IOHelper.getChapter(order + 1);
 if (tempChapter == null)
  return false;
 chapter = tempChapter;
 content = chapter.getContent();
 chapterLen = content.length();
 // curCharPos = 0;
 charBegin = 0;
 charEnd = 0;
 slicePage();
 pageNum = -1;
 return true;
 }
 
 /**
 * 跳到上一章,若返回值为false,则当前章节已经为第一章
 */
 public boolean preChapter() {
 int order = chapter.getOrder();
 Chapter tempChapter = IOHelper.getChapter(order - 1);
 if (tempChapter == null)
  return false;
 chapter = tempChapter;
 content = chapter.getContent();
 chapterLen = content.length();
 // curCharPos = chapterLen;
 charBegin = chapterLen;
 charEnd = chapterLen;
 slicePage();
 pageNum = pagesVe.size();
 return true;
 }
 
 public boolean isFirstPage() {
 if (pageNum <= 0)
  return true;
 return false;
 }
 
 public boolean isLastPage() {
 if (pageNum >= pagesVe.size() - 1)
  return true;
 return false;
 }
 
 public void draw(Canvas c) {
 if (linesVe.size() == 0)
  nextPage();
 if (linesVe.size() > 0) {
  if (bgBitmap == null)
  c.drawColor(bgColor);
  else
  c.drawBitmap(bgBitmap, 0, 0, null);
 
  int y = marginHeight;
  for (String line : linesVe) {
  y += lineHgight;
  c.drawText(line, marginWidth, y, paint);
  }
 }
 
 // float percent = (float) (charBegin * 1.0 / chapterLen);
 float percent = (float) ((pageNum + 1) * 1.0 / pagesVe.size());
 DecimalFormat df = new DecimalFormat("#0.0");
 String percetStr = df.format(percent * 100) + "%";
 
 Time time = new Time();
 time.setToNow();
 String timeStr;
 if (time.minute < 10)
  timeStr = "" + time.hour + " : 0" + time.minute;
 else
  timeStr = "" + time.hour + " : " + time.minute;
 
 int pSWidth = (int) paintBottom.measureText("99.9%") + 2;
 int titWidth = (int) paintBottom.measureText(chapter.getTitle());
 
  
 c.drawText(timeStr, marginWidth / 2, screenHeight - 5, paintBottom);
 c.drawText(chapter.getTitle(), screenWidth / 2 - titWidth / 2,
  screenHeight - 5, paintBottom);
 
声明:文章版权归原作者所有 部分文章转自互联网 如有侵权请联系 [邮箱地址] 删除

路过

雷人

握手

鲜花

鸡蛋

最新评论