Android gallery 3D效果
在看了iOS上面的CoverFlow后,感觉效果真的不错,就想在android上面实现一个,这个程序在网上参考了一此核心的代码,当然我添加了一些其他的东西,废话不多话,先看效果,不然就是无图无真相了。

其实实现这个效果很简单,下面作一个简单的介绍
这个基本思路是:
1,创建一个源图一样的图,利用martrix将图片旋转180度。这个倒影图的高是源图的一半。
package com.lee.gallery3d.utils;import android.graphics.Bitmap;import android.graphics.Bitmap.Config;import android.graphics.Canvas;import android.graphics.LinearGradient;import android.graphics.Matrix;import android.graphics.Paint;import android.graphics.PixelFormat;import android.graphics.PorterDuffXfermode;import android.graphics.Shader.TileMode;import android.graphics.drawable.Drawable;public class BitmapUtil{ public static Bitmap createReflectedBitmap(Bitmap srcBitmap) { if (null == srcBitmap) { return null; } // The gap between the reflection bitmap and original bitmap. final int REFLECTION_GAP = 4; int srcWidth = srcBitmap.getWidth(); int srcHeight = srcBitmap.getHeight(); int reflectionWidth = srcBitmap.getWidth(); int reflectionHeight = srcBitmap.getHeight() / 2; if (0 == srcWidth || srcHeight == 0) { return null; } // The matrix Matrix matrix = new Matrix(); matrix.preScale(1, -1); try { // The reflection bitmap, width is same with original's, height is half of original's. Bitmap reflectionBitmap = Bitmap.createBitmap( srcBitmap, 0, srcHeight / 2, srcWidth, srcHeight / 2, matrix, false); if (null == reflectionBitmap) { return null; } // Create the bitmap which contains original and reflection bitmap. Bitmap bitmapWithReflection = Bitmap.createBitmap( reflectionWidth, srcHeight + reflectionHeight + REFLECTION_GAP, Config.ARGB_8888); if (null == bitmapWithReflection) { return null; } // Prepare the canvas to draw stuff. Canvas canvas = new Canvas(bitmapWithReflection); // Draw the original bitmap. canvas.drawBitmap(srcBitmap, 0, 0, null); // Draw the reflection bitmap. canvas.drawBitmap(reflectionBitmap, 0, srcHeight + REFLECTION_GAP, null); Paint paint = new Paint(); paint.setAntiAlias(true); LinearGradient shader = new LinearGradient( 0, srcHeight, 0, bitmapWithReflection.getHeight() + REFLECTION_GAP, 0x70FFFFFF, 0x00FFFFFF, TileMode.MIRROR); paint.setShader(shader); paint.setXfermode(new PorterDuffXfermode(android.graphics.PorterDuff.Mode.DST_IN)); // Draw the linear shader. canvas.drawRect( 0, srcHeight, srcWidth, bitmapWithReflection.getHeight() + REFLECTION_GAP, paint); return bitmapWithReflection; } catch (Exception e) { e.printStackTrace(); } return null; }}