Android开发 翻书效果实例
Android开发当中,尤其是在做电子书等方面的项目时,难免会遇到翻书效果的使用。下面就来讲解下,实现翻书效果的实例。
eBook继承FrameLayout,好处在于FrameLayout有图层效果,后添加的View类能覆盖前面的View。
初始化:定义一个LinearLayout的成员变量mView,将page.xmlinflate成View分别用leftPage,rightPage引用,并初始化其数据,将leftPage,rightPage通过addView添加到mView,然后将mView添加到eBook。在eBook里定义一个私有类BookViewextendsView。并定义成员变量BookViewmBookView;最后将mBookView添加的eBook中,这样,mView中的内容为书面内容,mBookView中的内容为特效内容。后续手势动作:可将各种手势的特效动作画于mBookView的画布中。
Java代码:
packageeoe.book;
importAndroid.app.Activity;
importandroid.os.Bundle;
importandroid.util.Log;
importandroid.view.View;
importandroid.widget.ImageView;
publicclassBookextendsActivity{
/**Calledwhentheactivityisfirstcreated.*/
eBookmBook;
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mBook=(eBook)findViewById(R.id.my_book);
mBook.addLayoutRecForPage(R.layout.page,21);
mBook.setListener(neweBook.Listener(){
publicvoidonPrevPage(){
updateContent();
}
publicvoidonNextPage(){
updateContent();
}
publicvoidonInit(){
updateContent();
}
});
}
privatevoidupdateContent(){
intindex=mBook.getIndexForLeftPage();
Viewleft=mBook.getLeftPage(),right=mBook.getRightPage();
Viewnext1=mBook.getNextPage1(),next2=mBook.getNextPage2();
Viewprev1=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();
}
privatevoidsetImg(Viewv,intindex){
if(index>=0&&index<20){
ImageViewimg=(ImageView)v.findViewById(R.id.book_img);
if(img==null)return;
Log.d("eBook","setImg");
switch(index%6){
case0:
img.setImageResource(R.drawable.p1);
break;
case1:
img.setImageResource(R.drawable.p2);
break;
case2:
img.setImageResource(R.drawable.p3);
break;
case3:
img.setImageResource(R.drawable.p4);
break;
case4:
img.setImageResource(R.drawable.p5);
break;
case5:
img.setImageResource(R.drawable.p6);
break;
default:
break;
}
}
}
}
我们在来看看xml代码:
java代码:
main.xml文件:
<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.book.eBookandroid:id="@+id/my_book"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
page.xml文件:
<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns: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">
<ImageViewandroid: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>
控件TelEdit.java代码:
java代码:
packageeoe.book;
importandroid.content.Context;
importandroid.graphics.Canvas;
importandroid.graphics.Color;
importandroid.graphics.Paint;
importandroid.util.AttributeSet;
importandroid.view.WindowManager;
importandroid.widget.EditText;
publicclassTelEditextendsEditText{
ContextmContext;
publicTelEdit(Contextcontext){
super(context);
mContext=context;
}
publicTelEdit(Contextcontext,AttributeSetattrs){
super(context,attrs);
mContext=context;
}
publicTelEdit(Contextcontext,AttributeSetattrs,intdefStyle){
super(context,attrs,defStyle);
mContext=context;
}
protectedvoidonDraw(Canvascanvas){
WindowManagerwm=(WindowManager)mContext.getSystemService("window");
intwindowWidth=wm.getDefaultDisplay().getWidth();
intwindowHeight=wm.getDefaultDisplay().getHeight();
Paintpaint=newPaint();
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.BLACK);
intpaddingTop=getPaddingTop();
intpaddingBottom=getPaddingBottom();
intscrollY=getScrollY();
intscrollX=getScrollX()+windowWidth;
intinnerHeight=scrollY+getHeight()-paddingBottom;
intlineHeight=getLineHeight();
intbaseLine=scrollY+(lineHeight-((scrollY-paddingTop)%lineHeight));
intx=8;
while(baseLine<innerHeight){
canvas.drawLine(x,baseLine,scrollX-x,baseLine,paint);
baseLine+=lineHeight;
}
super.onDraw(canvas);
}
}
eBook.java文件部分代码:
java代码:
packageeoe.book;
importjava.util.ArrayList;
importjava.util.Date;
importjava.util.List;
importandroid.content.Context;
importandroid.graphics.Bitmap;
importandroid.graphics.Canvas;
importandroid.graphics.Color;
importandroid.graphics.LinearGradient;
importandroid.graphics.Paint;
importandroid.graphics.Path;
importandroid.graphics.Point;
importandroid.graphics.PorterDuffXfermode;
importandroid.graphics.Shader;
importandroid.graphics.PorterDuff.Mode;
importandroid.util.AttributeSet;
importandroid.util.Log;
importandroid.view.GestureDetector;
importandroid.view.LayoutInflater;
importandroid.view.MotionEvent;
importandroid.view.View;
importandroid.view.GestureDetector.OnGestureListener;
importandroid.widget.FrameLayout;
importandroid.widget.LinearLayout;
publicclasseBookextendsFrameLayout{
publicstaticfinalStringLOG_TAG="eBook";
List<Integer>myRecPages;
inttotalPageNum;
ContextmContext;
booleanhasInit=false;
finalintdefaultWidth=600,defaultHeight=400;
intcontentWidth=0;
intcontentHeight=0;
ViewleftPage,rightPage,llPage,lrPage,rrPage,rlPage;
LinearLayoutmView;
bookViewmBookView;
booleancloseBook=false;
privateenumCorner{
LeftTop,RightTop,LeftBottom,RightBottom,None
};
privateCornermSelectCorner;
finalintclickCornerLen=250*250;//50dip
floatscrollX=0,scrollY=0;
intindexPage=0;
privateenumState{
ABOUT_TO_ANIMATE,ANIMATING,ANIMATE_END,READY,TRACKING
};
privateStatemState;
privatePointaniStartPos;
privatePointaniStopPos;
privateDateaniStartTime;
privatelonganiTime=2000;
privatelongtimeOffset=900;
ListenermListener;
privateGestureDetectormGestureDetector;
privateBookOnGestureListenermGestureListener;
publiceBook(Contextcontext){
super(context);
Init(context);
}
publiceBook(Contextcontext,AttributeSetattrs){
super(context,attrs);
Init(context);
}
}