├── .gitignore
├── LICENSE
├── README.md
├── README_CN.md
├── app
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src
│ ├── androidTest
│ └── java
│ │ └── ryane
│ │ └── com
│ │ └── gallery
│ │ └── ExampleInstrumentedTest.java
│ ├── main
│ ├── AndroidManifest.xml
│ ├── java
│ │ └── ryane
│ │ │ └── com
│ │ │ └── gallery
│ │ │ ├── BlurBitmapUtil.java
│ │ │ ├── MainActivity.java
│ │ │ └── RecyclerAdapter.java
│ └── res
│ │ ├── drawable-xxhdpi
│ │ ├── beauty1.jpg
│ │ ├── beauty10.jpg
│ │ ├── beauty11.jpg
│ │ ├── beauty12.jpg
│ │ ├── beauty13.jpg
│ │ ├── beauty2.jpg
│ │ ├── beauty3.jpg
│ │ ├── beauty4.jpg
│ │ ├── beauty5.jpg
│ │ ├── beauty6.jpg
│ │ ├── beauty7.jpg
│ │ ├── beauty8.jpg
│ │ ├── beauty9.jpg
│ │ ├── ic_change.png
│ │ ├── photo_nba1.jpg
│ │ ├── photo_nba2.jpg
│ │ ├── photo_nba3.jpg
│ │ ├── photo_nba4.jpg
│ │ ├── photo_nba5.jpg
│ │ ├── photo_nba6.jpg
│ │ ├── photo_nba7.jpg
│ │ ├── photo_nba8.jpg
│ │ └── photo_nba9.jpg
│ │ ├── layout
│ │ ├── activity_main.xml
│ │ └── item_gallery.xml
│ │ ├── mipmap-hdpi
│ │ ├── ic_launcher.png
│ │ └── ic_launcher_round.png
│ │ ├── mipmap-mdpi
│ │ ├── ic_launcher.png
│ │ └── ic_launcher_round.png
│ │ ├── mipmap-xhdpi
│ │ ├── ic_launcher.png
│ │ └── ic_launcher_round.png
│ │ ├── mipmap-xxhdpi
│ │ ├── ic_launcher.png
│ │ └── ic_launcher_round.png
│ │ ├── mipmap-xxxhdpi
│ │ ├── ic_launcher.png
│ │ └── ic_launcher_round.png
│ │ ├── values-v19
│ │ └── styles.xml
│ │ ├── values-v21
│ │ └── styles.xml
│ │ └── values
│ │ ├── colors.xml
│ │ ├── strings.xml
│ │ └── styles.xml
│ └── test
│ └── java
│ └── ryane
│ └── com
│ └── gallery
│ └── ExampleUnitTest.java
├── build.gradle
├── gradle.properties
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── rv-gallery
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src
│ ├── androidTest
│ └── java
│ │ └── com
│ │ └── ryan
│ │ └── rv_gallery
│ │ └── ExampleInstrumentedTest.java
│ ├── main
│ ├── AndroidManifest.xml
│ ├── java
│ │ └── com
│ │ │ └── ryan
│ │ │ └── rv_gallery
│ │ │ ├── AnimManager.java
│ │ │ ├── GalleryItemDecoration.java
│ │ │ ├── GalleryRecyclerView.java
│ │ │ ├── ScrollManager.java
│ │ │ └── util
│ │ │ ├── DLog.java
│ │ │ └── OsUtil.java
│ └── res
│ │ └── values
│ │ ├── strings.xml
│ │ └── value.xml
│ └── test
│ └── java
│ └── com
│ └── ryan
│ └── rv_gallery
│ └── ExampleUnitTest.java
├── settings.gradle
└── 用RecyclerView做一个小清新的Gallery效果.md
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | .idea
4 | /local.properties
5 | /.idea/workspace.xml
6 | /.idea/libraries
7 | .DS_Store
8 | /build
9 | /captures
10 | .externalNativeBuild
11 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "[]"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright [yyyy] [name of copyright owner]
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
202 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # README #
2 |
3 | [](https://bintray.com/ryanlijianchang/maven/RecyclerView-Gallery)
4 | [](https://github.com/ryanlijianchang/Recyclerview-Gallery)
5 | [](https://github.com/ryanlijianchang/Recyclerview-Gallery)
6 |
7 | [中文版文档](https://github.com/ryanlijianchang/Recyclerview-Gallery/blob/master/README_CN.md)
8 |
9 | This library shows you a gallery using RecyclerView.
10 |
11 | 
12 |
13 | 
14 |
15 |
16 | # Usage #
17 |
18 | First step, add dependence in your `build.gradle`.
19 |
20 | compile 'com.ryan.rv_gallery:rv-gallery:1.1.2'
21 |
22 | Second step, using `GalleryRecyclerView` in your layout file.
23 |
24 |
33 |
34 |
35 |
36 |
41 |
42 |
43 |
44 | Third step, init your GalleryRecyclerView in your java code just like using the normal RecyclerView. Note that you must use the LinearLayoutManager as your LayoutManager. At the same time, you must set the orientation like `HORIZONTAL` or `VERTICAL`, to make your `GalleryRecyclerView` scroll horizontally or vertically.
45 |
46 |
47 | GalleryRecyclerView mRecyclerView = findViewById(R.id.rv_list);
48 | RecyclerAdapter adapter = new RecyclerAdapter(getApplicationContext(), getDatas());
49 | mRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
50 | mRecyclerView.setAdapter(adapter);
51 |
52 | Finally, set some params of `GalleryRecyclerView` if your need, and you must use the `setUp()` method to make the `GalleryRecyclerView` to work.
53 |
54 | mRecyclerView
55 | // set scroll speed(pixel/s)
56 | .initFlingSpeed(9000)
57 | // set page distance and visible distance of the nearby.
58 | .initPageParams(0, 40)
59 | // set the animation factor
60 | .setAnimFactor(0.1f)
61 | // set animation type. you can choose AnimManager.ANIM_BOTTOM_TO_TOP or AnimManager.ANIM_TOP_TO_BOTTOM
62 | .setAnimType(AnimManager.ANIM_BOTTOM_TO_TOP)
63 | // set click listener
64 | .setOnItemClickListener(this)
65 | // set whether auto play
66 | .autoPlay(false)
67 | // set auto play intervel
68 | .intervalTime(2000)
69 | // set default position
70 | .initPosition(1)
71 | // finally call method
72 | .setUp();
73 |
74 | You also can release `GalleryRecyclerView` if you need
75 |
76 |
77 | @Override
78 | protected void onDestroy() {
79 | super.onDestroy();
80 | if (mRecyclerView != null) {
81 | // release
82 | mRecyclerView.release();
83 | }
84 | }
85 |
86 | # API #
87 |
88 | **Java API**
89 |
90 | 1. `initFlingSpeed(int speed)`:set scroll speed(pixel/s)
91 | 2. `setAnimFactor(float factor)`:set the animation factor
92 | 3. `setAnimType(int type)`:set animation type. you can choose `AnimManager.ANIM_BOTTOM_TO_TOP` or `AnimManager.ANIM_TOP_TO_BOTTOM`
93 | 4. `setOnItemClickListener(OnItemClickListener mListener)`:set click listener
94 | 5. `initPageParams(int pageMargin, int leftPageVisibleWidth)`:set page distance and visible distance of the nearby.
95 | 6. `getScrolledPosition()`: get current position
96 | 7. `getLinearLayoutManager()`:get LayoutManager
97 | 8. `getOrientation()`:get current scroll orientation(HORIZONTAL:0 VERTICAL:1)
98 | 9. `autoPlay(boolean)`:set can it auto play
99 | 10. `intervalTime(int interval)`:set auto play intervel
100 | 11. `initPosition(int position)`: set default position
101 |
102 | **XML API**
103 |
104 | 1. `app:helper="PagerSnapHelper/LinearSnapHelper"`:PagerSnapHelper can move just one page when scroll,LinearSnapHelper can move serveral page when scroll.
105 |
106 | # Version feature #
107 |
108 | see more in [Releases](https://github.com/ryanlijianchang/Recyclerview-Gallery/releases)。
109 |
110 |
111 | # License #
112 |
113 |
114 | Copyright 2017 ryanlijianchang
115 |
116 | Licensed under the Apache License, Version 2.0 (the "License");
117 | you may not use this file except in compliance with the License.
118 | You may obtain a copy of the License at
119 |
120 | http://www.apache.org/licenses/LICENSE-2.0
121 |
122 | Unless required by applicable law or agreed to in writing, software
123 | distributed under the License is distributed on an "AS IS" BASIS,
124 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
125 | See the License for the specific language governing permissions and
126 | limitations under the License.
--------------------------------------------------------------------------------
/README_CN.md:
--------------------------------------------------------------------------------
1 | # README #
2 |
3 | [](https://bintray.com/ryanlijianchang/maven/RecyclerView-Gallery)
4 | [](https://github.com/ryanlijianchang/Recyclerview-Gallery)
5 | [](https://github.com/ryanlijianchang/Recyclerview-Gallery)
6 |
7 | 使用RecyclerView实现Gallery效果。实现效果如下:
8 |
9 | 
10 |
11 | 
12 |
13 |
14 | # 用法 #
15 |
16 | 首先,在你的`build.gradle`中添加依赖。
17 |
18 | compile 'com.ryan.rv_gallery:rv-gallery:1.1.2'
19 |
20 | 第二,在你的layout文件中使用`GalleryRecyclerView`。
21 |
22 |
31 |
32 |
33 |
34 |
39 |
40 |
41 |
42 | 第三,在代码中像使用普通的RecyclerView一样,初始化你的GalleryRecyclerView。LayoutManager必须使用LinearLayoutManager,同时在创建LinearLayoutManager需要指定你的方向为`HORIZONTAL`或者`VERTICAL`,让`GalleryRecyclerView`水平或者垂直方向滑动。
43 |
44 | GalleryRecyclerView mRecyclerView = findViewById(R.id.rv_list);
45 | RecyclerAdapter adapter = new RecyclerAdapter(getApplicationContext(), getDatas());
46 | mRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
47 | mRecyclerView.setAdapter(adapter);
48 |
49 | 最后,指定`GalleryRecyclerView`的参数(非必须,不指定的话会使用默认值),最后必须调用setUp()方法才生效。
50 |
51 | mRecyclerView
52 | // 设置滑动速度(像素/s)
53 | .initFlingSpeed(9000)
54 | // 设置页边距和左右图片的可见宽度,单位dp
55 | .initPageParams(0, 40)
56 | // 设置切换动画的参数因子
57 | .setAnimFactor(0.1f)
58 | // 设置切换动画类型,目前有AnimManager.ANIM_BOTTOM_TO_TOP和目前有AnimManager.ANIM_TOP_TO_BOTTOM
59 | .setAnimType(AnimManager.ANIM_BOTTOM_TO_TOP)
60 | // 设置点击事件
61 | .setOnItemClickListener(this)
62 | // 设置自动播放
63 | .autoPlay(false)
64 | // 设置自动播放间隔时间 ms
65 | .intervalTime(2000)
66 | // 设置初始化的位置
67 | .initPosition(1)
68 | // 在设置完成之后,必须调用setUp()方法
69 | .setUp();
70 |
71 | 在你需要释放资源的地方调用release()方法,释放资源:
72 |
73 |
74 | @Override
75 | protected void onDestroy() {
76 | super.onDestroy();
77 | if (mRecyclerView != null) {
78 | // 释放资源
79 | mRecyclerView.release();
80 | }
81 | }
82 |
83 | # API #
84 |
85 | **Java API**
86 |
87 | 1. `initFlingSpeed(int speed)`:修改滑动速度(像素/s)
88 | 2. `setAnimFactor(float factor)`:修改切换动画的参数因子
89 | 3. `setAnimType(int type)`:配置动画类型 //ANIM_BOTTOM_TO_TOP、ANIM_TOP_TO_BOTTOM
90 | 4. `setOnItemClickListener(OnItemClickListener mListener)`:设置点击事件
91 | 5. `initPageParams(int pageMargin, int leftPageVisibleWidth)`:动态配置页边距和左右页可视宽度/高度
92 | 6. `getScrolledPosition()`:获取当前位置
93 | 7. `getLinearLayoutManager()`:获取LayoutManager
94 | 8. `getOrientation()`:获取当前的滑动方向 HORIZONTAL:0 VERTICAL:1
95 | 9. `autoPlay(boolean)`:是否自动播放
96 | 10. `intervalTime(int interval)`:自动播放间隔时间,单位ms
97 | 11. `initPosition(int position)`:开始处于的位置
98 |
99 | **XML API**
100 |
101 | 1. `app:helper="PagerSnapHelper/LinearSnapHelper"`:PagerSnapHelper一次只能滑动一页,LinearSnapHelper一次滑动多页。
102 |
103 | # 实现 #
104 |
105 | 具体实现过程已在掘金上发布了,如果你感兴趣,可以跳转到[这里](https://juejin.im/post/5a30fe5a6fb9a045132ab1bf)。如果你觉得可以帮助到你,不妨点个Star。
106 |
107 | # 版本特性 #
108 |
109 | 查看更多,请转移至[Releases](https://github.com/ryanlijianchang/Recyclerview-Gallery/releases)。
110 |
111 | **V1.1.2**
112 | 1. BUG FIX。Fix By @[tingshuonitiao](https://github.com/tingshuonitiao)修复第一次进入时第0张图片leftPageVisibleWidth不展示的bug
113 |
114 | **V1.1.1**
115 |
116 | 1. BUG FIX。修复第一次初始化时设置默认位置不对的问题。提供接口initPosition(int pos)设置初始位置,初始后可以直接调用smoothScrollToPosition(int pos)移动到需要的位置。
117 | 2. 更换设置ScrollListener时机。
118 |
119 | **V1.1.0**
120 |
121 | 1. BUG FIX。修复在Fragment上使用出现的异常问题。
122 | 2. 增加自动播放接口。
123 |
124 | **V1.0.9**
125 |
126 | 1. BUG FIX。修复滑动动画不顺畅问题。
127 |
128 | **V1.0.8**
129 |
130 | 1. BUG FIX。修复从其他图片切换到第一张图片时抖动的问题。
131 |
132 | **V1.0.7**
133 |
134 | 1. BUG FIX。修复横竖屏切换时UI异常问题。
135 |
136 | **V1.0.6**
137 |
138 | 1. BUG FIX By @[jefshi](https://github.com/jefshi)。去除单例,修复前后台切换后,位置不对的问题。
139 | 2. BUG FIX By @[jefshi](https://github.com/jefshi)。修复接受到微信、QQ等消息通知后,图片高度不对问题。
140 | 3. Gradle升级V4.4。
141 |
142 | **V1.0.5**
143 |
144 | 1. BUG FIX。修复了GalleryRecyclerView从前台切换到后台时闪退,位置错乱等问题。
145 |
146 | **V1.0.4**
147 |
148 | 1. 增加helper属性,包括LinearySnapHelper和PagerSnapHelper。
149 |
150 | **V1.0.3**
151 |
152 | 1. 修复了移动一页理论消耗距离应该是图片宽度加上2倍页边距。
153 | 2. 修复了修改页边距和可视宽度之后,没有生效。
154 |
155 | **V1.0.2**
156 |
157 | 1. BUG FIX。修复LayoutManager使用非LinearyLayoutManager时不抛出异常。
158 |
159 | **V1.0.1**
160 |
161 | 1. BUG FIX。首次打开,获得焦点后滑动至第0项,避免第0项的margin不对。
162 |
163 | **V1.0.0**
164 |
165 | 1. GalleryRecyclerview支持实现Gallery效果。
166 | 2. 支持动态修改滑动速度(像素/s)。
167 | 3. 支持动态修改切换动画的参数因子。
168 | 4. 支持配置动画类型。
169 | 5. 支持点击事件。
170 | 6. 支持动态配置页边距和左右页可视宽度/高度。
171 |
172 |
173 |
174 | # License #
175 |
176 |
177 | Copyright 2017 ryanlijianchang
178 |
179 | Licensed under the Apache License, Version 2.0 (the "License");
180 | you may not use this file except in compliance with the License.
181 | You may obtain a copy of the License at
182 |
183 | http://www.apache.org/licenses/LICENSE-2.0
184 |
185 | Unless required by applicable law or agreed to in writing, software
186 | distributed under the License is distributed on an "AS IS" BASIS,
187 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
188 | See the License for the specific language governing permissions and
189 | limitations under the License.
--------------------------------------------------------------------------------
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 27
5 | buildToolsVersion '27.0.3'
6 | defaultConfig {
7 | applicationId "ryane.com.gallery"
8 | minSdkVersion 15
9 | targetSdkVersion 27
10 | versionCode 1
11 | versionName "1.0"
12 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
13 |
14 | renderscriptTargetApi 19
15 | renderscriptSupportModeEnabled true
16 | }
17 | buildTypes {
18 | release {
19 | minifyEnabled false
20 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
21 | }
22 | }
23 | }
24 |
25 | dependencies {
26 | implementation 'com.ryan.base-lib:base-lib:0.0.6'
27 | implementation project(':rv-gallery')
28 | }
29 |
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in C:\Users\Administrator\AppData\Local\Android\Sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
19 | # Uncomment this to preserve the line number information for
20 | # debugging stack traces.
21 | #-keepattributes SourceFile,LineNumberTable
22 |
23 | # If you keep the line number information, uncomment this to
24 | # hide the original source file name.
25 | #-renamesourcefileattribute SourceFile
26 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/ryane/com/gallery/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package ryane.com.gallery;
2 |
3 | import android.content.Context;
4 | import android.support.test.InstrumentationRegistry;
5 | import android.support.test.runner.AndroidJUnit4;
6 |
7 | import org.junit.Test;
8 | import org.junit.runner.RunWith;
9 |
10 | import static org.junit.Assert.*;
11 |
12 | /**
13 | * Instrumentation test, which will execute on an Android device.
14 | *
15 | * @see Testing documentation
16 | */
17 | @RunWith(AndroidJUnit4.class)
18 | public class ExampleInstrumentedTest {
19 | @Test
20 | public void useAppContext() throws Exception {
21 | // Context of the app under test.
22 | Context appContext = InstrumentationRegistry.getTargetContext();
23 |
24 | assertEquals("ryane.com.testaidl", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
8 |
15 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
--------------------------------------------------------------------------------
/app/src/main/java/ryane/com/gallery/BlurBitmapUtil.java:
--------------------------------------------------------------------------------
1 | package ryane.com.gallery;
2 |
3 | import android.content.Context;
4 | import android.graphics.Bitmap;
5 | import android.support.v8.renderscript.Allocation;
6 | import android.support.v8.renderscript.Element;
7 | import android.support.v8.renderscript.RenderScript;
8 | import android.support.v8.renderscript.ScriptIntrinsicBlur;
9 |
10 | /**
11 | * @author Qiushui
12 | * @description 模糊图片工具类
13 | * @revision Xiarui 16.09.05
14 | */
15 | public class BlurBitmapUtil {
16 | //图片缩放比例
17 | private static final float BITMAP_SCALE = 0.4f;
18 |
19 | /**
20 | * 模糊图片的具体方法
21 | *
22 | * @param context 上下文对象
23 | * @param image 需要模糊的图片
24 | * @return 模糊处理后的图片
25 | */
26 | public static Bitmap blurBitmap(Context context, Bitmap image, float blurRadius) {
27 | // 计算图片缩小后的长宽
28 | int width = Math.round(image.getWidth() * BITMAP_SCALE);
29 | int height = Math.round(image.getHeight() * BITMAP_SCALE);
30 |
31 | // 将缩小后的图片做为预渲染的图片
32 | Bitmap inputBitmap = Bitmap.createScaledBitmap(image, width, height, false);
33 | // 创建一张渲染后的输出图片
34 | Bitmap outputBitmap = Bitmap.createBitmap(inputBitmap);
35 |
36 | // 创建RenderScript内核对象
37 | RenderScript rs = RenderScript.create(context);
38 | // 创建一个模糊效果的RenderScript的工具对象
39 | ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
40 |
41 | // 由于RenderScript并没有使用VM来分配内存,所以需要使用Allocation类来创建和分配内存空间
42 | // 创建Allocation对象的时候其实内存是空的,需要使用copyTo()将数据填充进去
43 | Allocation tmpIn = Allocation.createFromBitmap(rs, inputBitmap);
44 | Allocation tmpOut = Allocation.createFromBitmap(rs, outputBitmap);
45 |
46 | // 设置渲染的模糊程度, 25f是最大模糊度
47 | blurScript.setRadius(blurRadius);
48 | // 设置blurScript对象的输入内存
49 | blurScript.setInput(tmpIn);
50 | // 将输出数据保存到输出内存中
51 | blurScript.forEach(tmpOut);
52 |
53 | // 将数据填充到Allocation中
54 | tmpOut.copyTo(outputBitmap);
55 |
56 | return outputBitmap;
57 | }
58 | }
--------------------------------------------------------------------------------
/app/src/main/java/ryane/com/gallery/MainActivity.java:
--------------------------------------------------------------------------------
1 | package ryane.com.gallery;
2 |
3 | import android.content.res.TypedArray;
4 | import android.graphics.Bitmap;
5 | import android.graphics.BitmapFactory;
6 | import android.graphics.drawable.BitmapDrawable;
7 | import android.graphics.drawable.Drawable;
8 | import android.graphics.drawable.TransitionDrawable;
9 | import android.os.Bundle;
10 | import android.support.v7.app.AppCompatActivity;
11 | import android.support.v7.widget.LinearLayoutManager;
12 | import android.support.v7.widget.RecyclerView;
13 | import android.view.View;
14 | import android.widget.RelativeLayout;
15 | import android.widget.SeekBar;
16 | import android.widget.Toast;
17 |
18 | import com.ryan.rv_gallery.AnimManager;
19 | import com.ryan.rv_gallery.GalleryRecyclerView;
20 | import com.ryan.rv_gallery.util.DLog;
21 |
22 | import java.util.ArrayList;
23 | import java.util.HashMap;
24 | import java.util.List;
25 | import java.util.Map;
26 |
27 | /**
28 | * @author RyanLee
29 | */
30 | public class MainActivity extends AppCompatActivity implements GalleryRecyclerView.OnItemClickListener, RecyclerAdapter.OnItemPhotoChangedListener {
31 | public static final String TAG = "MainActivity_TAG";
32 |
33 | private GalleryRecyclerView mRecyclerView;
34 | private RelativeLayout mContainer;
35 | private SeekBar mSeekbar;
36 |
37 | private Map mTSDraCacheMap = new HashMap<>();
38 | private static final String KEY_PRE_DRAW = "key_pre_draw";
39 |
40 | /**
41 | * 获取虚化背景的位置
42 | */
43 | private int mLastDraPosition = -1;
44 |
45 |
46 | @Override
47 | protected void onCreate(Bundle savedInstanceState) {
48 | super.onCreate(savedInstanceState);
49 | setContentView(R.layout.activity_main);
50 |
51 | DLog.setDebug(true);
52 |
53 | DLog.d(TAG, "MainActivity onCreate()");
54 |
55 | mRecyclerView = findViewById(R.id.rv_list);
56 | mContainer = findViewById(R.id.rl_container);
57 | mSeekbar = findViewById(R.id.seekBar);
58 |
59 |
60 | final RecyclerAdapter adapter = new RecyclerAdapter(MainActivity.this, getDatas());
61 | adapter.setOnItemPhotoChangedListener(this);
62 |
63 | mRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
64 | mRecyclerView.setAdapter(adapter);
65 | mRecyclerView
66 | // 设置滑动速度(像素/s)
67 | .initFlingSpeed(9000)
68 | // 设置页边距和左右图片的可见宽度,单位dp
69 | .initPageParams(0, 40)
70 | // 设置切换动画的参数因子
71 | .setAnimFactor(0.1f)
72 | // 设置切换动画类型,目前有AnimManager.ANIM_BOTTOM_TO_TOP和目前有AnimManager.ANIM_TOP_TO_BOTTOM
73 | .setAnimType(AnimManager.ANIM_BOTTOM_TO_TOP)
74 | // 设置点击事件
75 | .setOnItemClickListener(this)
76 | // 设置自动播放
77 | .autoPlay(false)
78 | // 设置自动播放间隔时间 ms
79 | .intervalTime(2000)
80 | // 设置初始化的位置
81 | .initPosition(1)
82 | // 在设置完成之后,必须调用setUp()方法
83 | .setUp();
84 |
85 |
86 | // 背景高斯模糊 & 淡入淡出
87 | mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
88 | @Override
89 | public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
90 | super.onScrollStateChanged(recyclerView, newState);
91 |
92 | if (newState == RecyclerView.SCROLL_STATE_IDLE) {
93 | setBlurImage(false);
94 |
95 | mSeekbar.setProgress(mRecyclerView.getScrolledPosition());
96 | }
97 | }
98 | });
99 | setBlurImage(false);
100 |
101 | mSeekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
102 | @Override
103 | public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
104 | mRecyclerView.smoothScrollToPosition(progress);
105 | }
106 |
107 | @Override
108 | public void onStartTrackingTouch(SeekBar seekBar) {
109 |
110 | }
111 |
112 | @Override
113 | public void onStopTrackingTouch(SeekBar seekBar) {
114 |
115 | }
116 | });
117 | }
118 |
119 | /**
120 | * 设置背景高斯模糊
121 | */
122 | public void setBlurImage(boolean forceUpdate) {
123 | RecyclerAdapter adapter = (RecyclerAdapter) mRecyclerView.getAdapter();
124 | final int mCurViewPosition = mRecyclerView.getScrolledPosition();
125 |
126 | boolean isSamePosAndNotUpdate = (mCurViewPosition == mLastDraPosition) && !forceUpdate;
127 |
128 | if (adapter == null || mRecyclerView == null || isSamePosAndNotUpdate) {
129 | return;
130 | }
131 | mRecyclerView.post(new Runnable() {
132 | @Override
133 | public void run() {
134 | //如果是Fragment的话,需要判断Fragment是否Attach当前Activity,否则getResource会报错
135 | /*if (!isAdded()) {
136 | // fix fragment not attached to Activity
137 | return;
138 | }*/
139 | // 获取当前位置的图片资源ID
140 | int resourceId = ((RecyclerAdapter) mRecyclerView.getAdapter()).getResId(mCurViewPosition);
141 | // 将该资源图片转为Bitmap
142 | Bitmap resBmp = BitmapFactory.decodeResource(getResources(), resourceId);
143 | // 将该Bitmap高斯模糊后返回到resBlurBmp
144 | Bitmap resBlurBmp = BlurBitmapUtil.blurBitmap(mRecyclerView.getContext(), resBmp, 15f);
145 | // 再将resBlurBmp转为Drawable
146 | Drawable resBlurDrawable = new BitmapDrawable(resBlurBmp);
147 | // 获取前一页的Drawable
148 | Drawable preBlurDrawable = mTSDraCacheMap.get(KEY_PRE_DRAW) == null ? resBlurDrawable : mTSDraCacheMap.get(KEY_PRE_DRAW);
149 |
150 | /* 以下为淡入淡出效果 */
151 | Drawable[] drawableArr = {preBlurDrawable, resBlurDrawable};
152 | TransitionDrawable transitionDrawable = new TransitionDrawable(drawableArr);
153 | mContainer.setBackgroundDrawable(transitionDrawable);
154 | transitionDrawable.startTransition(500);
155 |
156 | // 存入到cache中
157 | mTSDraCacheMap.put(KEY_PRE_DRAW, resBlurDrawable);
158 | // 记录上一次高斯模糊的位置
159 | mLastDraPosition = mCurViewPosition;
160 | }
161 | });
162 | }
163 |
164 |
165 | /***
166 | * 测试数据
167 | * @return List
168 | */
169 | public List getDatas() {
170 | TypedArray ar = getResources().obtainTypedArray(R.array.test_arr);
171 | final int[] resIds = new int[ar.length()];
172 | for (int i = 0; i < ar.length(); i++) {
173 | resIds[i] = ar.getResourceId(i, 0);
174 | }
175 | ar.recycle();
176 | List tDatas = new ArrayList<>();
177 | for (int resId : resIds) {
178 | tDatas.add(resId);
179 | }
180 | return tDatas;
181 | }
182 |
183 | @Override
184 | protected void onResume() {
185 | super.onResume();
186 |
187 | DLog.d(TAG, "MainActivity onResume()");
188 | }
189 |
190 | @Override
191 | protected void onStart() {
192 | super.onStart();
193 |
194 | DLog.d(TAG, "MainActivity onStart()");
195 | }
196 |
197 | @Override
198 | protected void onPause() {
199 | super.onPause();
200 |
201 | DLog.d(TAG, "MainActivity onPause()");
202 | }
203 |
204 | @Override
205 | protected void onStop() {
206 | super.onStop();
207 |
208 | DLog.d(TAG, "MainActivity onStop()");
209 | }
210 |
211 | @Override
212 | protected void onDestroy() {
213 | super.onDestroy();
214 | DLog.d(TAG, "MainActivity onDestroy()");
215 |
216 | if (mRecyclerView != null) {
217 | // 释放资源
218 | mRecyclerView.release();
219 | }
220 | }
221 |
222 | @Override
223 | public void onItemClick(View view, int position) {
224 | Toast.makeText(getApplicationContext(), "position=" + position, Toast.LENGTH_SHORT).show();
225 | }
226 |
227 | @Override
228 | public void onItemPhotoChanged() {
229 | setBlurImage(true);
230 | }
231 | }
232 |
--------------------------------------------------------------------------------
/app/src/main/java/ryane/com/gallery/RecyclerAdapter.java:
--------------------------------------------------------------------------------
1 | package ryane.com.gallery;
2 |
3 | import android.content.Context;
4 | import android.support.design.widget.FloatingActionButton;
5 | import android.support.v7.widget.RecyclerView;
6 | import android.view.LayoutInflater;
7 | import android.view.View;
8 | import android.view.ViewGroup;
9 | import android.widget.ImageView;
10 |
11 | import com.ryan.rv_gallery.util.DLog;
12 |
13 | import java.util.List;
14 | import java.util.Random;
15 |
16 | /**
17 | * @author RyanLee
18 | * @date 2017/12/7
19 | */
20 |
21 | public class RecyclerAdapter extends RecyclerView.Adapter {
22 | private Context mContext;
23 | private List mDatas;
24 |
25 | private OnItemPhotoChangedListener mOnItemPhotoChangedListener;
26 |
27 |
28 | RecyclerAdapter(Context mContext, List mDatas) {
29 | this.mContext = mContext;
30 | this.mDatas = mDatas;
31 | }
32 |
33 | @Override
34 | public void onAttachedToRecyclerView(RecyclerView recyclerView) {
35 | DLog.d(MainActivity.TAG, "RecyclerAdapter onAttachedToRecyclerView");
36 | super.onAttachedToRecyclerView(recyclerView);
37 | }
38 |
39 | @Override
40 | public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
41 | DLog.d(MainActivity.TAG, "RecyclerAdapter onCreateViewHolder" + " width = " + parent.getWidth());
42 | View itemView = LayoutInflater.from(mContext).inflate(R.layout.item_gallery, parent, false);
43 | return new MyHolder(itemView);
44 | }
45 |
46 | @Override
47 | public void onBindViewHolder(final MyHolder holder, final int position) {
48 | DLog.d(MainActivity.TAG, "RecyclerAdapter onBindViewHolder" + "--> position = " + position);
49 | holder.mView.setImageResource(mDatas.get(holder.getAdapterPosition()));
50 | holder.mChange.setOnClickListener(new View.OnClickListener() {
51 | @Override
52 | public void onClick(View view) {
53 | int randomNum = new Random().nextInt(9);
54 | int[] res = {R.drawable.photo_nba1, R.drawable.photo_nba2, R.drawable.photo_nba3, R.drawable.photo_nba4,
55 | R.drawable.photo_nba5, R.drawable.photo_nba6, R.drawable.photo_nba7, R.drawable.photo_nba8, R.drawable.photo_nba9};
56 | mDatas.set(holder.getAdapterPosition(), res[randomNum]);
57 | notifyItemChanged(holder.getAdapterPosition(), this.getClass().getName());
58 | if (mOnItemPhotoChangedListener != null) {
59 | mOnItemPhotoChangedListener.onItemPhotoChanged();
60 | }
61 | }
62 | });
63 | }
64 |
65 | @Override
66 | public int getItemCount() {
67 | return mDatas.size();
68 | }
69 |
70 | static class MyHolder extends RecyclerView.ViewHolder {
71 | final ImageView mView;
72 | FloatingActionButton mChange;
73 |
74 | MyHolder(View itemView) {
75 | super(itemView);
76 | mView = itemView.findViewById(R.id.iv_photo);
77 | mChange = itemView.findViewById(R.id.fab_change);
78 | }
79 | }
80 |
81 | /**
82 | * 获取position位置的resId
83 | *
84 | * @param position int
85 | * @return int
86 | */
87 | public int getResId(int position) {
88 | return mDatas == null ? 0 : mDatas.get(position);
89 | }
90 |
91 | public void setOnItemPhotoChangedListener(OnItemPhotoChangedListener mOnItemPhotoChangedListener) {
92 | this.mOnItemPhotoChangedListener = mOnItemPhotoChangedListener;
93 | }
94 |
95 | public interface OnItemPhotoChangedListener {
96 | /**
97 | * 局部更新后需要替换背景图片
98 | */
99 | void onItemPhotoChanged();
100 | }
101 | }
102 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable-xxhdpi/beauty1.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ryanlijianchang/Recyclerview-Gallery/f1611b8fc4692fcf51aac556998ed69c7f43e89c/app/src/main/res/drawable-xxhdpi/beauty1.jpg
--------------------------------------------------------------------------------
/app/src/main/res/drawable-xxhdpi/beauty10.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ryanlijianchang/Recyclerview-Gallery/f1611b8fc4692fcf51aac556998ed69c7f43e89c/app/src/main/res/drawable-xxhdpi/beauty10.jpg
--------------------------------------------------------------------------------
/app/src/main/res/drawable-xxhdpi/beauty11.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ryanlijianchang/Recyclerview-Gallery/f1611b8fc4692fcf51aac556998ed69c7f43e89c/app/src/main/res/drawable-xxhdpi/beauty11.jpg
--------------------------------------------------------------------------------
/app/src/main/res/drawable-xxhdpi/beauty12.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ryanlijianchang/Recyclerview-Gallery/f1611b8fc4692fcf51aac556998ed69c7f43e89c/app/src/main/res/drawable-xxhdpi/beauty12.jpg
--------------------------------------------------------------------------------
/app/src/main/res/drawable-xxhdpi/beauty13.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ryanlijianchang/Recyclerview-Gallery/f1611b8fc4692fcf51aac556998ed69c7f43e89c/app/src/main/res/drawable-xxhdpi/beauty13.jpg
--------------------------------------------------------------------------------
/app/src/main/res/drawable-xxhdpi/beauty2.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ryanlijianchang/Recyclerview-Gallery/f1611b8fc4692fcf51aac556998ed69c7f43e89c/app/src/main/res/drawable-xxhdpi/beauty2.jpg
--------------------------------------------------------------------------------
/app/src/main/res/drawable-xxhdpi/beauty3.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ryanlijianchang/Recyclerview-Gallery/f1611b8fc4692fcf51aac556998ed69c7f43e89c/app/src/main/res/drawable-xxhdpi/beauty3.jpg
--------------------------------------------------------------------------------
/app/src/main/res/drawable-xxhdpi/beauty4.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ryanlijianchang/Recyclerview-Gallery/f1611b8fc4692fcf51aac556998ed69c7f43e89c/app/src/main/res/drawable-xxhdpi/beauty4.jpg
--------------------------------------------------------------------------------
/app/src/main/res/drawable-xxhdpi/beauty5.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ryanlijianchang/Recyclerview-Gallery/f1611b8fc4692fcf51aac556998ed69c7f43e89c/app/src/main/res/drawable-xxhdpi/beauty5.jpg
--------------------------------------------------------------------------------
/app/src/main/res/drawable-xxhdpi/beauty6.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ryanlijianchang/Recyclerview-Gallery/f1611b8fc4692fcf51aac556998ed69c7f43e89c/app/src/main/res/drawable-xxhdpi/beauty6.jpg
--------------------------------------------------------------------------------
/app/src/main/res/drawable-xxhdpi/beauty7.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ryanlijianchang/Recyclerview-Gallery/f1611b8fc4692fcf51aac556998ed69c7f43e89c/app/src/main/res/drawable-xxhdpi/beauty7.jpg
--------------------------------------------------------------------------------
/app/src/main/res/drawable-xxhdpi/beauty8.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ryanlijianchang/Recyclerview-Gallery/f1611b8fc4692fcf51aac556998ed69c7f43e89c/app/src/main/res/drawable-xxhdpi/beauty8.jpg
--------------------------------------------------------------------------------
/app/src/main/res/drawable-xxhdpi/beauty9.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ryanlijianchang/Recyclerview-Gallery/f1611b8fc4692fcf51aac556998ed69c7f43e89c/app/src/main/res/drawable-xxhdpi/beauty9.jpg
--------------------------------------------------------------------------------
/app/src/main/res/drawable-xxhdpi/ic_change.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ryanlijianchang/Recyclerview-Gallery/f1611b8fc4692fcf51aac556998ed69c7f43e89c/app/src/main/res/drawable-xxhdpi/ic_change.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable-xxhdpi/photo_nba1.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ryanlijianchang/Recyclerview-Gallery/f1611b8fc4692fcf51aac556998ed69c7f43e89c/app/src/main/res/drawable-xxhdpi/photo_nba1.jpg
--------------------------------------------------------------------------------
/app/src/main/res/drawable-xxhdpi/photo_nba2.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ryanlijianchang/Recyclerview-Gallery/f1611b8fc4692fcf51aac556998ed69c7f43e89c/app/src/main/res/drawable-xxhdpi/photo_nba2.jpg
--------------------------------------------------------------------------------
/app/src/main/res/drawable-xxhdpi/photo_nba3.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ryanlijianchang/Recyclerview-Gallery/f1611b8fc4692fcf51aac556998ed69c7f43e89c/app/src/main/res/drawable-xxhdpi/photo_nba3.jpg
--------------------------------------------------------------------------------
/app/src/main/res/drawable-xxhdpi/photo_nba4.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ryanlijianchang/Recyclerview-Gallery/f1611b8fc4692fcf51aac556998ed69c7f43e89c/app/src/main/res/drawable-xxhdpi/photo_nba4.jpg
--------------------------------------------------------------------------------
/app/src/main/res/drawable-xxhdpi/photo_nba5.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ryanlijianchang/Recyclerview-Gallery/f1611b8fc4692fcf51aac556998ed69c7f43e89c/app/src/main/res/drawable-xxhdpi/photo_nba5.jpg
--------------------------------------------------------------------------------
/app/src/main/res/drawable-xxhdpi/photo_nba6.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ryanlijianchang/Recyclerview-Gallery/f1611b8fc4692fcf51aac556998ed69c7f43e89c/app/src/main/res/drawable-xxhdpi/photo_nba6.jpg
--------------------------------------------------------------------------------
/app/src/main/res/drawable-xxhdpi/photo_nba7.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ryanlijianchang/Recyclerview-Gallery/f1611b8fc4692fcf51aac556998ed69c7f43e89c/app/src/main/res/drawable-xxhdpi/photo_nba7.jpg
--------------------------------------------------------------------------------
/app/src/main/res/drawable-xxhdpi/photo_nba8.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ryanlijianchang/Recyclerview-Gallery/f1611b8fc4692fcf51aac556998ed69c7f43e89c/app/src/main/res/drawable-xxhdpi/photo_nba8.jpg
--------------------------------------------------------------------------------
/app/src/main/res/drawable-xxhdpi/photo_nba9.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ryanlijianchang/Recyclerview-Gallery/f1611b8fc4692fcf51aac556998ed69c7f43e89c/app/src/main/res/drawable-xxhdpi/photo_nba9.jpg
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
11 |
12 |
17 |
18 |
27 |
28 |
29 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/item_gallery.xml:
--------------------------------------------------------------------------------
1 |
2 |
10 |
11 |
17 |
18 |
33 |
34 |
35 |
36 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ryanlijianchang/Recyclerview-Gallery/f1611b8fc4692fcf51aac556998ed69c7f43e89c/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ryanlijianchang/Recyclerview-Gallery/f1611b8fc4692fcf51aac556998ed69c7f43e89c/app/src/main/res/mipmap-hdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ryanlijianchang/Recyclerview-Gallery/f1611b8fc4692fcf51aac556998ed69c7f43e89c/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ryanlijianchang/Recyclerview-Gallery/f1611b8fc4692fcf51aac556998ed69c7f43e89c/app/src/main/res/mipmap-mdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ryanlijianchang/Recyclerview-Gallery/f1611b8fc4692fcf51aac556998ed69c7f43e89c/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ryanlijianchang/Recyclerview-Gallery/f1611b8fc4692fcf51aac556998ed69c7f43e89c/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ryanlijianchang/Recyclerview-Gallery/f1611b8fc4692fcf51aac556998ed69c7f43e89c/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ryanlijianchang/Recyclerview-Gallery/f1611b8fc4692fcf51aac556998ed69c7f43e89c/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ryanlijianchang/Recyclerview-Gallery/f1611b8fc4692fcf51aac556998ed69c7f43e89c/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ryanlijianchang/Recyclerview-Gallery/f1611b8fc4692fcf51aac556998ed69c7f43e89c/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/values-v19/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
8 |
--------------------------------------------------------------------------------
/app/src/main/res/values-v21/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #9E9E9E
4 | #6A1B9A
5 | #FF4081
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | RecyclerView-Gallery
3 |
4 |
5 | - @drawable/beauty1
6 | - @drawable/beauty2
7 | - @drawable/beauty3
8 | - @drawable/beauty4
9 | - @drawable/beauty5
10 | - @drawable/beauty6
11 | - @drawable/beauty7
12 | - @drawable/beauty8
13 | - @drawable/beauty9
14 | - @drawable/beauty10
15 | - @drawable/beauty11
16 | - @drawable/beauty12
17 | - @drawable/beauty13
18 |
19 |
20 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/app/src/test/java/ryane/com/gallery/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package ryane.com.gallery;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * @see Testing documentation
11 | */
12 | public class ExampleUnitTest {
13 | @Test
14 | public void addition_isCorrect() throws Exception {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
--------------------------------------------------------------------------------
/build.gradle:
--------------------------------------------------------------------------------
1 | // Top-level build file where you can add configuration options common to all sub-projects/modules.
2 |
3 | buildscript {
4 | repositories {
5 | jcenter()
6 | maven {
7 | url 'https://maven.google.com/'
8 | name 'Google'
9 | }
10 | google()
11 | }
12 | dependencies {
13 | classpath 'com.android.tools.build:gradle:3.1.2'
14 |
15 | // NOTE: Do not place your application dependencies here; they belong
16 | // in the individual module build.gradle files
17 |
18 | classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.3'
19 | classpath 'com.github.dcendents:android-maven-gradle-plugin:1.4.1'
20 | }
21 | }
22 |
23 | allprojects {
24 | repositories {
25 | jcenter()
26 | maven {
27 | url 'https://maven.google.com/'
28 | name 'Google'
29 | }
30 | }
31 | }
32 |
33 | task clean(type: Delete) {
34 | delete rootProject.buildDir
35 | }
36 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 |
3 | # IDE (e.g. Android Studio) users:
4 | # Gradle settings configured through the IDE *will override*
5 | # any settings specified in this file.
6 |
7 | # For more details on how to configure your build environment visit
8 | # http://www.gradle.org/docs/current/userguide/build_environment.html
9 |
10 | # Specifies the JVM arguments used for the daemon process.
11 | # The setting is particularly useful for tweaking memory settings.
12 | org.gradle.jvmargs=-Xmx1536m
13 |
14 | # When configured, Gradle will run in incubating parallel mode.
15 | # This option should only be used with decoupled projects. More details, visit
16 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
17 | # org.gradle.parallel=true
18 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ryanlijianchang/Recyclerview-Gallery/f1611b8fc4692fcf51aac556998ed69c7f43e89c/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Wed Jun 13 15:02:19 CST 2018
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip
7 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10 | DEFAULT_JVM_OPTS=""
11 |
12 | APP_NAME="Gradle"
13 | APP_BASE_NAME=`basename "$0"`
14 |
15 | # Use the maximum available, or set MAX_FD != -1 to use that value.
16 | MAX_FD="maximum"
17 |
18 | warn ( ) {
19 | echo "$*"
20 | }
21 |
22 | die ( ) {
23 | echo
24 | echo "$*"
25 | echo
26 | exit 1
27 | }
28 |
29 | # OS specific support (must be 'true' or 'false').
30 | cygwin=false
31 | msys=false
32 | darwin=false
33 | case "`uname`" in
34 | CYGWIN* )
35 | cygwin=true
36 | ;;
37 | Darwin* )
38 | darwin=true
39 | ;;
40 | MINGW* )
41 | msys=true
42 | ;;
43 | esac
44 |
45 | # Attempt to set APP_HOME
46 | # Resolve links: $0 may be a link
47 | PRG="$0"
48 | # Need this for relative symlinks.
49 | while [ -h "$PRG" ] ; do
50 | ls=`ls -ld "$PRG"`
51 | link=`expr "$ls" : '.*-> \(.*\)$'`
52 | if expr "$link" : '/.*' > /dev/null; then
53 | PRG="$link"
54 | else
55 | PRG=`dirname "$PRG"`"/$link"
56 | fi
57 | done
58 | SAVED="`pwd`"
59 | cd "`dirname \"$PRG\"`/" >/dev/null
60 | APP_HOME="`pwd -P`"
61 | cd "$SAVED" >/dev/null
62 |
63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
64 |
65 | # Determine the Java command to use to start the JVM.
66 | if [ -n "$JAVA_HOME" ] ; then
67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
68 | # IBM's JDK on AIX uses strange locations for the executables
69 | JAVACMD="$JAVA_HOME/jre/sh/java"
70 | else
71 | JAVACMD="$JAVA_HOME/bin/java"
72 | fi
73 | if [ ! -x "$JAVACMD" ] ; then
74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
75 |
76 | Please set the JAVA_HOME variable in your environment to match the
77 | location of your Java installation."
78 | fi
79 | else
80 | JAVACMD="java"
81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
82 |
83 | Please set the JAVA_HOME variable in your environment to match the
84 | location of your Java installation."
85 | fi
86 |
87 | # Increase the maximum file descriptors if we can.
88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
89 | MAX_FD_LIMIT=`ulimit -H -n`
90 | if [ $? -eq 0 ] ; then
91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
92 | MAX_FD="$MAX_FD_LIMIT"
93 | fi
94 | ulimit -n $MAX_FD
95 | if [ $? -ne 0 ] ; then
96 | warn "Could not set maximum file descriptor limit: $MAX_FD"
97 | fi
98 | else
99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
100 | fi
101 | fi
102 |
103 | # For Darwin, add options to specify how the application appears in the dock
104 | if $darwin; then
105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
106 | fi
107 |
108 | # For Cygwin, switch paths to Windows format before running java
109 | if $cygwin ; then
110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
112 | JAVACMD=`cygpath --unix "$JAVACMD"`
113 |
114 | # We build the pattern for arguments to be converted via cygpath
115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
116 | SEP=""
117 | for dir in $ROOTDIRSRAW ; do
118 | ROOTDIRS="$ROOTDIRS$SEP$dir"
119 | SEP="|"
120 | done
121 | OURCYGPATTERN="(^($ROOTDIRS))"
122 | # Add a user-defined pattern to the cygpath arguments
123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
125 | fi
126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
127 | i=0
128 | for arg in "$@" ; do
129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
131 |
132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
134 | else
135 | eval `echo args$i`="\"$arg\""
136 | fi
137 | i=$((i+1))
138 | done
139 | case $i in
140 | (0) set -- ;;
141 | (1) set -- "$args0" ;;
142 | (2) set -- "$args0" "$args1" ;;
143 | (3) set -- "$args0" "$args1" "$args2" ;;
144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
150 | esac
151 | fi
152 |
153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
154 | function splitJvmOpts() {
155 | JVM_OPTS=("$@")
156 | }
157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
159 |
160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
161 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12 | set DEFAULT_JVM_OPTS=
13 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windowz variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/rv-gallery/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/rv-gallery/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 | apply plugin: 'com.jfrog.bintray'
3 | apply plugin: 'com.github.dcendents.android-maven'
4 |
5 | android {
6 | compileSdkVersion 27
7 | buildToolsVersion "27.0.3"
8 |
9 |
10 |
11 | defaultConfig {
12 | minSdkVersion 14
13 | targetSdkVersion 27
14 | versionCode 1
15 | versionName "1.0"
16 |
17 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
18 |
19 | }
20 |
21 | buildTypes {
22 | release {
23 | minifyEnabled false
24 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
25 | }
26 | }
27 |
28 | }
29 |
30 | dependencies {
31 | implementation 'com.ryan.base-lib:base-lib:0.0.5'
32 | }
33 |
34 | //important
35 | Properties properties = new Properties()
36 | properties.load(project.rootProject.file('local.properties').newDataInputStream())
37 |
38 | version = "1.1.2"//发布的library的版本号,这个比较关键!!!
39 |
40 | def siteUrl = 'https://github.com/ryanlijianchang/Recyclerview-Gallery' // 项目的主页(可随意,建议填library的github地址)
41 | def gitUrl = 'https://github.com/ryanlijianchang/Recyclerview-Gallery' // Git仓库的url(可随意,建议填library的github地址)
42 | group = "com.ryan.rv_gallery" // 这里是groupId ,必须填写(建议填唯一的包名)
43 |
44 | bintray {
45 | user = properties.getProperty("bintray.user") //读取 local.properties 文件里面的 bintray.user
46 | key = properties.getProperty("bintray.apikey")
47 |
48 | configurations = ['archives']
49 | pkg {
50 | repo = "maven"
51 | group = group
52 | name = "RecyclerView-Gallery"//发布到JCenter上的项目名字,必须填写
53 | websiteUrl = siteUrl
54 | vcsUrl = gitUrl
55 | licenses = ["Apache-2.0"]
56 | publish = true
57 | }
58 | }
59 |
60 | install {
61 | repositories.mavenInstaller {
62 | // This generates POM.xml with proper parameters
63 | pom {
64 | project {
65 | packaging 'aar'
66 | // Add your description here
67 | name 'The library is a custom view about "Personal center consumption points" in our old version app' //项目描述
68 | url siteUrl
69 | // Set your license
70 | licenses {
71 | license {
72 | name 'The Apache Software License, Version 2.0'
73 | url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
74 | }
75 | }
76 | developers {
77 | developer {
78 | id 'ryanlijianchang' //开发者的基本信息(建议填自己名称)
79 | name 'RyanLee' //开发者基本信息(建议填自己名称)
80 | email 'liji.anchang@163.com' //开发者基本信息(建议填自己名称)
81 | }
82 | }
83 | scm {
84 | connection gitUrl
85 | developerConnection gitUrl
86 | url siteUrl
87 | }
88 | }
89 | }
90 | }
91 | }
92 |
--------------------------------------------------------------------------------
/rv-gallery/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
22 |
--------------------------------------------------------------------------------
/rv-gallery/src/androidTest/java/com/ryan/rv_gallery/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package com.ryan.rv_gallery;
2 |
3 | import android.content.Context;
4 | import android.support.test.InstrumentationRegistry;
5 | import android.support.test.runner.AndroidJUnit4;
6 |
7 | import org.junit.Test;
8 | import org.junit.runner.RunWith;
9 |
10 | import static org.junit.Assert.*;
11 |
12 | /**
13 | * Instrumented test, which will execute on an Android device.
14 | *
15 | * @see Testing documentation
16 | */
17 | @RunWith(AndroidJUnit4.class)
18 | public class ExampleInstrumentedTest {
19 | @Test
20 | public void useAppContext() throws Exception {
21 | // Context of the app under test.
22 | Context appContext = InstrumentationRegistry.getTargetContext();
23 |
24 | assertEquals("com.ambits.rv_gallery.test", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/rv-gallery/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
3 |
--------------------------------------------------------------------------------
/rv-gallery/src/main/java/com/ryan/rv_gallery/AnimManager.java:
--------------------------------------------------------------------------------
1 | package com.ryan.rv_gallery;
2 |
3 | import android.support.v7.widget.RecyclerView;
4 | import android.view.View;
5 |
6 | /**
7 | *
8 | * @author RyanLee
9 | * @date 2017/12/12
10 | */
11 |
12 | public class AnimManager {
13 |
14 | public static final int ANIM_BOTTOM_TO_TOP = 0;
15 | public static final int ANIM_TOP_TO_BOTTOM = 1;
16 |
17 | /**
18 | * 动画类型
19 | */
20 | private int mAnimType = ANIM_BOTTOM_TO_TOP;
21 | /**
22 | * 变化因子
23 | */
24 | private float mAnimFactor = 0.2f;
25 |
26 | public void setAnimation(RecyclerView recyclerView, int position, float percent) {
27 | switch (mAnimType) {
28 | case ANIM_BOTTOM_TO_TOP:
29 | setBottomToTopAnim(recyclerView, position, percent);
30 | break;
31 | case ANIM_TOP_TO_BOTTOM:
32 | setTopToBottomAnim(recyclerView, position, percent);
33 | break;
34 | default:
35 | setBottomToTopAnim(recyclerView, position, percent);
36 | break;
37 | }
38 | }
39 |
40 |
41 | /**
42 | * 从下到上的动画效果
43 | *
44 | * @param recyclerView RecyclerView
45 | * @param position int
46 | * @param percent float
47 | */
48 | private void setBottomToTopAnim(RecyclerView recyclerView, int position, float percent) {
49 | // 中间页
50 | View mCurView = recyclerView.getLayoutManager().findViewByPosition(position);
51 | // 右边页
52 | View mRightView = recyclerView.getLayoutManager().findViewByPosition(position + 1);
53 | // 左边页
54 | View mLeftView = recyclerView.getLayoutManager().findViewByPosition(position - 1);
55 | // 右右边页
56 | View mRRView = recyclerView.getLayoutManager().findViewByPosition(position + 2);
57 |
58 | if (mLeftView != null) {
59 | mLeftView.setScaleX((1 - mAnimFactor) + percent * mAnimFactor);
60 | mLeftView.setScaleY((1 - mAnimFactor) + percent * mAnimFactor);
61 | }
62 | if (mCurView != null) {
63 | mCurView.setScaleX(1 - percent * mAnimFactor);
64 | mCurView.setScaleY(1 - percent * mAnimFactor);
65 | }
66 | if (mRightView != null) {
67 | mRightView.setScaleX((1 - mAnimFactor) + percent * mAnimFactor);
68 | mRightView.setScaleY((1 - mAnimFactor) + percent * mAnimFactor);
69 | }
70 | if (mRRView != null) {
71 | mRRView.setScaleX(1 - percent * mAnimFactor);
72 | mRRView.setScaleY(1 - percent * mAnimFactor);
73 | }
74 | }
75 |
76 |
77 | /***
78 | * 从上到下的效果
79 | * @param recyclerView RecyclerView
80 | * @param position int
81 | * @param percent int
82 | */
83 | private void setTopToBottomAnim(RecyclerView recyclerView, int position, float percent) {
84 | // 中间页
85 | View mCurView = recyclerView.getLayoutManager().findViewByPosition(position);
86 | // 右边页
87 | View mRightView = recyclerView.getLayoutManager().findViewByPosition(position + 1);
88 | // 左边页
89 | View mLeftView = recyclerView.getLayoutManager().findViewByPosition(position - 1);
90 | // 左左边页
91 | View mLLView = recyclerView.getLayoutManager().findViewByPosition(position - 2);
92 |
93 | if (mLeftView != null) {
94 | mLeftView.setScaleX(1 - percent * mAnimFactor);
95 | mLeftView.setScaleY(1 - percent * mAnimFactor);
96 | }
97 | if (mCurView != null) {
98 | mCurView.setScaleX((1 - mAnimFactor) + percent * mAnimFactor);
99 | mCurView.setScaleY((1 - mAnimFactor) + percent * mAnimFactor);
100 | }
101 | if (mRightView != null) {
102 | mRightView.setScaleX(1 - percent * mAnimFactor);
103 | mRightView.setScaleY(1 - percent * mAnimFactor);
104 | }
105 | if (mLLView != null) {
106 | mLLView.setScaleX((1 - mAnimFactor) + percent * mAnimFactor);
107 | mLLView.setScaleY((1 - mAnimFactor) + percent * mAnimFactor);
108 | }
109 | }
110 |
111 | public void setAnimFactor(float mAnimFactor) {
112 | this.mAnimFactor = mAnimFactor;
113 | }
114 |
115 | public void setAnimType(int mAnimType) {
116 | this.mAnimType = mAnimType;
117 | }
118 | }
119 |
--------------------------------------------------------------------------------
/rv-gallery/src/main/java/com/ryan/rv_gallery/GalleryItemDecoration.java:
--------------------------------------------------------------------------------
1 | package com.ryan.rv_gallery;
2 |
3 | import android.graphics.Rect;
4 | import android.support.v7.widget.LinearLayoutManager;
5 | import android.support.v7.widget.RecyclerView;
6 | import android.view.View;
7 | import android.view.ViewGroup;
8 |
9 | import com.ryan.rv_gallery.util.DLog;
10 | import com.ryan.rv_gallery.util.OsUtil;
11 |
12 | /**
13 | * @author RyanLee
14 | * @date 2017/12/14
15 | */
16 |
17 | public class GalleryItemDecoration extends RecyclerView.ItemDecoration {
18 | private final String TAG = "MainActivity_TAG";
19 |
20 | /**
21 | * 每一个页面默认页边距
22 | */
23 | public int mPageMargin = 0;
24 | /**
25 | * 中间页面左右两边的页面可见部分宽度
26 | */
27 | public int mLeftPageVisibleWidth = 50;
28 |
29 | public int mItemConsumeY = 0;
30 | public int mItemConsumeX = 0;
31 |
32 | private GalleryRecyclerView.OnItemClickListener onItemClickListener;
33 |
34 | private OnItemSizeMeasuredListener mOnItemSizeMeasuredListener;
35 |
36 | GalleryItemDecoration() {
37 | }
38 |
39 | @Override
40 | public void getItemOffsets(Rect outRect, final View view, final RecyclerView parent, RecyclerView.State state) {
41 | super.getItemOffsets(outRect, view, parent, state);
42 |
43 | DLog.d(TAG, "GalleryItemDecoration getItemOffset() --> position = " + parent.getChildAdapterPosition(view));
44 |
45 | final int position = parent.getChildAdapterPosition(view);
46 | final int itemCount = parent.getAdapter().getItemCount();
47 |
48 | parent.post(new Runnable() {
49 | @Override
50 | public void run() {
51 | if (((GalleryRecyclerView) parent).getOrientation() == LinearLayoutManager.HORIZONTAL) {
52 | onSetHorizontalParams(parent, view, position, itemCount);
53 | } else {
54 | onSetVerticalParams(parent, view, position, itemCount);
55 | }
56 | }
57 | });
58 |
59 | view.setOnClickListener(new View.OnClickListener() {
60 | @Override
61 | public void onClick(View v) {
62 | if (onItemClickListener != null) {
63 | onItemClickListener.onItemClick(v, position);
64 | }
65 | }
66 | });
67 | }
68 |
69 | private void onSetVerticalParams(ViewGroup parent, View itemView, int position, int itemCount) {
70 | int itemNewWidth = parent.getWidth();
71 | int itemNewHeight = parent.getHeight() - OsUtil.dpToPx(4 * mPageMargin + 2 * mLeftPageVisibleWidth);
72 |
73 | mItemConsumeY = itemNewHeight + OsUtil.dpToPx(2 * mPageMargin);
74 |
75 | if (mOnItemSizeMeasuredListener != null) {
76 | mOnItemSizeMeasuredListener.onItemSizeMeasured(mItemConsumeY);
77 | }
78 |
79 | // 适配第0页和最后一页没有左页面和右页面,让他们保持左边距和右边距和其他项一样
80 | int topMargin = position == 0 ? OsUtil.dpToPx(mLeftPageVisibleWidth + 2 * mPageMargin) : OsUtil.dpToPx(mPageMargin);
81 | int bottomMargin = position == itemCount - 1 ? OsUtil.dpToPx(mLeftPageVisibleWidth + 2 * mPageMargin) : OsUtil.dpToPx(mPageMargin);
82 |
83 | setLayoutParams(itemView, 0, topMargin, 0, bottomMargin, itemNewWidth, itemNewHeight);
84 | }
85 |
86 | /**
87 | * 设置水平滚动的参数
88 | *
89 | * @param parent ViewGroup
90 | * @param itemView View
91 | * @param position int
92 | * @param itemCount int
93 | */
94 | private void onSetHorizontalParams(ViewGroup parent, View itemView, int position, int itemCount) {
95 | int itemNewWidth = parent.getWidth() - OsUtil.dpToPx(4 * mPageMargin + 2 * mLeftPageVisibleWidth);
96 | int itemNewHeight = parent.getHeight();
97 |
98 | mItemConsumeX = itemNewWidth + OsUtil.dpToPx(2 * mPageMargin);
99 |
100 | if (mOnItemSizeMeasuredListener != null) {
101 | mOnItemSizeMeasuredListener.onItemSizeMeasured(mItemConsumeX);
102 | }
103 |
104 | DLog.d(TAG, "GalleryItemDecoration onSetHorizontalParams -->" + "parent.width=" + parent.getWidth() + ";mPageMargin=" + OsUtil.dpToPx(mPageMargin)
105 | + ";mLeftVis=" + OsUtil.dpToPx(mLeftPageVisibleWidth) + ";itemNewWidth=" + itemNewWidth);
106 |
107 | // 适配第0页和最后一页没有左页面和右页面,让他们保持左边距和右边距和其他项一样
108 | int leftMargin = position == 0 ? OsUtil.dpToPx(mLeftPageVisibleWidth + 2 * mPageMargin) : OsUtil.dpToPx(mPageMargin);
109 | int rightMargin = position == itemCount - 1 ? OsUtil.dpToPx(mLeftPageVisibleWidth + 2 * mPageMargin) : OsUtil.dpToPx(mPageMargin);
110 |
111 | setLayoutParams(itemView, leftMargin, 0, rightMargin, 0, itemNewWidth, itemNewHeight);
112 | }
113 |
114 | /**
115 | * 设置参数
116 | *
117 | * @param itemView View
118 | * @param left int
119 | * @param top int
120 | * @param right int
121 | * @param bottom int
122 | * @param itemWidth int
123 | * @param itemHeight int
124 | */
125 | private void setLayoutParams(View itemView, int left, int top, int right, int bottom, int itemWidth, int itemHeight) {
126 |
127 | DLog.d(TAG, "GalleryItemDecoration setLayoutParams -->" + "left=" + left + ";top=" + top
128 | + ";right=" + right + ";bottom=" + bottom + ";itemWidth=" + itemWidth + ";itemHeight=" + itemHeight);
129 |
130 | RecyclerView.LayoutParams lp = (RecyclerView.LayoutParams) itemView.getLayoutParams();
131 | boolean mMarginChange = false;
132 | boolean mWidthChange = false;
133 | boolean mHeightChange = false;
134 |
135 | if (lp.leftMargin != left || lp.topMargin != top || lp.rightMargin != right || lp.bottomMargin != bottom) {
136 | lp.setMargins(left, top, right, bottom);
137 | mMarginChange = true;
138 | }
139 | if (lp.width != itemWidth) {
140 | lp.width = itemWidth;
141 | mWidthChange = true;
142 | }
143 | if (lp.height != itemHeight) {
144 | lp.height = itemHeight;
145 | mHeightChange = true;
146 |
147 | }
148 |
149 | // 因为方法会不断调用,只有在真正变化了之后才调用
150 | if (mWidthChange || mMarginChange || mHeightChange) {
151 | itemView.setLayoutParams(lp);
152 | }
153 | }
154 |
155 | public void setOnItemClickListener(GalleryRecyclerView.OnItemClickListener onItemClickListener) {
156 | this.onItemClickListener = onItemClickListener;
157 | }
158 |
159 | public void setOnItemSizeMeasuredListener(OnItemSizeMeasuredListener itemSizeMeasuredListener) {
160 | this.mOnItemSizeMeasuredListener = itemSizeMeasuredListener;
161 | }
162 |
163 | interface OnItemSizeMeasuredListener {
164 | /**
165 | * Item的大小测量完成
166 | * @param size int
167 | */
168 | void onItemSizeMeasured(int size);
169 | }
170 | }
171 |
--------------------------------------------------------------------------------
/rv-gallery/src/main/java/com/ryan/rv_gallery/GalleryRecyclerView.java:
--------------------------------------------------------------------------------
1 | package com.ryan.rv_gallery;
2 |
3 | import android.content.Context;
4 | import android.content.res.TypedArray;
5 | import android.os.Parcelable;
6 | import android.support.annotation.FloatRange;
7 | import android.support.annotation.IntRange;
8 | import android.support.annotation.Nullable;
9 | import android.support.v7.widget.GridLayoutManager;
10 | import android.support.v7.widget.LinearLayoutManager;
11 | import android.support.v7.widget.RecyclerView;
12 | import android.util.AttributeSet;
13 | import android.view.MotionEvent;
14 | import android.view.View;
15 |
16 | import com.ryan.baselib.util.ThreadUtils;
17 | import com.ryan.rv_gallery.util.DLog;
18 |
19 | /**
20 | * @author RyanLee
21 | * @date 2017/12/8
22 | */
23 |
24 | public class GalleryRecyclerView extends RecyclerView implements View.OnTouchListener, GalleryItemDecoration.OnItemSizeMeasuredListener {
25 |
26 | private static final String TAG = "MainActivity_TAG";
27 |
28 | public static final int LINEAR_SNAP_HELPER = 0;
29 | public static final int PAGER_SNAP_HELPER = 1;
30 | /**
31 | * 滑动速度
32 | */
33 | private int mFlingSpeed = 1000;
34 | /**
35 | * 是否自动播放
36 | */
37 | private boolean mAutoPlay = false;
38 | /**
39 | * 自动播放间隔时间
40 | */
41 | private int mInterval = 1000;
42 |
43 | private int mInitPos = -1;
44 |
45 |
46 | private AnimManager mAnimManager;
47 | private ScrollManager mScrollManager;
48 | private GalleryItemDecoration mDecoration;
49 |
50 | private Runnable mAutoPlayTask = new Runnable() {
51 | @Override
52 | public void run() {
53 | if (getAdapter() == null || getAdapter().getItemCount() <= 0) {
54 | return;
55 | }
56 |
57 | int position = getScrolledPosition();
58 | int itemCount = getAdapter().getItemCount();
59 |
60 | int newPosition = (position + 1) % itemCount;
61 | smoothScrollToPosition(newPosition);
62 |
63 | ThreadUtils.removeCallbacks(this);
64 | ThreadUtils.runOnUiThread(this, mInterval);
65 | }
66 | };
67 |
68 | public GalleryItemDecoration getDecoration() {
69 | return mDecoration;
70 | }
71 |
72 | public AnimManager getAnimManager() {
73 | return mAnimManager;
74 | }
75 |
76 | public GalleryRecyclerView(Context context) {
77 | this(context, null);
78 | }
79 |
80 | public GalleryRecyclerView(Context context, @Nullable AttributeSet attrs) {
81 | this(context, attrs, 0);
82 | }
83 |
84 | public GalleryRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {
85 | super(context, attrs, defStyle);
86 |
87 | TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.GalleryRecyclerView);
88 | int helper = ta.getInteger(R.styleable.GalleryRecyclerView_helper, LINEAR_SNAP_HELPER);
89 | ta.recycle();
90 |
91 | DLog.d(TAG, "GalleryRecyclerView constructor");
92 |
93 | mAnimManager = new AnimManager();
94 | attachDecoration();
95 | attachToRecyclerHelper(helper);
96 |
97 | //设置触碰监听
98 | setOnTouchListener(this);
99 | }
100 |
101 | @Override
102 | protected void onFinishInflate() {
103 | super.onFinishInflate();
104 | }
105 |
106 | @Override
107 | public void onWindowFocusChanged(boolean hasWindowFocus) {
108 | super.onWindowFocusChanged(hasWindowFocus);
109 | }
110 |
111 |
112 | private void attachDecoration() {
113 | DLog.d(TAG, "GalleryRecyclerView attachDecoration");
114 |
115 | mDecoration = new GalleryItemDecoration();
116 | mDecoration.setOnItemSizeMeasuredListener(this);
117 | addItemDecoration(mDecoration);
118 | }
119 |
120 |
121 | @Override
122 | public boolean fling(int velocityX, int velocityY) {
123 | velocityX = balanceVelocity(velocityX);
124 | velocityY = balanceVelocity(velocityY);
125 | return super.fling(velocityX, velocityY);
126 | }
127 |
128 | /**
129 | * 返回滑动速度值
130 | *
131 | * @param velocity int
132 | * @return int
133 | */
134 | private int balanceVelocity(int velocity) {
135 | if (velocity > 0) {
136 | return Math.min(velocity, mFlingSpeed);
137 | } else {
138 | return Math.max(velocity, -mFlingSpeed);
139 | }
140 | }
141 |
142 | /**
143 | * 连接RecyclerHelper
144 | *
145 | * @param helper int
146 | */
147 | private void attachToRecyclerHelper(int helper) {
148 | DLog.d(TAG, "GalleryRecyclerView attachToRecyclerHelper");
149 |
150 | mScrollManager = new ScrollManager(this);
151 | mScrollManager.initScrollListener();
152 | mScrollManager.initSnapHelper(helper);
153 | }
154 |
155 | /**
156 | * 设置页面参数,单位dp
157 | *
158 | * @param pageMargin 默认:0dp
159 | * @param leftPageVisibleWidth 默认:50dp
160 | * @return GalleryRecyclerView
161 | */
162 | public GalleryRecyclerView initPageParams(int pageMargin, int leftPageVisibleWidth) {
163 | mDecoration.mPageMargin = pageMargin;
164 | mDecoration.mLeftPageVisibleWidth = leftPageVisibleWidth;
165 | return this;
166 | }
167 |
168 | /**
169 | * 设置滑动速度(像素/s)
170 | *
171 | * @param speed int
172 | * @return GalleryRecyclerView
173 | */
174 | public GalleryRecyclerView initFlingSpeed(@IntRange(from = 0) int speed) {
175 | this.mFlingSpeed = speed;
176 | return this;
177 | }
178 |
179 | /**
180 | * 设置动画因子
181 | *
182 | * @param factor float
183 | * @return GalleryRecyclerView
184 | */
185 | public GalleryRecyclerView setAnimFactor(@FloatRange(from = 0f) float factor) {
186 | mAnimManager.setAnimFactor(factor);
187 | return this;
188 | }
189 |
190 | /**
191 | * 设置动画类型
192 | *
193 | * @param type int
194 | * @return GalleryRecyclerView
195 | */
196 | public GalleryRecyclerView setAnimType(int type) {
197 | mAnimManager.setAnimType(type);
198 | return this;
199 | }
200 |
201 | /**
202 | * 设置点击事件
203 | *
204 | * @param mListener OnItemClickListener
205 | */
206 | public GalleryRecyclerView setOnItemClickListener(OnItemClickListener mListener) {
207 | if (mDecoration != null) {
208 | mDecoration.setOnItemClickListener(mListener);
209 | }
210 | return this;
211 | }
212 |
213 | /**
214 | * 是否自动滚动
215 | *
216 | * @param auto boolean
217 | * @return GalleryRecyclerView
218 | */
219 | public GalleryRecyclerView autoPlay(boolean auto) {
220 | this.mAutoPlay = auto;
221 | return this;
222 | }
223 |
224 | /**
225 | * 自动播放
226 | */
227 | private void autoPlayGallery() {
228 | if (mAutoPlay) {
229 | ThreadUtils.removeCallbacks(mAutoPlayTask);
230 | ThreadUtils.runOnUiThread(mAutoPlayTask, mInterval);
231 | }
232 | }
233 |
234 | /**
235 | * 移除自动播放Runnable
236 | */
237 | private void removeAutoPlayTask() {
238 | if (mAutoPlay) {
239 | ThreadUtils.removeCallbacks(mAutoPlayTask);
240 | }
241 | }
242 |
243 | /**
244 | * 装载
245 | *
246 | * @return GalleryRecyclerView
247 | */
248 | public GalleryRecyclerView setUp() {
249 | if (getAdapter().getItemCount() <= 0) {
250 | return this;
251 | }
252 |
253 | smoothScrollToPosition(0);
254 | mScrollManager.updateConsume();
255 |
256 | autoPlayGallery();
257 |
258 | return this;
259 | }
260 |
261 | /**
262 | * 释放资源
263 | */
264 | public void release() {
265 | removeAutoPlayTask();
266 | }
267 |
268 |
269 | public int getOrientation() {
270 |
271 | if (getLayoutManager() instanceof LinearLayoutManager) {
272 | if (getLayoutManager() instanceof GridLayoutManager) {
273 | throw new RuntimeException("请设置LayoutManager为LinearLayoutManager");
274 | } else {
275 | return ((LinearLayoutManager) getLayoutManager()).getOrientation();
276 | }
277 | } else {
278 | throw new RuntimeException("请设置LayoutManager为LinearLayoutManager");
279 | }
280 | }
281 |
282 | public int getScrolledPosition() {
283 | if (mScrollManager == null) {
284 | return 0;
285 | } else {
286 | return mScrollManager.getPosition();
287 | }
288 | }
289 |
290 | @Override
291 | protected Parcelable onSaveInstanceState() {
292 | DLog.w(TAG, "GalleryRecyclerView onSaveInstanceState()");
293 | return super.onSaveInstanceState();
294 | }
295 |
296 | @Override
297 | protected void onRestoreInstanceState(Parcelable state) {
298 | super.onRestoreInstanceState(state);
299 |
300 | // 如果是横竖屏切换(Fragment销毁),不应该走smoothScrollToPosition(0),因为这个方法会导致ScrollManager的onHorizontalScroll不断执行,而ScrollManager.mConsumeX已经重置,会导致这个值紊乱
301 | // 而如果走scrollToPosition(0)方法,则不会导致ScrollManager的onHorizontalScroll执行,所以ScrollManager.mConsumeX这个值不会错误
302 | scrollToPosition(0);
303 | // 但是因为不走ScrollManager的onHorizontalScroll,所以不会执行切换动画,所以就调用smoothScrollBy(int dx, int dy),让item轻微滑动,触发动画
304 | smoothScrollBy(10, 0);
305 | smoothScrollBy(0, 0);
306 |
307 | autoPlayGallery();
308 | }
309 |
310 | @Override
311 | public boolean onTouch(View v, MotionEvent event) {
312 | switch (event.getAction()) {
313 | case MotionEvent.ACTION_DOWN:
314 | removeAutoPlayTask();
315 | break;
316 | case MotionEvent.ACTION_MOVE:
317 | removeAutoPlayTask();
318 | break;
319 | case MotionEvent.ACTION_UP:
320 | autoPlayGallery();
321 | break;
322 | default:
323 | break;
324 | }
325 | return false;
326 | }
327 |
328 | /**
329 | * 播放间隔时间 ms
330 | *
331 | * @param interval int
332 | * @return GalleryRecyclerView
333 | */
334 | public GalleryRecyclerView intervalTime(@IntRange(from = 10) int interval) {
335 | this.mInterval = interval;
336 | return this;
337 | }
338 |
339 | /**
340 | * 开始处于的位置
341 | *
342 | * @param i int
343 | * @return GalleryRecyclerView
344 | */
345 | public GalleryRecyclerView initPosition(@IntRange(from = 0) int i) {
346 | if (i >= getAdapter().getItemCount()) {
347 | i = getAdapter().getItemCount() - 1;
348 | } else if (i < 0) {
349 | i = 0;
350 | }
351 | mInitPos = i;
352 | return this;
353 | }
354 |
355 | @Override
356 | public void onItemSizeMeasured(int size) {
357 | if (mInitPos < 0) {
358 | return;
359 | }
360 | if (mInitPos == 0) {
361 | scrollToPosition(0);
362 | } else {
363 | if (getOrientation() == LinearLayoutManager.HORIZONTAL) {
364 | smoothScrollBy(mInitPos * size, 0);
365 | } else {
366 | smoothScrollBy(0, mInitPos * size);
367 | }
368 | }
369 | mInitPos = -1;
370 | }
371 |
372 | public interface OnItemClickListener {
373 | /**
374 | * 点击事件
375 | *
376 | * @param view View
377 | * @param position int
378 | */
379 | void onItemClick(View view, int position);
380 | }
381 | }
382 |
--------------------------------------------------------------------------------
/rv-gallery/src/main/java/com/ryan/rv_gallery/ScrollManager.java:
--------------------------------------------------------------------------------
1 | package com.ryan.rv_gallery;
2 |
3 | import android.support.v7.widget.LinearLayoutManager;
4 | import android.support.v7.widget.LinearSnapHelper;
5 | import android.support.v7.widget.PagerSnapHelper;
6 | import android.support.v7.widget.RecyclerView;
7 |
8 | import com.ryan.rv_gallery.util.DLog;
9 | import com.ryan.rv_gallery.util.OsUtil;
10 |
11 | /**
12 | * @author RyanLee
13 | * @date 2017/12/8
14 | */
15 |
16 | public class ScrollManager {
17 | private static final String TAG = "MainActivity_TAG";
18 |
19 | private GalleryRecyclerView mGalleryRecyclerView;
20 |
21 | private int mPosition = 0;
22 |
23 | /**
24 | * x方向消耗距离,使偏移量为左边距 + 左边Item的可视部分宽度
25 | */
26 | private int mConsumeX = 0;
27 | private int mConsumeY = 0;
28 |
29 | ScrollManager(GalleryRecyclerView mGalleryRecyclerView) {
30 | this.mGalleryRecyclerView = mGalleryRecyclerView;
31 | }
32 |
33 | /**
34 | * 初始化SnapHelper
35 | *
36 | * @param helper int
37 | */
38 | public void initSnapHelper(int helper) {
39 | switch (helper) {
40 | case GalleryRecyclerView.LINEAR_SNAP_HELPER:
41 | LinearSnapHelper mLinearSnapHelper = new LinearSnapHelper();
42 | mLinearSnapHelper.attachToRecyclerView(mGalleryRecyclerView);
43 | break;
44 | case GalleryRecyclerView.PAGER_SNAP_HELPER:
45 | PagerSnapHelper mPagerSnapHelper = new PagerSnapHelper();
46 | mPagerSnapHelper.attachToRecyclerView(mGalleryRecyclerView);
47 | break;
48 | default:
49 | break;
50 | }
51 | }
52 |
53 | /**
54 | * 监听RecyclerView的滑动
55 | */
56 | public void initScrollListener() {
57 | GalleryScrollerListener mScrollerListener = new GalleryScrollerListener();
58 | mGalleryRecyclerView.addOnScrollListener(mScrollerListener);
59 | }
60 |
61 | public void updateConsume() {
62 | mConsumeX += OsUtil.dpToPx(mGalleryRecyclerView.getDecoration().mLeftPageVisibleWidth + mGalleryRecyclerView.getDecoration().mPageMargin * 2);
63 | mConsumeY += OsUtil.dpToPx(mGalleryRecyclerView.getDecoration().mLeftPageVisibleWidth + mGalleryRecyclerView.getDecoration().mPageMargin * 2);
64 | DLog.d(TAG, "ScrollManager updateConsume mConsumeX=" + mConsumeX);
65 | }
66 |
67 | class GalleryScrollerListener extends RecyclerView.OnScrollListener {
68 |
69 | @Override
70 | public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
71 | DLog.d(TAG, "ScrollManager newState=" + newState);
72 | super.onScrollStateChanged(recyclerView, newState);
73 | }
74 |
75 | @Override
76 | public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
77 | super.onScrolled(recyclerView, dx, dy);
78 |
79 | if (mGalleryRecyclerView.getOrientation() == LinearLayoutManager.HORIZONTAL) {
80 | onHorizontalScroll(recyclerView, dx);
81 | } else {
82 | onVerticalScroll(recyclerView, dy);
83 | }
84 | }
85 | }
86 |
87 | /**
88 | * 垂直滑动
89 | *
90 | * @param recyclerView RecyclerView
91 | * @param dy int
92 | */
93 | private void onVerticalScroll(final RecyclerView recyclerView, int dy) {
94 | mConsumeY += dy;
95 |
96 | // 让RecyclerView测绘完成后再调用,避免GalleryAdapterHelper.mItemHeight的值拿不到
97 | recyclerView.post(new Runnable() {
98 | @Override
99 | public void run() {
100 | int shouldConsumeY = mGalleryRecyclerView.getDecoration().mItemConsumeY;
101 |
102 | // 位置浮点值(即总消耗距离 / 每一页理论消耗距离 = 一个浮点型的位置值)
103 | float offset = (float) mConsumeY / (float) shouldConsumeY;
104 | // 获取当前页移动的百分值
105 | float percent = offset - ((int) offset);
106 |
107 | mPosition = (int) offset;
108 |
109 | DLog.i(TAG, "ScrollManager offset=" + offset + "; mConsumeY=" + mConsumeY + "; shouldConsumeY=" + mPosition);
110 |
111 |
112 | // 设置动画变化
113 | mGalleryRecyclerView.getAnimManager().setAnimation(recyclerView, mPosition, percent);
114 | }
115 | });
116 | }
117 |
118 | /**
119 | * 水平滑动
120 | *
121 | * @param recyclerView RecyclerView
122 | * @param dx int
123 | */
124 | private void onHorizontalScroll(final RecyclerView recyclerView, final int dx) {
125 | mConsumeX += dx;
126 |
127 | // 让RecyclerView测绘完成后再调用,避免GalleryAdapterHelper.mItemWidth的值拿不到
128 | recyclerView.post(new Runnable() {
129 | @Override
130 | public void run() {
131 | int shouldConsumeX = mGalleryRecyclerView.getDecoration().mItemConsumeX;
132 |
133 | // 位置浮点值(即总消耗距离 / 每一页理论消耗距离 = 一个浮点型的位置值)
134 | float offset = (float) mConsumeX / (float) shouldConsumeX;
135 |
136 | // 获取当前页移动的百分值
137 | float percent = offset - ((int) offset);
138 |
139 | mPosition = (int) offset;
140 |
141 | DLog.i(TAG, "ScrollManager offset=" + offset + "; percent=" + percent + "; mConsumeX=" + mConsumeX + "; shouldConsumeX=" + shouldConsumeX + "; position=" + mPosition);
142 |
143 | // 设置动画变化
144 | mGalleryRecyclerView.getAnimManager().setAnimation(recyclerView, mPosition, percent);
145 | }
146 | });
147 |
148 | }
149 |
150 | public int getPosition() {
151 | return mPosition;
152 | }
153 | }
154 |
--------------------------------------------------------------------------------
/rv-gallery/src/main/java/com/ryan/rv_gallery/util/DLog.java:
--------------------------------------------------------------------------------
1 | package com.ryan.rv_gallery.util;
2 |
3 | import android.util.Log;
4 |
5 | /**
6 | *
7 | * @author RyanLee
8 | * @date 2018/2/2
9 | */
10 |
11 | public class DLog {
12 |
13 | private static boolean isDebug = false;
14 |
15 | public static void v(String tag, String msg) {
16 | if (isDebug) {
17 | Log.v(tag, msg);
18 | }
19 | }
20 |
21 | public static void d(String tag, String msg) {
22 | if (isDebug) {
23 | Log.d(tag, msg);
24 | }
25 | }
26 |
27 | public static void i(String tag, String msg) {
28 | if (isDebug) {
29 | Log.i(tag, msg);
30 | }
31 | }
32 |
33 | public static void w(String tag, String msg) {
34 | if (isDebug) {
35 | Log.w(tag, msg);
36 | }
37 | }
38 |
39 | public static void e(String tag, String msg) {
40 | if (isDebug) {
41 | Log.e(tag, msg);
42 | }
43 | }
44 |
45 |
46 | public static void setDebug(boolean debug) {
47 | isDebug = debug;
48 | }
49 | }
50 |
--------------------------------------------------------------------------------
/rv-gallery/src/main/java/com/ryan/rv_gallery/util/OsUtil.java:
--------------------------------------------------------------------------------
1 | package com.ryan.rv_gallery.util;
2 |
3 | import android.app.Activity;
4 | import android.content.res.Resources;
5 | import android.util.DisplayMetrics;
6 |
7 | /**
8 | *
9 | * @author RyanLee
10 | * @date 2017/11/29
11 | */
12 |
13 | public class OsUtil {
14 | public static int dpToPx(int dp) {
15 | return (int) (dp * Resources.getSystem().getDisplayMetrics().density);
16 | }
17 |
18 | public static int pxToDp(int px) {
19 | return (int) (px / Resources.getSystem().getDisplayMetrics().density);
20 | }
21 |
22 |
23 | /**
24 | * 获取屏幕宽度
25 | *
26 | * @return
27 | */
28 | public static int getScreenWidth() {
29 | return Resources.getSystem().getDisplayMetrics().widthPixels;
30 | }
31 |
32 | /**
33 | * 获取屏幕高度
34 | *
35 | * @return
36 | */
37 | public static int getScreenHeigth() {
38 | return Resources.getSystem().getDisplayMetrics().heightPixels;
39 | }
40 |
41 | /**
42 | * 获取屏幕宽度
43 | *
44 | * @return
45 | */
46 | public static int getScreenWidth(Activity activity) {
47 | DisplayMetrics outMetrics = new DisplayMetrics();
48 | activity.getWindowManager().getDefaultDisplay().getMetrics(outMetrics);
49 | return outMetrics.widthPixels;
50 |
51 | }
52 | }
53 |
--------------------------------------------------------------------------------
/rv-gallery/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | rv-gallery
3 |
4 |
--------------------------------------------------------------------------------
/rv-gallery/src/main/res/values/value.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/rv-gallery/src/test/java/com/ryan/rv_gallery/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.ryan.rv_gallery;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * @see Testing documentation
11 | */
12 | public class ExampleUnitTest {
13 | @Test
14 | public void addition_isCorrect() throws Exception {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app', ':rv-gallery'
2 |
--------------------------------------------------------------------------------
/用RecyclerView做一个小清新的Gallery效果.md:
--------------------------------------------------------------------------------
1 | # 用RecyclerView做一个小清新的Gallery效果 #
2 |
3 |
4 | ## 一、简介 ##
5 | RecyclerView现在已经是越来越强大,且不说已经被大家用到滚瓜烂熟的代替ListView的基础功能,现在RecyclerView还可以取代ViewPager实现Banner效果,当然,以下做的小清新的Gallery效果也是类似于一些轮播图的效果,如下图所示,这其中使用到了24.2.0版本后RecyclerView增加的SnapHelper这个辅助类,在实现以下效果起来也是非常简单。所以这也是为什么RecyclerView强大之处,因为Google一直在对RecyclerView不断地进行更新补充,从而它内部的API也是越来越丰富。
6 |
7 |
8 | 
9 |
10 | 
11 |
12 |
13 | 那么我们从水平滑动为例,我们细分为以下几个小问题:
14 |
15 | 1. 每一次滑动都让图片保持在正中间。
16 | 2. 第一张图片的左边距和最后一张的右边距需要保持和其他照片的左右边距一样。
17 | 3. 滑动时,中间图片滑动到左边时从大变小,右边图片滑动到中间时从小变大。
18 | 4. 背景实现高斯模糊。
19 | 5. 滑动结束时背景有一个渐变效果,从上一张图片淡入淡出到当前图片。
20 |
21 | ## 二、实现思路 ##
22 |
23 | 解决以上问题当然也不难,我们分步来讲解下实现思路:
24 |
25 | ### (1) 每一次滑动都让图片保持在正中间 ###
26 |
27 | 保持让图片保持在正中间,正如简介中所说,在ToolsVersion24.2.0之后,Google给我们提供了一个`SnapHelper`的辅助类,它只需要几行代码就能帮助我们实现滑动结束时保持在居中位置:
28 |
29 | LinearSnapHelper mLinearySnapHelper = new LinearSnapHelper();
30 | mLinearySnapHelper.attachToRecyclerView(mGalleryRecyclerView);
31 |
32 | `LinearSnapHelper`类继承于`SnapHelper`,当然`SnapHelper`还有一个子类,叫做`PagerSnapHelper`。它们之间的区别是,`LinearSnapHelper`可以使RecyclerView一次滑动越过多个Item,而`PagerSnapHelper`像ViewPager一样限制你一次只能滑动一个Item。
33 |
34 | ### (2) 第一张图片的左边距和最后一张的右边距需要保持和其他照片的左右边距一样 ###
35 |
36 |
37 |
38 | 由于第0个位置,和最后一个位置的图片比较特殊,其他图片都默认设置他们的**页边距**和**左右图片的可视距离**,由于第0页左边没有图片,所以左边只有1倍页边距,这样滑动到最左边时看起来就会比较奇怪,如下图所示。
39 |
40 | 
41 |
42 | 让第0位置的图片左边保持和其他图片一样的距离,那么就需要动态设置第0位置图片的左边距为**2倍页边距 + 可视距离**。同理,最后一张也是做同样的操作。
43 |
44 | 动态修改图片的`LayoutParams`,由于RecyclerView对Holder的复用机制,我们最好不要在Adapter里面动态修改,这样子首先不够优雅,这里感谢`@W_BinaryTree`的建议,我们给RecyclerView添加一个自定义的Decoration会让我们的代码更加优雅,只需要重写`RecyclerView.ItemDecoration`里面的`getItemOffsets(Rect outRect, final View view, final RecyclerView parent, RecyclerView.State state)`方法,并在里面设置每一页的参数即可,修改如下:
45 |
46 | public class GalleryItemDecoration extends RecyclerView.ItemDecoration {
47 | int mPageMargin = 0; // 每一个页面默认页边距
48 | int mLeftPageVisibleWidth = 50; // 中间页面左右两边的页面可见部分宽度
49 |
50 | public static int mItemComusemX = 0; // 一页理论消耗距离
51 |
52 |
53 | @Override
54 | public void getItemOffsets(Rect outRect, final View view, final RecyclerView parent, RecyclerView.State state) {
55 | super.getItemOffsets(outRect, view, parent, state);
56 | // ...
57 |
58 | // 动态修改页面的宽度
59 | int itemNewWidth = parent.getWidth() - dpToPx(4 * mPageMargin + 2 * mLeftPageVisibleWidth);
60 |
61 | // 一页理论消耗距离
62 | mItemComusemX = itemNewWidth + OsUtil.dpToPx(2 * mPageMargin);
63 |
64 | // 第0页和最后一页没有左页面和右页面,让他们保持左边距和右边距和其他项一样
65 | int leftMargin = position == 0 ? dpToPx(mLeftPageVisibleWidth + 2 * mPageMargin) : dpToPx(mPageMargin);
66 | int rightMargin = position == itemCount - 1 ? dpToPx(mLeftPageVisibleWidth + 2 * mPageMargin) : dpToPx(mPageMargin);
67 |
68 | // 设置参数
69 | RecyclerView.LayoutParams lp = (RecyclerView.LayoutParams) itemView.getLayoutParams();
70 | lp.setMargins(leftMargin, 0, rightMargin, 0);
71 | lp.width = itemWidth;
72 | itemView.setLayoutParams(lp);
73 |
74 |
75 | // ...
76 |
77 | }
78 |
79 | public int dpToPx(int dp) {
80 | return (int) (dp * Resources.getSystem().getDisplayMetrics().density + 0.5f);
81 | }
82 | }
83 |
84 | 然后,把`GalleryItemDecoration`传入即可:
85 |
86 | mGalleryRecyclerView.addItemDecoration(new GalleryItemDecoration());
87 |
88 |
89 |
90 |
91 | ### (3) 滑动时,中间图片滑动到左边时从大变小,右边图片滑动到中间时从小变大 ###
92 |
93 | 这个问题涉及到比较多的问题。
94 |
95 | **(a) 获取滑动过程中当前位置。**
96 |
97 | 首先,RecyclerView当前的API,并不能让我们在滑动的过程中,简单地获取到我们图中效果中间图片的位置,或许你会说,可以通过
98 | `mGalleryRecyclerView.getLinearLayoutManager().findFirstVisibleItemPosition()`能拿到RecyclerView中第一个可见的位置,但是通过效果可以知道,我们每一个张照片(除去第一张和最后一张)左右两边都是有前一张照片和最后一张照片的部分内容的,所以需要做区分判断是否是中间的照片还是第一张亦或最后一张,然后返回`mGalleryRecyclerView.getLinearLayoutManager().findFirstVisibleItemPosition() + 1`或者其他。 那么这样又会引出一个问题,当我们把前后照片展示的宽度设置成可配置,即前后照片的露出部分宽度是可配置,那么当我们把屏幕不显示前后照片遗留部分在屏幕的话,那么我们这一个方法又不能兼容了,所以通过这一个方法来获取,或许不那么靠谱。
99 |
100 | 我们可以这样来计算出比较准确的位置。在RecyclerView中,我们可以监听它的滑动事件:
101 |
102 | // 滑动监听
103 | mGalleryRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
104 | @Override
105 | public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
106 | super.onScrollStateChanged(recyclerView, newState);
107 | }
108 |
109 | @Override
110 | public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
111 | super.onScrolled(recyclerView, dx, dy);
112 |
113 | // 通过dx或者dy来计算位置。
114 | }
115 | });
116 |
117 | 里面有一个`onScrolled(int dx, int dy)`方法,这里面的dx,dy非常有用。首先,通过判断dx,dy是否大于0可以判断它是上、下、左、右滑动,dx > 0右滑,反之左滑,dy > 0 下滑,反之上滑(当然,我这里的滑动是相对于RecyclerView,即列表的滑动方向,手指的滑动方向和这里相反)。其次,dx和dy还能监听每一次滑动在x,y轴上消耗的距离。
118 |
119 | 举个例子,当我们迅速至列表右边时,`onScrolled(int dx, int dy)`会不断被调用,通过在方法里面Log输出,你会看到不断输出dx的值,而且他们的大小都是无规律的,而这里的dx就是每一次`onScroll`方法调用一次,RecyclerView在x轴上的消耗距离。
120 |
121 | 所以我们可以通过一个全局变量`mConsumeX`来累加所有dx,当这样我们就可以知道当前RecyclerView滑动的总距离。而我们Demo中每移动到下一张照片的距离(即如下图中所示的**移动一页理论消耗距离**)是一定的,那么就可以通过`当前位置 = mConsumeX / 移动一张照片所需要的距离`来获取滑动结束时的位置了。
122 |
123 | 
124 |
125 | /**
126 | * 获取位置
127 | *
128 | * @param mConsumeX 实际消耗距离
129 | * @param shouldConsumeX 移动一页理论消耗距离
130 | * @return
131 | */
132 | private int getPosition(int mConsumeX, int shouldConsumeX) {
133 | float offset = (float) mConsumeX / (float) shouldConsumeX;
134 | int position = Math.round(offset); // 四舍五入获取位置
135 | return position;
136 | }
137 |
138 | **(b) 根据位置获取当前页的滑动偏移率**
139 |
140 | 当我们可以准确拿到当前位置时,我们就需要明确一下几个概念。
141 |
142 | `总的偏移距离`:意思是从第一个位置移动到现在当前位置偏移的总距离,即dx的累加结果(也就是上述的mConsumX)。
143 |
144 | `当前页偏移距离`:意思是从上一个位置移动到当前位置偏移距离。
145 |
146 | `总的偏移率`:意思是 总的偏移距离 / 移动一页理论消耗距离。
147 |
148 | `当前页的偏移率`:意思是 当前页偏移距离 / 移动一页理论消耗距离。
149 |
150 |
151 | 
152 |
153 | 我们都知道,获取当前位置方法里面有一个
154 |
155 | float offset = (float) mConsumeX / (float) shouldConsumeX;
156 |
157 | 它的意思就是总的偏移率,例如图中我们当前位置是3,我们从3移动到4时,`onScroll`方法会不断被调用,那么这个offset就会不断变化,从3.0逐渐增加一直到4.0,图中此时的offset大概是3.2左右,我们知道这一个有什么用呢?试想一下,offset是一个浮点型数,将它向下取整,那就是变成3了,那么3.2 - 3 = 0.2就是我们当前页的偏移率了。而我们通过偏移率就可以动态设置图片的大小,就形成了我们这个问题中所说的图片大小变化效果。所以这里的关键就是获取到`当前页的偏移率`。
158 |
159 | @Override
160 | public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
161 | super.onScrolled(recyclerView, dx, dy);
162 |
163 | // ...
164 |
165 |
166 | // 移动一页理论消耗距离
167 | int shouldConsumeX = GalleryItemDecoration.mItemComusemX;
168 |
169 |
170 | // 获取当前的位置
171 | int position = getPosition(mConsumeX, shouldConsumeX);
172 |
173 | // 位置浮点值(即总消耗距离 / 每一页理论消耗距离 = 一个浮点型的位置值)
174 | float offset = (float) mConsumeX / (float) shouldConsumeX;
175 |
176 | // 避免offset值取整时进一,从而影响了percent值
177 | if (offset >= mGalleryRecyclerView.getLinearLayoutManager().findFirstVisibleItemPosition() + 1 && slideDirct == SLIDE_RIGHT) {
178 | return;
179 | }
180 |
181 | // 当前页的偏移率
182 | float percent = offset - ((int) offset);
183 |
184 |
185 | // 设置动画变化
186 | setAnimation(recyclerView, position, percent);
187 |
188 | // ...
189 |
190 | }
191 |
192 | **(c) 根据偏移率实现动画**
193 |
194 | 现在我们拿到了偏移率,就可以动态修改它们的尺寸大小了,首先,我们需要拿到当前View,前一个View和后一个View,并同时对它们做Scale伸缩。即上面的`setAnimation(recyclerView, position, percent)`方法里面进行动画操作。
195 |
196 | View mCurView = recyclerView.getLayoutManager().findViewByPosition(position); // 中间页
197 | View mRightView = recyclerView.getLayoutManager().findViewByPosition(position + 1); // 左边页
198 | View mLeftView = recyclerView.getLayoutManager().findViewByPosition(position - 1); // 右边页
199 |
200 | 认真观察图中变化,两种变化:
201 |
202 | 1. 位置的变化:第一张图片是从mCurView慢慢变成mLeftView,而第二张图片是从mRightView慢慢变成mCurView。
203 | 2. 大小变化:第一张图是从大变小,第二张图是从小变大。
204 |
205 | 理解了以上的变化之后,我们就可以做动画了。
206 |
207 | 
208 |
209 | 首先说明一点,大家观察我的`getPosition(mConsumeX, shouldConsumeX)`方法,里面的实现是,当一页滑动的偏移率超过了0.5之后,position就会自动切换到下一页。当然你的实现逻辑不一样,那么后面你的设置动画的方法就不一样。为什么需要明确这一点呢?因为当我滑动超过图片超过它的一半宽度之后,上面的mCurView就会切换成下一张图片了,所以我在设置动画的方法里以0.5为一个临界点,因为0.5临界点的两边,`mCurView`,`mRightView`,`mLeftView`的指向都已经不一样了。
210 |
211 | 假如我们定义大小变化因子 `float mAnimFactor = 0.2f`,它的意思就是控制我们的图片从1.0伸缩至0.8。以上图为例,当percent <= 0.5时,`mCurView`的ScaleX和ScaleY从大慢慢变小,至于这个变化范围,就根据我们定义的变化因子和percent来修改;而当percent > 0.5时,刚才那个View就变成了`mLeftView`,此时我们继续刚才的操作,整个过程我们就实现了第一张图片的Scale从1.0变化到了0.8。而另外两张图片也是同理,大概代码逻辑如下:
212 |
213 |
214 | private void setBottomToTopAnim(RecyclerView recyclerView, int position, float percent) {
215 | View mCurView = recyclerView.getLayoutManager().findViewByPosition(position); // 中间页
216 | View mRightView = recyclerView.getLayoutManager().findViewByPosition(position + 1); // 左边页
217 | View mLeftView = recyclerView.getLayoutManager().findViewByPosition(position - 1); // 右边页
218 |
219 |
220 | if (percent <= 0.5) {
221 | if (mLeftView != null) {
222 | // 变大
223 | mLeftView.setScaleX((1 - mAnimFactor) + percent * mAnimFactor);
224 | mLeftView.setScaleY((1 - mAnimFactor) + percent * mAnimFactor);
225 | }
226 | if (mCurView != null) {
227 | // 变小
228 | mCurView.setScaleX(1 - percent * mAnimFactor);
229 | mCurView.setScaleY(1 - percent * mAnimFactor);
230 | }
231 | if (mRightView != null) {
232 | // 变大
233 | mRightView.setScaleX((1 - mAnimFactor) + percent * mAnimFactor);
234 | mRightView.setScaleY((1 - mAnimFactor) + percent * mAnimFactor);
235 | }
236 | } else {
237 | if (mLeftView != null) {
238 | mLeftView.setScaleX(1 - percent * mAnimFactor);
239 | mLeftView.setScaleY(1 - percent * mAnimFactor);
240 | }
241 | if (mCurView != null) {
242 | mCurView.setScaleX((1 - mAnimFactor) + percent * mAnimFactor);
243 | mCurView.setScaleY((1 - mAnimFactor) + percent * mAnimFactor);
244 | }
245 | if (mRightView != null) {
246 | mRightView.setScaleX(1 - percent * mAnimFactor);
247 | mRightView.setScaleY(1 - percent * mAnimFactor);
248 | }
249 | }
250 | }
251 |
252 | ### (4)背景实现高斯模糊 ###
253 |
254 | 高斯模糊有挺多种实现方法的,Google一下就出来了。但是还是推荐Native层的实现算法,因为Java层的实现对性能影响实在太大了,例子里使用的是`RenderScript`,当然是参考博主`湫水`的[教你一分钟实现动态模糊效果](http://wl9739.github.io/2016/07/14/%E6%95%99%E4%BD%A0%E4%B8%80%E5%88%86%E9%92%9F%E5%AE%9E%E7%8E%B0%E6%A8%A1%E7%B3%8A%E6%95%88%E6%9E%9C/),大家感兴趣可以过去看看,用法也是非常简单。直接调用`blurBitmap(Context context, Bitmap image, float blurRadius)`方法即可。
255 |
256 | public class BlurBitmapUtil {
257 | //图片缩放比例
258 | private static final float BITMAP_SCALE = 0.4f;
259 |
260 | /**
261 | * 模糊图片的具体方法
262 | *
263 | * @param context 上下文对象
264 | * @param image 需要模糊的图片
265 | * @return 模糊处理后的图片
266 | */
267 | public static Bitmap blurBitmap(Context context, Bitmap image, float blurRadius) {
268 | // 计算图片缩小后的长宽
269 | int width = Math.round(image.getWidth() * BITMAP_SCALE);
270 | int height = Math.round(image.getHeight() * BITMAP_SCALE);
271 |
272 | // 将缩小后的图片做为预渲染的图片
273 | Bitmap inputBitmap = Bitmap.createScaledBitmap(image, width, height, false);
274 | // 创建一张渲染后的输出图片
275 | Bitmap outputBitmap = Bitmap.createBitmap(inputBitmap);
276 |
277 | // 创建RenderScript内核对象
278 | RenderScript rs = RenderScript.create(context);
279 | // 创建一个模糊效果的RenderScript的工具对象
280 | ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
281 |
282 | // 由于RenderScript并没有使用VM来分配内存,所以需要使用Allocation类来创建和分配内存空间
283 | // 创建Allocation对象的时候其实内存是空的,需要使用copyTo()将数据填充进去
284 | Allocation tmpIn = Allocation.createFromBitmap(rs, inputBitmap);
285 | Allocation tmpOut = Allocation.createFromBitmap(rs, outputBitmap);
286 |
287 | // 设置渲染的模糊程度, 25f是最大模糊度
288 | blurScript.setRadius(blurRadius);
289 | // 设置blurScript对象的输入内存
290 | blurScript.setInput(tmpIn);
291 | // 将输出数据保存到输出内存中
292 | blurScript.forEach(tmpOut);
293 |
294 | // 将数据填充到Allocation中
295 | tmpOut.copyTo(outputBitmap);
296 |
297 | return outputBitmap;
298 | }
299 | }
300 |
301 | 这个方法只要传入Context,Bitmap,和一个模糊程度即可,然后返回一个高斯模糊后的Bitmap给我们,我们只需要将RecyclerView的父布局设置背景为这个Bitmap即可。
302 |
303 | ### (5)滑动结束时背景有一个渐变效果,从上一张图片淡入淡出到当前图片 ###
304 |
305 | 实现这个效果最好不要使用Tween动画,因为它的实现效果比较生硬,使用`TransitionDrawable`会让效果更佳接近淡入淡出效果。那我们怎么记录前后两个位置的照片呢?方法很多种,这里就使用了一个Map来记录每一次显示的图片,在它切换到下一个图片时,便从上一次记录的图片淡入淡出到本次的图片。
306 |
307 | // 获取当前位置的图片资源ID
308 | int resourceId = ((RecyclerAdapter) mRecyclerView.getAdapter()).getResId(mRecyclerView.getScrolledPosition());
309 | // 将该资源图片转为Bitmap
310 | Bitmap resBmp = BitmapFactory.decodeResource(getResources(), resourceId);
311 | // 将该Bitmap高斯模糊后返回到resBlurBmp
312 | Bitmap resBlurBmp = BlurBitmapUtil.blurBitmap(mRecyclerView.getContext(), resBmp, 15f);
313 | // 再将resBlurBmp转为Drawable
314 | Drawable resBlurDrawable = new BitmapDrawable(resBlurBmp);
315 | // 获取前一页的Drawable
316 | Drawable preBlurDrawable = mTSDraCacheMap.get(KEY_PRE_DRAW) == null ? resBlurDrawable : mTSDraCacheMap.get(KEY_PRE_DRAW);
317 |
318 | /* 以下为淡入淡出效果 */
319 | Drawable[] drawableArr = {preBlurDrawable, resBlurDrawable};
320 | TransitionDrawable transitionDrawable = new TransitionDrawable(drawableArr);
321 | mContainer.setBackgroundDrawable(transitionDrawable);
322 | transitionDrawable.startTransition(500);
323 |
324 | // 存入到cache中
325 | mTSDraCacheMap.put(KEY_PRE_DRAW, resBlurDrawable);
326 |
327 | ## 更多 ##
328 |
329 | 以上所讲的都是实现的一个思路,虽然效果和小清新搭不上关系哈,但是配了几张小清新的图片还是让我们的程序员生活增添一丝精彩。其实大家实现了基础效果之后,还可以深挖更多辅助功能,例如不同的切换效果,支持横屏,动态修改滑动速度等,相信这个过程可以让你收获良多。
330 |
331 | Github:[Recyclerview-Gallery](https://github.com/ryanlijianchang/Recyclerview-Gallery)
332 |
333 |
--------------------------------------------------------------------------------