Android custom controls Create paging interface detail Code _android

Source: Internet
Author: User
Tags drawtext

The purpose of this class is to provide an interface for the actions that need to be made when reading a book, and to create a paging interface using the Android custom control, as follows

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;
 /** * The purpose of this class is to provide an interface for the actions that need to be made when reading pages. * Including a page down, turn up a page.
 When you turn to the last page of each chapter, if you continue with the next chapter, you will not show the user that you have finished reading it.
 * When flipping through a section, if there are chapters before, turn to the previous chapter, no show to the user, this is already the first chapter. * Intuitively, this should only be set to an interface, because only the action interface is provided to the view layer, which means that this class belongs to the model layer.
 It may also be appropriate to set it as an excuse. * But if set to an interface, then the implementation of the interface class, there are multiple data to be saved. For code reuse, abstract classes may be more appropriate than interfaces.
 The above is a personal analysis, may not be very appropriate.  * * * @author MJZ * */public class BookPage {//configuration information private int screenwidth;//Screen width private int screenheight; Screen height private int fontsize; Font size private int linehgight; The height of each row is private int marginwidth = 15; The distance between the left and the edge is private int marginheight = 15; Up and down the distance with the edge of the private int textcolor; Font color private Bitmap bgbitmap; background picture private int bgcolor; Background color priVate Paint Paint;
 Private Paint Paintbottom; private int visiblewidth;
 The width of the text can be displayed in the screen private int visibleheight; Private Chapter Chapter; The chapter object to be dealt with is private vector<string> linesve; The chapters are divided into rows, and each page is stored by row into the vector object, private int linecount;
 How many lines of private String content are there in a chapter in the current configuration? private int Chapterlen; Length of chapter//private int curcharpos; The position of the current character in the chapter private int charbegin; The position of the first character of each page in the chapter private int charend;
 The position of the last character in each page in the chapter private Boolean isfirstpage;

 Private Boolean islastpage;
 Private vector<vector<string>> pagesve;

 int pagenum;
 /** * When you create a new BookPage object, you need to provide it with data to support the screen-paging feature.      * @param screenwidth * screen width, used to calculate how many words per line @param screenheight * screen height, to calculate how many rows per page * @param Chapter *
 Chapter Object */Public bookpage (int screenwidth, int screenheight, Chapter Chapter) {this.screenheight = ScreenHeight;
 This.screenwidth = ScreenWidth;
 This.chapter = Chapter;
 Init (); The/** * Initial is best initialized in order to define the variables, uniform. It's easy to find when you need to modify a variable in the future. The maintenance of code should alsoIt's very useful.
 * * 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;
  //If you are finished reading a whole paragraph, you need to add 1 if (paragraphstr.length () = = 0) CurPos + = "\ n". Length ();
 } pagesve.add (lines); 
  }/** * Turn to Next page/public boolean nextPage () {if (Islastpage ()) {if (!nextchapter ())//If you are already at the end of this book, you cannot continue the paging code
 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; //If you are reading a whole paragraph, you need to add 1 if * (paragraphstr.length () = = 0) charend + = "\ n" to the current position. Length ();
  } linesve = * lines;
 * * linesve = Pagesve.get (++pagenum);
 return true; /** * Turn to the previous page/public boolean prepage () {if (Isfirstpage ()) {if (!prechapter ())//If you have already been in chapter one of this book, you cannot continue with the paging code re
 Turn 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;
 /** * Skip to the next chapter, if the return value is False, the current chapter is already the last chapter */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;
 /** * Skip to the previous chapter, if the return value is False, the current chapter is already the first chapter */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;
 The public boolean isfirstpage () {if (pagenum <= 0) is 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 is = new Time ();
 Time.settonow ();
 String Timestr;
 if (Time.minute <) 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;
/**
 * Chapter information, including title and content, and order
 * @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;
 }
 
 
}

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.