本文分享的这个类的目的是为在看书翻页时,需要进行的动作提供接口,利用Android自定义控件创建翻页接口,具体内容如下 BookPage.java package com.
本文分享的这个类的目的是为在看书翻页时,需要进行的动作提供接口,利用Android自定义控件创建翻页接口,具体内容如下
BookPage.java
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;
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<String> 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<Vector<String>> pagesVe;
int pageNum;
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<String>();
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<Vector<String>>();
pageNum = -1;
slicePage();
}
public Vector<String> getCurPage() {
return linesVe;
}
protected void slicePage() {
pagesVe.clear();
int curPos = 0;
while (curPos < chapterLen) {
Vector<String> lines = new Vector<String>();
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;
}
linesVe = pagesVe.get(++pageNum);
return true;
}
public boolean prePage() {
if (isFirstPage()) {
if (!preChapter()) // 如果已经到本书第一章,就不能继续执行翻页代码
return false;
}
linesVe = pagesVe.get(--pageNum);
return true;
}
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;
}
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);
c.drawText(percetStr, screenWidth - pSWidth, screenHeight - 5, paintBottom);
}
public void setBgBitmap(Bitmap bMap) {
bgBitmap = Bitmap.createScaledBitmap(bMap, screenWidth, screenHeight,
true);
}
}
com.horse.bean.Chapter
package com.horse.bean;
public class Chapter {
private String title;
private String content;
private int order;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public int getOrder() {
return order;
}
public void setOrder(int order) {
this.order = order;
}
}
您可能感兴趣的文章:android中图片翻页效果简单的实现方法解析Android中实现滑动翻页之ViewFlipper的使用详解Android实现阅读APP平移翻页效果Android利用悬浮按钮实现翻页效果Android通过手势实现答题器翻页效果基于Android实现3D翻页效果Android程序开发ListView+JSON+异步网络图片加载+滚动翻页的例子(图片能缓存,图片不错乱)android ViewPager实现滑动翻页效果实例代码Android基于ListView实现类似QQ空间的滚动翻页与滚动加载效果Android使用手势实现翻页效果
--结束END--
本文标题: android自定义控件创建翻页接口详细代码
本文链接: https://lsjlt.com/news/24563.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-01-21
2023-10-28
2023-10-28
2023-10-27
2023-10-27
2023-10-27
2023-10-27
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0