当前位置: 移动技术网 > 移动技术>移动开发>Android > android自定义控件创建翻页接口详细代码

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

2019年07月24日  | 移动技术网移动技术  | 我要评论

本文分享的这个类的目的是为在看书翻页时,需要进行的动作提供接口,利用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;
 }
 
 
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网