栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 移动开发 > Android

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

Android 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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;


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;

 
 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;
 }
 
 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;
 }
 
 
}

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/162076.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号