Android custom Control ebook Implementation of the book effect of the example detailed _android

Source: Internet
Author: User

The example of this article describes the Android custom control ebook the way to achieve the effect of flipping books. Share to everyone for your reference, specific as follows:

Effect Chart: file:

Import Android.os.Bundle;
Import Android.util.Log;
Import Android.view.View;
Import Android.widget.ImageView;
  The public class book extends activity {/** Called when the ' activity is ' A-created EBook Mbook;
    public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
    Setcontentview (R.layout.main);
    Mbook = (EBook) Findviewbyid (;
    Mbook.addlayoutrecforpage (,21);
  Mbook.setlistener (New Ebook.listener () {public void Onprevpage () {updatecontent ();
  public void Onnextpage () {updatecontent ();
  public void OnInit () {updatecontent ();
   private void Updatecontent () {int index = mbook.getindexforleftpage ();
    View left = Mbook.getleftpage (), right = Mbook.getrightpage ();
    View next1 = Mbook.getnextpage1 (), next2 = Mbook.getnextpage2 ();
    View prev1 = Mbook.getprevpage1 (), Prev2 = Mbook.getprevpage2 (); if (left!= null) setimg (left,iNDEX);
    if (right!= null) setimg (right,index+1);
    if (next1!= null) setimg (next1,index+2);
    if (next2!= null) setimg (NEXT2,INDEX+3);
    if (prev1!= null) setimg (PREV1,INDEX-1);
    if (Prev2!= null) setimg (PREV2,INDEX-2); 
  Mbook.invalidate (); } private void Setimg (View v, int index) {if (index >= 0 && Index <) {ImageView img = (imageview)
  Ndviewbyid (R.ID.BOOK_IMG);
  if (img = null) return;
  LOG.D ("EBook", "set IMG");
  Switch (index%6) {case 0:img.setimageresource (R.DRAWABLE.P1);
  Case 1:img.setimageresource (R.DRAWABLE.P2);
  Case 2:img.setimageresource (R.DRAWABLE.P3);
  Case 3:img.setimageresource (R.DRAWABLE.P4);
  Case 4:img.setimageresource (R.DRAWABLE.P5);
  Case 5:img.setimageresource (R.DRAWABLE.P6);


Main.xml file:

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"" Res/android "
  android:orientation=" vertical "
  android:layout_width=" fill_parent "
  android:layout_" height= "Fill_parent" >
 < android:id= "@+id/my_book" android:layout_width= "Fill_parent"
  android:layout_height=" fill_parent "/>

Page.xml file:

 <?xml version= "1.0" encoding= "Utf-8"?> <linearlayout "xmlns:android=" "android:orientation=" vertical "android:layout_width=" Fill_parent "Android: layout_height= "Fill_parent" android:padding= "20dip" android:background= "#FFFFDD" > <imageview android:layout_
  Width= "Fill_parent" android:id= "@+id/book_img" android:layout_height= "Fill_parent" android:layout_weight= "1"
  Android:scaletype= "Fitxy" android:src= ""/> <
  Android:id= "@+id/book_text" android:layout_width= "fill_parent" android:background= "#ffffdd" android:gravity= "Top" Android:typeface= "sans" android:capitalize= "sentences" android:linespacingextra= "5dip" android:textsize= "15dip" a Ndroid:textcolor= "#000000" android:layout_height= "fill_parent" android:paddingtop= "30dip" android:layout_weight= " 1 "/> </LinearLayout> 

Control Code:

Import Android.content.Context;
Import Android.util.AttributeSet;
Import Android.view.WindowManager;
Import Android.widget.EditText;
 public class Teledit extends EditText {context mcontext;
 Public Teledit {Super (context);
 Mcontext = context;
 Public Teledit (context, AttributeSet attrs) {Super (context, attrs);
 Mcontext = context;
 Public Teledit (context, AttributeSet attrs, int defstyle) {Super (context, attrs, Defstyle);
 Mcontext = context;
 } protected void OnDraw (Canvas Canvas) {WindowManager wm = (WindowManager) mcontext.getsystemservice ("window");
 int windowwidth = Wm.getdefaultdisplay (). GetWidth ();
 int windowheight = Wm.getdefaultdisplay (). GetHeight ();
 Paint Paint = new Paint ();
 Paint.setstyle (Paint.Style.FILL);
 Paint.setcolor (Color.Black);
 int paddingtop = Getpaddingtop (); int paddingbottom = Getpaddingbottom ();
 int scrolly = getscrolly ();
 int scrollx = GETSCROLLX () + windowwidth;
 int innerheight = scrolly + getheight ()-paddingbottom;
 int lineheight = Getlineheight ();
 int baseLine = scrolly + (Lineheight-((scrolly-paddingtop)% lineheight));
 int x = 8;
  while (BaseLine < innerheight) {canvas.drawline (x, BaseLine, Scrollx-x, baseLine, paint);
 BaseLine + = Lineheight;
 } super.ondraw (canvas);

} File Part code:

Import java.util.ArrayList;
Import Java.util.Date;
Import java.util.List;
Import Android.content.Context;
Import Android.util.AttributeSet;
Import Android.util.Log;
Import Android.view.GestureDetector;
Import Android.view.LayoutInflater;
Import android.view.MotionEvent;
Import Android.view.View;
Import Android.view.GestureDetector.OnGestureListener;
Import Android.widget.FrameLayout;
Import Android.widget.LinearLayout;
 public class EBook extends framelayout{public static final String Log_tag = "EBook";
 List<integer> myrecpages;
 int totalpagenum;
 Context Mcontext;
 Boolean hasinit = false; Final int defaultwidth = 600, DefaultHeight = 400;
 int contentwidth = 0;
 int contentheight = 0;
 View Leftpage,rightpage,llpage,lrpage,rrpage,rlpage;
 LinearLayout Mview;
 Bookview Mbookview;
 Boolean closebook = false;
 Private enum Corner {lefttop, righttop, Leftbottom, Rightbottom, None};
 Private Corner Mselectcorner; Final int clickcornerlen = 250*250;
 50dip float scrollx = 0,scrolly = 0;
 int indexpage = 0;
 Private enum State {about_to_animate, animating, Animate_end, READY, tracking};
 Private State mstate;
 Private point Anistartpos;
 Private point Anistoppos;
 Private Date Anistarttime;
 Private long anitime = 2000;
 Private long timeoffset = 900;
 Listener Mlistener;
 Private Gesturedetector Mgesturedetector;
 Private Bookongesturelistener Mgesturelistener;
 Public EBook {Super (context);
 Init (context);
 Public EBook (context, AttributeSet attrs) {Super (context, attrs);
 Init (context); }


The control is generally implemented by the method:

ebook Inherits Framelayout, and the advantage is that Framelayout has layer effect, and the view class added can overwrite the view above.

Initialization: Defines a linearlayout member variable Mview, page.xml inflate into view with Leftpage,rightpage reference and initializes its data, Leftpage, Rightpage is added to the Mview through AddView, and Mview is added to the ebook. Define a private class Bookview extends View in ebook. and define the member variable Bookview Mbookview, and finally add the Mbookview ebook, so that the content in Mview is written and the content in Mbookview is the special effects content.

Follow-up gesture action: can be a variety of gestures of the special effects of the action to draw on the Mbookview canvas.

For more information on Android-related content readers can view the site: "Android graphics and image processing skills summary", "Android Development introduction and Advanced Course", "Android debugging techniques and common problems solution summary", " Android Multimedia How-to Summary (audio, video, audio, etc), summary of Android Basic components usage, Android View tips Summary, Android layout layout tips and a summary of Android controls usage

I hope this article will help you with the Android program.

Related Article

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: 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.