android自定义控件创建翻页接口详细代码
本文分享的这个类的目的是为在看书翻页时,需要进行的动作提供接口,利用android自定义控件创建翻页接口,具体内容如下
BookPage.java
packagecom.horse.util;
importjava.text.DecimalFormat;
importjava.util.Vector;
importcom.horse.bean.Chapter;
importandroid.graphics.Bitmap;
importandroid.graphics.Canvas;
importandroid.graphics.Color;
importandroid.graphics.Paint;
importandroid.graphics.Paint.Align;
importandroid.text.format.Time;
/**
*这个类的目的是为在看书翻页时,需要进行的动作提供接口。
*包括翻向下一页,翻向上一页。在翻到每章最后一页时,如果后面还有章节就继续翻向下一章节,没有就向用户显示已读完。
*在翻向上一章节时,如果前面还有章节,就翻到上一章节,没有就向用户显示,这已经是第一章节。
*
*在直觉上认为这个应该只设置成一个接口,因为只需向视图层提供动作接口,也就是本类应属于模型层。则其设置为一个借口可能也合适。
*但是如果设置成一个接口,那么接口的实现类,有多个都要保存的数据。那么为了代码重用,抽象类可能比接口更加合适。上面是个人分析,可能不是很合适。
*
*@authorMJZ
*
*/
publicclassBookPage{
//configurationinformation
privateintscreenWidth;//屏幕宽度
privateintscreenHeight;//屏幕高度
privateintfontSize;//字体大小
privateintlineHgight;//每行的高度
privateintmarginWidth=15;//左右与边缘的距离
privateintmarginHeight=15;//上下与边缘的距离
privateinttextColor;//字体颜色
privateBitmapbgBitmap;//背景图片
privateintbgColor;//背景颜色
privatePaintpaint;
privatePaintpaintBottom;
privateintvisibleWidth;//屏幕中可显示文本的宽度
privateintvisibleHeight;
privateChapterchapter;//需要处理的章节对象
privateVector<String>linesVe;//将章节內容分成行,并将每页按行存储到vector对象中
privateintlineCount;//一个章节在当前配置下一共有多少行
privateStringcontent;
privateintchapterLen;//章节的长度
//privateintcurCharPos;//当前字符在章节中所在位置
privateintcharBegin;//每一页第一个字符在章节中的位置
privateintcharEnd;//每一页最后一个字符在章节中的位置
privatebooleanisfirstPage;
privatebooleanislastPage;
privateVector<Vector<String>>pagesVe;
intpageNum;
/**
*在新建一个BookPage对象时,需要向其提供数据,以支持屏幕翻页功能。
*
*@paramscreenWidth
*屏幕宽度,用来计算每行显示多少字
*@paramscreenHeight
*屏幕高度,用来计算每页显示多少行
*@paramchapter
*章节对象
*/
publicBookPage(intscreenWidth,intscreenHeight,Chapterchapter){
this.screenHeight=screenHeight;
this.screenWidth=screenWidth;
this.chapter=chapter;
init();
}
/**
*初始最好按照定义变量的顺序来初始化,统一。在将来需要修改某个变量的时候,容易找到。对代码维护应该也很有用吧。
*/
protectedvoidinit(){
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=newVector<String>();
paint=newPaint(Paint.ANTI_ALIAS_FLAG);
paint.setTextAlign(Align.LEFT);
paint.setTextSize(fontSize);
paint.setColor(textColor);
paintBottom=newPaint(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=newVector<Vector<String>>();
pageNum=-1;
slicePage();
}
publicVector<String>getCurPage(){
returnlinesVe;
}
protectedvoidslicePage(){
pagesVe.clear();
intcurPos=0;
while(curPos<chapterLen){
Vector<String>lines=newVector<String>();
charBegin=curPos;
while(lines.size()<lineCount&&curPos<chapterLen){
inti=content.indexOf("\n",curPos);
StringparagraphStr=content.substring(curPos,i);
//curCharPos+=i;
if(curPos==i)
lines.add("");
while(paragraphStr.length()>0){
inthorSize=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);
}
}
/**
*翻到下一页
*/
publicbooleannextPage(){
if(isLastPage()){
if(!nextChapter())//如果已经到本书末尾,那么不能继续执行翻页代码
returnfalse;
}
/*
*Vector<String>lines=newVector<String>();charBegin=charEnd;
*while(lines.size()<lineCount&&charEnd<chapterLen){inti=
*content.indexOf("\n",charEnd);StringparagraphStr=
*content.substring(charEnd,i);//curCharPos+=i;if(charEnd==i)
*lines.add("");
*
*while(paragraphStr.length()>0){inthorSize=
*paint.breakText(paragraphStr,true,visibleWidth,null);
*lines.add(paragraphStr.substring(0,horSize));paragraphStr=
*paragraphStr.substring(horSize);charEnd+=horSize;if(lines.size()
*>lineCount)break;}//如果是把一整段读取完的话,需要给当前位置加1if
*(paragraphStr.length()==0)charEnd+="\n".length();}linesVe=
*lines;
*/
linesVe=pagesVe.get(++pageNum);
returntrue;
}
/**
*翻到上一页
*/
publicbooleanprePage(){
if(isFirstPage()){
if(!preChapter())//如果已经到本书第一章,就不能继续执行翻页代码
returnfalse;
}
/*
*Vector<String>lines=newVector<String>();StringbackStr=
*content.substring(0,charBegin);charEnd=charBegin;
*
*while(lines.size()<lineCount&&charBegin>0){inti=
*backStr.lastIndexOf("\n");if(i==-1)i=0;StringparagraphStr=
*backStr.substring(i,charBegin);Vector<String>vet=new
*Vector<String>(lines);
*
*//if(charBegin==i)lines.add("");
*
*while(paragraphStr.length()>0){inthorSize=
*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);intj=-1;for(String
*line:vet)lines.insertElementAt(line,++j);}linesVe=lines;
*/
linesVe=pagesVe.get(--pageNum);
returntrue;
}
/**
*跳到下一章,若返回值为false,则当前章节已经为最后一章
*/
publicbooleannextChapter(){
intorder=chapter.getOrder();
ChaptertempChapter=IOHelper.getChapter(order+1);
if(tempChapter==null)
returnfalse;
chapter=tempChapter;
content=chapter.getContent();
chapterLen=content.length();
//curCharPos=0;
charBegin=0;
charEnd=0;
slicePage();
pageNum=-1;
returntrue;
}
/**
*跳到上一章,若返回值为false,则当前章节已经为第一章
*/
publicbooleanpreChapter(){
intorder=chapter.getOrder();
ChaptertempChapter=IOHelper.getChapter(order-1);
if(tempChapter==null)
returnfalse;
chapter=tempChapter;
content=chapter.getContent();
chapterLen=content.length();
//curCharPos=chapterLen;
charBegin=chapterLen;
charEnd=chapterLen;
slicePage();
pageNum=pagesVe.size();
returntrue;
}
publicbooleanisFirstPage(){
if(pageNum<=0)
returntrue;
returnfalse;
}
publicbooleanisLastPage(){
if(pageNum>=pagesVe.size()-1)
returntrue;
returnfalse;
}
publicvoiddraw(Canvasc){
if(linesVe.size()==0)
nextPage();
if(linesVe.size()>0){
if(bgBitmap==null)
c.drawColor(bgColor);
else
c.drawBitmap(bgBitmap,0,0,null);
inty=marginHeight;
for(Stringline:linesVe){
y+=lineHgight;
c.drawText(line,marginWidth,y,paint);
}
}
//floatpercent=(float)(charBegin*1.0/chapterLen);
floatpercent=(float)((pageNum+1)*1.0/pagesVe.size());
DecimalFormatdf=newDecimalFormat("#0.0");
StringpercetStr=df.format(percent*100)+"%";
Timetime=newTime();
time.setToNow();
StringtimeStr;
if(time.minute<10)
timeStr=""+time.hour+":0"+time.minute;
else
timeStr=""+time.hour+":"+time.minute;
intpSWidth=(int)paintBottom.measureText("99.9%")+2;
inttitWidth=(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);
}
publicvoidsetBgBitmap(BitmapbMap){
bgBitmap=Bitmap.createScaledBitmap(bMap,screenWidth,screenHeight,
true);
}
}
com.horse.bean.Chapter
packagecom.horse.bean;
/**
*章节信息,包括标题和内容,及顺序
*@authorMJZ
*
*/
publicclassChapter{
privateStringtitle;
privateStringcontent;
privateintorder;
publicStringgetTitle(){
returntitle;
}
publicvoidsetTitle(Stringtitle){
this.title=title;
}
publicStringgetContent(){
returncontent;
}
publicvoidsetContent(Stringcontent){
this.content=content;
}
publicintgetOrder(){
returnorder;
}
publicvoidsetOrder(intorder){
this.order=order;
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。