Android 使用Gallery实现3D相册(附效果图+Demo源码)

  public boolean createReflectedForAdapter()

  {

  final int reflectionGap = 4;

  final int Height = 200;

  int index = 0;

  for (Map map : list)

  {

  Integer id = (Integer) map.get("image");

  // 获取原始图片

  Bitmap originalImage = BitmapFactory.decodeResource(mContext.getResources(), id);

  int width = originalImage.getWidth();

  int height = originalImage.getHeight();

  float scale = Height / (float)height;

  Matrix sMatrix = new Matrix();

  sMatrix.postScale(scale, scale);

  Bitmap miniBitmap = Bitmap.createBitmap(originalImage, 0, 0,

  originalImage.getWidth(), originalImage.getHeight(), sMatrix, true);

  //是否原图片数据,节省内存

  originalImage.recycle();

  int mwidth = miniBitmap.getWidth();

  int mheight = miniBitmap.getHeight();

  Matrix matrix = new Matrix();

  // 图片矩阵变换(从低部向顶部的倒影)

  matrix.preScale(1, -1);

  // 截取原图下半部分

  Bitmap reflectionImage = Bitmap.createBitmap(miniBitmap, 0, mheight/2, mwidth, mheight/2, matrix, false);

  // 创建倒影图片(高度为原图3/2)

  Bitmap bitmapWithReflection = Bitmap.createBitmap(mwidth, (mheight + mheight / 2), Config.ARGB_8888);

  // 绘制倒影图(原图 + 间距 + 倒影)

  Canvas canvas = new Canvas(bitmapWithReflection);

  // 绘制原图

  canvas.drawBitmap(miniBitmap, 0, 0, null);

  Paint paint = new Paint();

  // 绘制原图与倒影的间距

  canvas.drawRect(0, mheight, mwidth, mheight + reflectionGap, paint);

  // 绘制倒影图

  canvas.drawBitmap(reflectionImage, 0, mheight + reflectionGap, null);

  paint = new Paint();

  // 线性渐变效果

  LinearGradient shader = new LinearGradient(0, miniBitmap.getHeight(), 0, bitmapWithReflection.getHeight()

  + reflectionGap, 0x70ffffff, 0x00ffffff, TileMode.CLAMP);

  paint.setShader(shader);

  // 倒影遮罩效果

  paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));

  // 绘制倒影的阴影效果

  canvas.drawRect(0, mheight, mwidth, bitmapWithReflection.getHeight() + reflectionGap, paint);

  ImageView imageView = new ImageView(mContext);

  // 设置倒影图片

  imageView.setImageBitmap(bitmapWithReflection);

  imageView.setLayoutParams(new GalleryView.LayoutParams((int)(width * scale),

  (int)(mheight * 3 / 2.0 + reflectionGap)));

  imageView.setScaleType(ScaleType.MATRIX);

  mImages[index++] = imageView;

  }

  return true;

  }