Android自定义控件eBook实现翻书效果实例详解

本文实例讲述了Android自定义控件eBook实现翻书效果的方法。分享给大家供大家参考,具体如下:

效果图:

Book.java文件:

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文件:

<?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文件:

<?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="http://wallage.blog.163.com/blog/@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代码:

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 context) { super(context); mContext = context; } public TelEdit(Context context, AttributeSet attrs) { super(context, attrs); mContext = context; } public TelEdit(Context 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); } }

eBook.java文件部分代码:

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.PorterDuffXfermode; 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 context) { super(context); Init(context); } public eBook(Context context, AttributeSet attrs) { super(context, attrs); Init(context); } ...省略 }

该控件大致实现方法:

eBook继承FrameLayout,好处在于FrameLayout有图层效果,后添加的View类能覆盖前面的View。

初始化:定义一个LinearLayout的成员变量mView,将page.xml inflate 成View分别用leftPage,rightPage引用,并初始化其数据,将leftPage,rightPage通过addView添加到mView,然后将mView添加到eBook。在eBook里定义一个私有类BookView extends View。 并定义成员变量 BookView mBookView; 最后将mBookView添加的eBook中,这样,mView中的内容为书面内容,mBookView中的内容为特效内容。

后续手势动作:可将各种手势的特效动作画于mBookView的画布中。

更多关于Android相关内容感兴趣的读者可查看本站专题:《Android图形与图像处理技巧总结》、《Android开发入门与进阶教程》、《Android调试技巧与常见问题解决方法汇总》、《Android多媒体操作技巧汇总(音频,视频,录音等)》、《Android基本组件用法总结》、《Android视图View技巧总结》、《Android布局layout技巧总结》及《Android控件用法总结》

希望本文所述对大家Android程序设计有所帮助。

时间: 2024-07-30 12:36:17

Android自定义控件eBook实现翻书效果实例详解的相关文章

Android自定义控件eBook实现翻书效果实例详解_Android

本文实例讲述了Android自定义控件eBook实现翻书效果的方法.分享给大家供大家参考,具体如下: 效果图: Book.java文件: 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 extend

Android开发高仿课程表的布局实例详解_Android

先说下这个demo,这是一个模仿课程表的布局文件,虽然我是个菜鸟,但我还是想留给学习的人一些例子,先看下效果   然后再来看一下我们学校的app 布局分析 先上一张划分好了的布局图 首先整个页面放在一个LinearLayout布局下面,分为上面和下面两个部分,下面一个是显示课程表的详细信息 1:这个没什么好讲的,就是直接一个LinearLayout布局,然后将控件一个TextView用来显示年份,一个View用来当作竖线,一个Spinner用来显示选择周数 2:这个是显示星期几的部件,是我自定义

Android开发高仿课程表的布局实例详解

先说下这个demo,这是一个模仿课程表的布局文件,虽然我是个菜鸟,但我还是想留给学习的人一些例子,先看下效果 然后再来看一下我们学校的app 布局分析 先上一张划分好了的布局图 首先整个页面放在一个LinearLayout布局下面,分为上面和下面两个部分,下面一个是显示课程表的详细信息 1:这个没什么好讲的,就是直接一个LinearLayout布局,然后将控件一个TextView用来显示年份,一个View用来当作竖线,一个Spinner用来显示选择周数 2:这个是显示星期几的部件,是我自定义的V

Android自定义View中attrs.xml的实例详解

Android自定义View中attrs.xml的实例详解 我们在自定义View的时候通常需要先完成attrs.xml文件 在values中定义一个attrs.xml 然后添加相关属性 这一篇先详细介绍一下attrs.xml的属性. <?xml version="1.0" encoding="utf-8"?> <resources> //自定义属性名,定义公共属性 <attr name="titleText" for

Android 图片存入系统相册更新显示实例详解

Android 图片存入系统相册更新显示实例详解 在开发android的过程中,我们避免不了可能会涉及到做一个自定义相册或则会去本地创建一个文件夹来存储我们需要的图片.拿相册来说,比如我们创建一个test的文件夹,拍完一张照片后存储到这个指定的test文件夹里,然后在相册里面显示出来,就像微信的效果一样.拍完即可立即显示.但是,在实际开发过程中我们保存完一张图片后并不能立即更新显示出来这个图片,需要我们重启手机才能在系统相册中显示出来. 这里先提供一个插入系统图库的方法: MediaStore.

Android顶部(toolbar)搜索框实现的实例详解

Android顶部(toolbar)搜索框实现的实例详解 本文介绍两种SearchView的使用情况,一种是输入框和搜索结果不在一个activity中,另一种是在一个activity中. 首先编写toolbar的布局文件 toolbar中图标在menu文件下定义一个布局文件实现 示例代码: <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.

Android实现定时器的五种方法实例详解

一.Timer Timer是Android直接启动定时器的类,TimerTask是一个子线程,方便处理一些比较复杂耗时的功能逻辑,经常与handler结合使用. 跟handler自身实现的定时器相比,Timer可以做一些复杂的处理,例如,需要对有大量对象的list进行排序,在TimerTask中执行不会阻塞子线程,常常与handler结合使用,在处理完复杂耗时的操作后,通过handler来更新UI界面. timer.schedule(task, delay,period); task: Time

Android 监听软键盘状态的实例详解

Android 监听软键盘状态的实例详解 近日遇到要检测软键盘是否显示或隐藏的问题,搜了一下网上,最后找到一个很简单的,记录一下. activityRoot是activity的根view,就是xml里面的第一个view,给它设置一个id. final View activityRootView = findViewById(R.id.activityRoot); activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(ne

Android jni调试打印char阵列的实例详解

Android jni调试打印char阵列的实例详解 前言: 在android开发中,用jni有时候需要打印某一个字符串的二进制格式输出,比较友好的输出格式是一个四列,八列,十六列的矩阵格式.类似在错误删除野指针时出现如下错误: pid: 2721, tid: 3005, name: pool-5-thread-5 >>> onxmaps.hunt <<< signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr dea