編輯:關於Android編程
本文分享的這個類的目的是為在看書翻頁時,需要進行的動作提供接口,利用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;
/**
* 這個類的目的是為在看書翻頁時,需要進行的動作提供接口。
* 包括翻向下一頁,翻向上一頁。在翻到每章最後一頁時,如果後面還有章節就繼續翻向下一章節,沒有就向用戶顯示已讀完。
* 在翻向上一章節時,如果前面還有章節,就翻到上一章節,沒有就向用戶顯示,這已經是第一章節。
*
* 在直覺上認為這個應該只設置成一個接口,因為只需向視圖層提供動作接口,也就是本類應屬於模型層。則其設置為一個借口可能也合適。
* 但是如果設置成一個接口,那麼接口的實現類,有多個都要保存的數據。那麼為了代碼重用,抽象類可能比接口更加合適。 上面是個人分析,可能不是很合適。
*
* @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<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;
/**
* 在新建一個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<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;
}
/*
* Vector<String> lines = new Vector<String>(); 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<String> lines = new Vector<String>(); 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<String> vet = new
* Vector<String>(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);
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;
/**
* 章節信息,包括標題和內容,及順序
* @author MJZ
*
*/
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 自定義圓圈進度並顯示百分比例控件(純代碼實現)
首先,感謝公司能給我閒暇的時間,來穩固我的技術,讓我不斷的去探索研究,在此不勝感激。 先不說實現功能,上圖看看效果 這個是續上一次水平變色進度條的有一個全新的控件,理論實
紅米3s怎麼插卡 紅米3S SIM卡安裝圖文教程
紅米3S是小米在14號推出的新機,紅米3S配備金屬機身、指紋識別、八核主流配置、長續航等特點,性價比很高。隨著紅米3S開啟預約,不僅之後米粉用戶就可以用上這
Android實現果凍滑動效果的控件
前言在微信是的處理方法是讓用戶滑動,但最終還是回滾到最初的地方,這樣的效果很生動(畢竟成功還是取決於細節)。那麼在安卓我們要怎麼弄呢。下面為大家介紹一下JellyScro
android動態設置app當前運行語言
android開發中有時候碰到切換語言的需求,這時候需要通過代碼動態改變當前運行語言。 package com.example.androidtest; import