今天在网上看到一个非常酷的效果图,今天特来分享给大家。好了直接上图吧。
运行后的效果如下:
其中有两段核心源码,一是让Gallery产生层叠效果的部分:
二是控制图片渲染顺序的部分:
除此之外的代码应该没什么特别的,直接上源码吧,欢迎大家下载学习
GalleryDemo.rar (2.08 MB)
运行后的效果如下:
其中有两段核心源码,一是让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)