热门标签 | HotTags
当前位置:  开发笔记 > Android > 正文

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

这篇文章主要为大家介绍了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 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;

 /**
 * 在新建一个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();

 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  lines = new Vector();
  charBegin = curPos;
  while (lines.size()  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 lines = new Vector(); charBegin = charEnd;
  * while (lines.size()  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 lines = new Vector(); String backStr =
  * content.substring(0, charBegin); charEnd = charBegin;
  * 
  * while (lines.size()  0) { int i =
  * backStr.lastIndexOf("\n"); if(i == -1) i = 0; String paragraphStr =
  * backStr.substring(i, charBegin); Vector vet = new
  * Vector(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 九宫格布局详解及实现:人人网应用示例
    本文深入探讨了人人网Android应用中独特的九宫格布局设计,解析其背后的GridView实现原理,并提供详细的代码示例。这种布局方式不仅美观大方,而且在现代Android应用中较为少见,值得开发者借鉴。 ... [详细]
  • 深入解析Android自定义View面试题
    本文探讨了Android Launcher开发中自定义View的重要性,并通过一道经典的面试题,帮助开发者更好地理解自定义View的实现细节。文章不仅涵盖了基础知识,还提供了实际操作建议。 ... [详细]
  • Linux 系统启动故障排除指南:MBR 和 GRUB 问题
    本文详细介绍了 Linux 系统启动过程中常见的 MBR 扇区和 GRUB 引导程序故障及其解决方案,涵盖从备份、模拟故障到恢复的具体步骤。 ... [详细]
  • 本文介绍了如何使用jQuery根据元素的类型(如复选框)和标签名(如段落)来获取DOM对象。这有助于更高效地操作网页中的特定元素。 ... [详细]
  • 本文将详细介绍如何使用剪映应用中的镜像功能,帮助用户轻松实现视频的镜像效果。通过简单的步骤,您可以快速掌握这一实用技巧。 ... [详细]
  • 深入理解Cookie与Session会话管理
    本文详细介绍了如何通过HTTP响应和请求处理浏览器的Cookie信息,以及如何创建、设置和管理Cookie。同时探讨了会话跟踪技术中的Session机制,解释其原理及应用场景。 ... [详细]
  • 本文介绍如何在 Xcode 中使用快捷键和菜单命令对多行代码进行缩进,包括右缩进和左缩进的具体操作方法。 ... [详细]
  • 本文介绍了一款用于自动化部署 Linux 服务的 Bash 脚本。该脚本不仅涵盖了基本的文件复制和目录创建,还处理了系统服务的配置和启动,确保在多种 Linux 发行版上都能顺利运行。 ... [详细]
  • 在Linux系统中配置并启动ActiveMQ
    本文详细介绍了如何在Linux环境中安装和配置ActiveMQ,包括端口开放及防火墙设置。通过本文,您可以掌握完整的ActiveMQ部署流程,确保其在网络环境中正常运行。 ... [详细]
  • Android 渐变圆环加载控件实现
    本文介绍了如何在 Android 中创建一个自定义的渐变圆环加载控件,该控件已在多个知名应用中使用。我们将详细探讨其工作原理和实现方法。 ... [详细]
  • 如何在WPS Office for Mac中调整Word文档的文字排列方向
    本文将详细介绍如何使用最新版WPS Office for Mac调整Word文档中的文字排列方向。通过这些步骤,用户可以轻松更改文本的水平或垂直排列方式,以满足不同的排版需求。 ... [详细]
  • 本文总结了在使用Ionic 5进行Android平台APK打包时遇到的问题,特别是针对QRScanner插件的改造。通过详细分析和提供具体的解决方法,帮助开发者顺利打包并优化应用性能。 ... [详细]
  • 理解存储器的层次结构有助于程序员优化程序性能,通过合理安排数据在不同层级的存储位置,提升CPU的数据访问速度。本文详细探讨了静态随机访问存储器(SRAM)和动态随机访问存储器(DRAM)的工作原理及其应用场景,并介绍了存储器模块中的数据存取过程及局部性原理。 ... [详细]
  • 360SRC安全应急响应:从漏洞提交到修复的全过程
    本文详细介绍了360SRC平台处理一起关键安全事件的过程,涵盖从漏洞提交、验证、排查到最终修复的各个环节。通过这一案例,展示了360在安全应急响应方面的专业能力和严谨态度。 ... [详细]
  • 几何画板展示电场线与等势面的交互关系
    几何画板是一款功能强大的物理教学软件,具备丰富的绘图和度量工具。它不仅能够模拟物理实验过程,还能通过定量分析揭示物理现象背后的规律,尤其适用于难以在实际实验中展示的内容。本文将介绍如何使用几何画板演示电场线与等势面之间的关系。 ... [详细]
author-avatar
潇潇雨621715
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有