Android翻书翻页(支持硬翻软翻)
来源:互联网 发布:全天时时彩计划数据 编辑:程序博客网 时间:2024/06/05 09:58
有一次公司有个需求,要求做翻页效果,支持硬翻和软翻,发现网上根本没有一个支持硬翻,软翻也没有几个效果好的,楼主就自己实现了一下.
软翻页是修改Harism/android_page_curl ,这个软翻页还是不错的,如果只要做软翻页可以直接用那个,或者把楼主的代码修改
下面贴一下硬翻页的类,最后会给源代码地址
public class HardSurface { private static final int VERTEX_COUNT = 2; private float mRotate = 0.0f; // 旋转角度 private float mtransaction = 0.0f; // 移动距离 private Bitmap[] coverBitmap; // 两面图片 private FloatBuffer verticeBuffer; private FloatBuffer textureFrontBuffer; private FloatBuffer textureBackBuffer; private int[] textures; public HardSurface() { verticeBuffer = ByteBuffer.allocateDirect(VERTEX_COUNT * 8 * 4).order(ByteOrder.nativeOrder()).asFloatBuffer(); verticeBuffer.position(0); textureFrontBuffer = ByteBuffer.allocateDirect(VERTEX_COUNT * 8 * 4).order(ByteOrder.nativeOrder()).asFloatBuffer(); textureFrontBuffer.position(0); textureBackBuffer = ByteBuffer.allocateDirect(VERTEX_COUNT * 8 * 4).order(ByteOrder.nativeOrder()).asFloatBuffer(); textureBackBuffer.position(0); } public boolean isNotHaveCover() { return coverBitmap == null; } /** * 设置旋转度数 * * @param rotate */ public float setRotate(float rotate) { if (Float.isNaN(rotate)) { return mRotate; } if (rotate < -180) { this.mRotate = -180; } else if (rotate > 0) { this.mRotate = 0; } this.mRotate = rotate; return mRotate; } /** * 设置旋转移动距离 解决旋转时候移动的问题 * * @param transaction */ public void setTransaction(float transaction) { this.mtransaction = transaction; } public void draw(GL10 gl) { gl.glPushMatrix(); gl.glEnable(gl.GL_TEXTURE_2D); gl.glEnable(gl.GL_BLEND); gl.glEnable(gl.GL_CULL_FACE); gl.glEnableClientState(gl.GL_VERTEX_ARRAY); gl.glEnableClientState(gl.GL_TEXTURE_COORD_ARRAY); gl.glVertexPointer(VERTEX_COUNT, gl.GL_FLOAT, 0, verticeBuffer); gl.glTranslatef(mtransaction, 0, 0); gl.glRotatef(mRotate, 0, 1, 0); loadTexture(gl); gl.glCullFace(GL10.GL_BACK); drawFrontFace(gl); gl.glCullFace(GL10.GL_FRONT); drawBackFace(gl); gl.glDisable(GL10.GL_TEXTURE_2D); gl.glDisable(GL10.GL_BLEND); gl.glDisable(GL10.GL_CULL_FACE); gl.glDisableClientState(GL10.GL_VERTEX_ARRAY); gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY); gl.glPopMatrix(); } /** * 画后面 * * @param gl */ private void drawBackFace(GL10 gl) { if (textures != null) { gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[1]); } gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBackBuffer); gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4); } /** * 画前面 * * @param gl */ private void drawFrontFace(GL10 gl) { if (textures != null) { gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]); } gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureFrontBuffer); gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4); } /** * 加载纹理 * * @param gl */ private void loadTexture(GL10 gl) { if (textures == null) { textures = new int[2]; gl.glGenTextures(2, textures, 0); } gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST); for (int i = 0; i < 2; i++) { gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[i]); gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST); gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_NEAREST); gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_CLAMP_TO_EDGE); gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_CLAMP_TO_EDGE); GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, coverBitmap[i], 0); } } /** * 设置矩阵 * * @param coverRect */ public void setRect(RectF coverRect) { verticeBuffer.put(coverRect.left); verticeBuffer.put(coverRect.bottom); verticeBuffer.put(coverRect.right); verticeBuffer.put(coverRect.bottom); verticeBuffer.put(coverRect.left); verticeBuffer.put(coverRect.top); verticeBuffer.put(coverRect.right); verticeBuffer.put(coverRect.top); verticeBuffer.position(0); } /** * 设置前页面纹理矩阵 * * @param vertices */ public void setFrontTextureRect(float[] vertices) { float left = vertices[0]; float bottom = vertices[1]; float right = vertices[2]; float top = vertices[3]; textureFrontBuffer.put(left); textureFrontBuffer.put(top); textureFrontBuffer.put(right); textureFrontBuffer.put(top); textureFrontBuffer.put(left); textureFrontBuffer.put(bottom); textureFrontBuffer.put(right); textureFrontBuffer.put(bottom); textureFrontBuffer.position(0); } /** * 设置前页面纹理矩阵 * * @param vertices */ public void setFrontTextureRect(RectF vertices) { float left = vertices.left; float bottom = vertices.top; float right = vertices.right; float top = vertices.bottom; textureFrontBuffer.put(left); textureFrontBuffer.put(top); textureFrontBuffer.put(right); textureFrontBuffer.put(top); textureFrontBuffer.put(left); textureFrontBuffer.put(bottom); textureFrontBuffer.put(right); textureFrontBuffer.put(bottom); textureFrontBuffer.position(0); } /** * 设置后页面纹理矩阵 * * @param vertices */ public void setBackTextureRect(float[] vertices) { float left = vertices[0]; float bottom = vertices[1]; float right = vertices[2]; float top = vertices[3]; textureBackBuffer.put(right); textureBackBuffer.put(top); textureBackBuffer.put(left); textureBackBuffer.put(top); textureBackBuffer.put(right); textureBackBuffer.put(bottom); textureBackBuffer.put(left); textureBackBuffer.put(bottom); textureBackBuffer.position(0); } /** * 设置后页面纹理矩阵 * * @param vertices */ public void setBackTextureRect(RectF vertices) { float left = vertices.left; float bottom = vertices.top; float right = vertices.right; float top = vertices.bottom; textureBackBuffer.put(right); textureBackBuffer.put(top); textureBackBuffer.put(left); textureBackBuffer.put(top); textureBackBuffer.put(right); textureBackBuffer.put(bottom); textureBackBuffer.put(left); textureBackBuffer.put(bottom); textureBackBuffer.position(0); } /** * 设置图片 * * @param bitmap */ public void setCoverBitmap(Bitmap[] bitmap) { if (bitmap != null) { coverBitmap = new Bitmap[2]; this.coverBitmap = bitmap; } }}
主要是运用一些数学算法和opengl的一些相关知识,这个类是主要实现硬板翻页效果,其他都是一些渲染逻辑之类的.
源代码下载地址
1 0
- Android翻书翻页(支持硬翻软翻)
- Android_Page Curl:Android平台上的翻书翻页
- jQuery实现图片3D折叠,翻页,翻书效果
- 【翻译+山寨】html5+css3+js实现翻页翻书效果
- page curling(翻页 翻屏)
- Android模拟翻书效果
- Android模拟翻书效果
- Android 自定义控件 eBook 翻书效果
- Android自定义控件eBook翻书效果
- Android 自定义控件 eBook 翻书效果
- Android 自定义控件 eBook 翻书效果
- Android电子书翻书效果(一)
- Android 自定义控件 eBook 翻书效果
- Android翻书效果-双页模式
- Android电子书翻书效果(一)
- 一个炫酷的opengles2.0翻页效果(安卓上opengles2.0f 翻书效果附源码)
- android 模仿书的翻页(附带源码)
- 3D翻转翻页特效之左右翻和上下翻
- TCP包头详解
- 千位数换算的封装
- DBus如何传送和接收数据,包括结构体
- 380. Insert Delete GetRandom O(1)
- 免费馅饼(动态规划)
- Android翻书翻页(支持硬翻软翻)
- MS SQL varchar索引与查询
- java 成长之路
- 记忆网络RNN、LSTM与GRU
- Android-网络通信框架Volley使用详解
- 494. Target Sum
- 静态分析工具
- 正确评估SQL数据库性能,你必须知道的原理和方法!
- 给自己看的Redis