Author: Wallace Wang Email: wallage@qq.com
Disclaimer: This article is my original work and is only for learning and use.
Reprinted address:
Http://wallage.blog.163.com/blog/static/1738962420108915833584/
:
Book. Java file: Package com. Book; import Android. App. activity;
Import Android. OS. Bundle;
Import Android. util. log;
Import Android. View. view;
Import Android. widget. imageview; public class Book extends activity {
/** Called when the activity is first created .*/
Ebook mbook;
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
Mbook = (ebook) findviewbyid (R. Id. my_book );
Mbook. addlayoutrecforpage (R. layout. Page, 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 <20 ){
Imageview IMG = (imageview) v. findviewbyid (R. Id. book_img );
If (IMG = NULL) return;
Log. D ("ebook", "set IMG ");
Switch (index % 6 ){
Case 0:
IMG. setimageresource (R. drawable. P1 );
Break;
Case 1:
IMG. setimageresource (R. drawable. P2 );
Break;
Case 2:
IMG. setimageresource (R. drawable. P3 );
Break;
Case 3:
IMG. setimageresource (R. drawable. P4 );
Break;
Case 4:
IMG. setimageresource (R. drawable. P5 );
Break;
Case 5:
IMG. setimageresource (R. drawable. p6 );
Break;
Default:
Break;
}
}
}
} Main. xml file: <? XML version = "1.0" encoding = "UTF-8"?>
<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: Orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">
<Com. Book. ebook Android: Id = "@ + ID/my_book"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"/>
</Linearlayout> page. xml file: <? XML version = "1.0" encoding = "UTF-8"?>
<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/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 = "@ drawable/P1"/>
<Com. Book. teledit
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"
Android: textcolor = "#000000"
Android: layout_height = "fill_parent"
Android: paddingtop = "30dip"
Android: layout_weight = "1"/>
</Linearlayout> Control teledit. Java code: Package com. Book; import Android. content. context;
Import Android. Graphics. Canvas;
Import Android. Graphics. color;
Import Android. Graphics. paint;
Import Android. util. attributeset;
Import Android. View. windowmanager;
Import Android. widget. edittext; public class teledit extends edittext {
Context mcontext; Public teledit (context ){
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 ){
Windowmanager WM = (windowmanager) mcontext. getsystemservice ("window ");
Int defaults wwidth = WM. getdefadisplay display (). getwidth ();
Int optional wheight = WM. getdefaultdisplay (). getheight (); paint = new paint ();
Paint. setstyle (paint. style. Fill );
Paint. setcolor (color. Black); int paddingtop = getpaddingtop ();
Int paddingbottom = getpaddingbottom (); int scrolly = getscrolly ();
Int scrollx = getscrollx () + descriwwidth;
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 );
}
} Part of the ebook. Java file code: Package com. Book; import java. util. arraylist;
Import java. util. date;
Import java. util. List; import Android. content. context;
Import Android. Graphics. Bitmap;
Import Android. Graphics. Canvas;
Import Android. Graphics. color;
Import Android. Graphics. lineargradient;
Import Android. Graphics. paint;
Import Android. Graphics. path;
Import Android. Graphics. Point;
Import Android. Graphics. porterduduxfermode;
Import Android. Graphics. shader;
Import Android. Graphics. porterduff. mode;
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 (context ){
Super (context );
Init (context );
} Public ebook (context, attributeset attrs ){
Super (context, attrs );
Init (context );
}... Omitted}
The general implementation method of this control: ebook inherits framelayout. The advantage is that framelayout has a layer effect, and the added View class can overwrite the previous view. Initialization: defines a linearlayout member variable mview. XML inflate is referenced by leftpage and rightpage respectively in view, and its data is initialized. Add leftpage and rightpage to mview through addview, and then add mview to ebook. Define a private class bookview extends view in the ebook. Define the member variable bookview mbookview. Finally, add the ebook to the mbookview. In this way, the content in the mview is written, and the content in the mbookview is the special effect content. Subsequent gesture actions: You can draw special effects of various gestures on the canvas of mbookview.