使用Gallery做出图片层叠效果

今天在网上看到一个非常酷的效果图,今天特来分享给大家。好了直接上图吧。   

运行后的效果如下:
 

其中有两段核心源码,一是让Gallery产生层叠效果的部分:
01 private void transformImageBitmap(BorderImageView child, Transformation t, float rotationAngle) {
02         mCamera.save();
03         final Matrix imageMatrix = t.getMatrix();
04         final int imageHeight = child.getLayoutParams().height;
05         final int imageWidth = child.getLayoutParams().width;
06         final float rotation = Math.abs(rotationAngle);
07         float zoomAmount = mMaxZoom + 2 * rotation;
08   
09         //缩放比例,720P为1,1080P为1.5
10         float scale = this.getContext().getResources().getInteger(R.integer.gallery_scale) / 10.0f;
11   
12         mCamera.translate((float) (rotationAngle * 2.8 * scale), (float) (rotation * 0.1),
13                 zoomAmount);
14   
15         //设置透明度
16         child.setAlpha((int) (255 - 255 * 0.1 * Math.pow(rotation / mMaxRotationAngle, 2)));
17   
18         //设置背景
19   
20         int padding = (int) Math.abs(mMaxRotationAngle * 0.1f * scale);
21         child.setBorderwidth(padding);
22         child.setBorderRadius(padding);
23         if (rotation / mMaxRotationAngle == 1) {
24             child.setBorderColor(this.getContext().getResources()
25                     .getColor(R.color.layer_middle_border));
26         } else if (rotation / mMaxRotationAngle == 2) {
27             child.setBorderColor(this.getContext().getResources()
28                     .getColor(R.color.layer_bottom_border));
29         } else {
30             child.setBorderColor(Color.TRANSPARENT);
31         }
32   
33         mCamera.getMatrix(imageMatrix);
34         imageMatrix.preTranslate(-(imageWidth / 2), -(imageHeight / 2));
35         imageMatrix.postTranslate((imageWidth / 2), (imageHeight / 2));
36         mCamera.restore();
37   
38     }

二是控制图片渲染顺序的部分:
01 protected int getChildDrawingOrder(int childCount, int i) {
02         int selectedPos = this.getSelectedItemPosition();
03         if (childCount <= 2) {
04             return super.getChildDrawingOrder(childCount, i);
05         } else if (childCount == 3) {
06             if (selectedPos < (childCount / 2)) {
07                 return childCount - i - 1;
08             } else {
09                 return i;
10             }
11   
12         } else if (childCount == 4) {
13             if (selectedPos < (childCount / 2)) {
14                 if (i < 1) {
15                     return i;
16                 }
17                 return childCount - i;
18             } else {
19                 if (i < (childCount / 2)) {
20                     return i;
21                 }
22                 return (childCount - i) + 1;
23             }
24   
25         } else {
26             if (i < (childCount / 2)) {
27                 return i;
28             }
29             return (childCount - i - 1) + (childCount / 2);
30         }
31     }

除此之外的代码应该没什么特别的,直接上源码吧,欢迎大家下载学习
 GalleryDemo.rar (2.08 MB) 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值