├── .gitignore
├── .travis.yml
├── LICENSE
├── README.md
├── WheelViewDemo.iml
├── androidWheelView-eclipse.rar
├── app
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src
│ └── main
│ ├── AndroidManifest.xml
│ ├── java
│ └── com
│ │ └── weidongjian
│ │ └── meitu
│ │ └── wheelviewdemo
│ │ ├── DialogActivity.java
│ │ ├── MainActivity.java
│ │ └── ScrollViewActivity.java
│ └── res
│ ├── layout
│ ├── activity_dialog.xml
│ ├── activity_main.xml
│ ├── activity_scroll_view.xml
│ └── dialog_view.xml
│ ├── menu
│ └── menu_main.xml
│ ├── mipmap-hdpi
│ └── ic_launcher.png
│ ├── mipmap-mdpi
│ └── ic_launcher.png
│ ├── mipmap-xhdpi
│ └── ic_launcher.png
│ ├── mipmap-xxhdpi
│ └── ic_launcher.png
│ ├── values-w820dp
│ └── dimens.xml
│ └── values
│ ├── dimens.xml
│ ├── strings.xml
│ └── styles.xml
├── build.gradle
├── gradle.properties
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── loopview
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src
│ └── main
│ ├── AndroidManifest.xml
│ ├── java
│ └── com
│ │ └── weigan
│ │ └── loopview
│ │ ├── InertiaTimerTask.java
│ │ ├── LoopView.java
│ │ ├── LoopViewGestureListener.java
│ │ ├── MessageHandler.java
│ │ ├── OnItemScrollListener.java
│ │ ├── OnItemSelectedListener.java
│ │ ├── OnItemSelectedRunnable.java
│ │ └── SmoothScrollTimerTask.java
│ └── res
│ └── values
│ ├── attrs.xml
│ └── strings.xml
├── photo
├── 1625735942774356.gif
├── Gif_20161230_162613.gif
└── circle.jpg
└── settings.gradle
/.gitignore:
--------------------------------------------------------------------------------
1 | .gradle
2 | /local.properties
3 | /.idea/workspace.xml
4 | /.idea/libraries
5 | .DS_Store
6 | /build
7 | /captures
8 | androidWheelView.iml
9 | .idea/.name
10 | .idea/gradle.xml
11 | .idea/misc.xml
12 | .idea/modules.xml
13 | app/app.iml
14 | .idea/vcs.xml
15 | loopview/loopview.iml
16 |
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | # 声明构建语言环境
2 | language: android
3 | android: # 配置信息
4 | components:
5 | # 你可能需要修改下面两项的版本
6 | - build-tools-25.0.0
7 | - android-25
8 | # Android Support Repository
9 | - extra-android-m2repository
10 | # Support Library
11 | - extra-android-support
12 | script:
13 | # 生成 APK
14 | - ./gradlew assembleRelease
15 |
16 | # 部署
17 | deploy:
18 | # 部署到GitHub Release。
19 | # 除此之外,Travis CI还支持发布到fir.im、AWS、Google App Engine等
20 | provider: releases
21 | # Github oauth token
22 | api_key: "GITHUB OAUTH TOKEN"
23 | # 部署文件路径;对于Android就部署生成的 apk 文件
24 | file: "FILE TO UPLOAD"
25 | # 避免 Travis CI在部署之前清空生成的APK文件
26 | skip_cleanup: true
27 | # 发布时机
28 | on:
29 | # tags设置为true表示只有在有tag的情况下才部署
30 | tags: true
31 |
--------------------------------------------------------------------------------
/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 | # androidWheelView
2 | 仿照iOS的滚轮控件,从请吃饭apk反编译出来的
3 |
4 | 具体的请查看这个博客:http://www.jianshu.com/p/fa7adfa90c68
5 |
6 | 由于Jcenter库停止维护,现迁移到jitpack,依赖如下
7 | ### 在根目录的build.gradle增加jitpack
8 | ```gradle
9 | allprojects {
10 | repositories {
11 | ...
12 | maven { url 'https://jitpack.io' }
13 | }
14 | }
15 | ```
16 |
17 | ### 添加依赖
18 | ```gradle
19 | dependencies {
20 | implementation 'com.github.weidongjian:androidWheelView:1.0.0'
21 | }
22 | ```
23 |
24 |
25 |
26 | 效果图如下:
27 |
28 | 
29 |
30 |
31 |
32 | ### 常见问题
33 |
34 | 1. 设置初始位置:调用`setInitPosition()`,不是`setCurrentPosition()`。
35 |
36 | ### 更新历史
37 | **版本号:1.0.0 更新时间:2021.7.08**
38 |
39 | 增加对无3D效果的配置:app:awv_isCurve="false"
40 |
41 | **版本号:0.9.1 更新时间:2021.7.06**
42 |
43 | 修复setCurrentPosition后,对应的位置不会回调的异常
44 |
45 | **版本号:0.2.2 更新时间:2019.10.30**
46 |
47 | 修复设置当前位置为0无效的异常
48 |
49 | **版本号:0.2.1 更新时间:2019.6.25**
50 |
51 | 修复一波bug
52 |
53 | **版本号:0.1.2 更新时间:2017.3.25**
54 |
55 | 1. 适配`setCurrentPosition(0)`的场景
56 | 2. 适配嵌套在`ScrollerView`滑动冲突
57 | 3. 适配在`dialog`中显示
58 |
59 |
60 |
61 | ## 2016-12-30:各种性能效果做了很大的更新
62 |
63 | 同时,提供各种参数接口,包括文本大小,显示数量,控件颜色等各种参数
64 | #### Description of Attributes
65 |
66 | | Attributes | Format | Default | Description |
67 | | :-------------------: | :-----: | :--------: | :----------------: |
68 | | awv_textsize | integer | 15 | textsize |
69 | | awv_lineSpace | float | 2.0f | line space |
70 | | awv_centerTextColor | integer | oxff313131 | center text color |
71 | | awv_outerTextColor | integer | 0xffafafaf | outer text color |
72 | | awv_dividerTextColor | integer | oxff313131 | center text color |
73 | | awv_itemsVisibleCount | integer | 9 | visible item count |
74 | | awv_isLoop | boolean | true | is loop mode |
75 |
76 | 
77 |
78 | ## 滚动效果类似一个圆柱 ##
79 | ### 绘制 ###
80 |
81 | 在onMeasure方法中计算控件宽和高以及初始化画笔
82 |
83 | itemHeight = lineSpacingMultiplier * maxTextHeight
84 |
85 | 圆柱半圆周为itemHeight*(itemCount - 1)
86 |
87 | 计算出圆柱直径即为控件高度
88 |
89 | 在onTouchEvent方法中计算圆柱滚动距离totalScrollY
90 |
91 | ### 在onDraw方法中绘制控件 ###
92 |
93 | 计算滚动了多少个条目change = (int) ((float) totalScrollY / itemHeight);
94 |
95 | 计算当前条目位置preCurrentIndex(处理超出边界的情况)
96 | 计算滚动超出条目的位移:
97 | int j2 = (int) ((float) totalScrollY % itemHeight);
98 |
99 | 绘制各个条目:
100 |
101 | 计算条目弧度radian
102 |
103 | 计算图纸canvas偏移量
104 |
105 | 图中的弧度标错了,应该标它的补角,那样感觉不好理解,大家自行脑补一下
106 |
107 | h2也标错了,画图的时候忘记考虑空白区域了,h2应该是文字高度的sin值
108 |
109 | double h1 = Math.cos(radian) * (double) radius;
110 |
111 | double h2 = (Math.sin(radian) * (double) maxTextHeight) / 2D)
112 |
113 | int translateY = (int) ((double) radius h1 h2;
114 |
115 | 图纸延Y方向缩放,值为弧度radian的sin值(这样出来的效果就感觉是个圆柱)
116 |
117 | ### 最后分不同情况绘制各个条目 ###
118 | 1. 偏移量translateY y值小于第一条线firstLineY y值的并且偏移量translateY+maxTextHeight大于第一条线y值小于第一条线firstLineY y值的(即第一条线穿过该条目文字)
119 | 2. 条目文字穿过第二条线的情况
120 | 3. 条目刚好在两条线中间的
121 | 4. 其他情况
122 |
123 | onTouchEvent方法,当手离开控件时开始平滑滚动控件
124 |
125 | LoopViewGestureListener处理手势,当按下时取消所有的滚动,当滑行时,平滑滚动
126 |
127 | 平滑滚动用定时器ScheduledExecutorService来处理
128 |
129 | 哪里写的不对的多多指正。
130 |
--------------------------------------------------------------------------------
/WheelViewDemo.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
--------------------------------------------------------------------------------
/androidWheelView-eclipse.rar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/weidongjian/androidWheelView/47f07204434598fbb1a877a6ca3409b11f7cb3e4/androidWheelView-eclipse.rar
--------------------------------------------------------------------------------
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 29
5 | buildToolsVersion "29.0.3"
6 |
7 | defaultConfig {
8 | applicationId "com.weidongjian.meitu.wheelviewdemo"
9 | minSdkVersion 21
10 | targetSdkVersion 29
11 | versionCode 1
12 | versionName "1.0"
13 | // jackOptions {
14 | // enabled true
15 | // }
16 | }
17 | buildTypes {
18 | release {
19 | minifyEnabled false
20 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
21 | }
22 | debug {
23 | minifyEnabled false
24 | }
25 | }
26 | }
27 |
28 |
29 | dependencies {
30 | api fileTree(include: ['*.jar'], dir: 'libs')
31 | // api project(':loopview')
32 | api 'com.android.support:appcompat-v7:28.0.0'
33 | api 'com.github.weidongjian:androidWheelView:1.0.0'
34 | }
35 |
36 |
--------------------------------------------------------------------------------
/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 D:\Program Files (x86)\Android\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 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
10 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
--------------------------------------------------------------------------------
/app/src/main/java/com/weidongjian/meitu/wheelviewdemo/DialogActivity.java:
--------------------------------------------------------------------------------
1 | package com.weidongjian.meitu.wheelviewdemo;
2 |
3 | import android.content.DialogInterface;
4 | import android.os.Bundle;
5 | import android.support.annotation.Nullable;
6 | import android.support.v7.app.AlertDialog;
7 | import android.support.v7.app.AppCompatActivity;
8 | import android.view.LayoutInflater;
9 | import android.view.View;
10 | import android.widget.Toast;
11 |
12 | import com.weigan.loopview.LoopView;
13 | import com.weigan.loopview.OnItemSelectedListener;
14 |
15 | import java.util.ArrayList;
16 |
17 | /**
18 | * Created by Weidongjian on 2017/3/25.
19 | */
20 |
21 | public class DialogActivity extends AppCompatActivity {
22 |
23 | private Toast toast;
24 |
25 | @Override
26 | protected void onCreate(@Nullable Bundle savedInstanceState) {
27 | super.onCreate(savedInstanceState);
28 | setContentView(R.layout.activity_dialog);
29 |
30 | findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
31 | @Override
32 | public void onClick(View v) {
33 | showDialog();
34 | }
35 | });
36 | }
37 |
38 | private void showDialog() {
39 | AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
40 | LayoutInflater inflater = this.getLayoutInflater();
41 | View dialogView = inflater.inflate(R.layout.dialog_view, null);
42 | dialogBuilder.setView(dialogView);
43 |
44 | final LoopView loopView = (LoopView) dialogView.findViewById(R.id.loopView);
45 |
46 | dialogBuilder.setOnDismissListener(new DialogInterface.OnDismissListener() {
47 | @Override
48 | public void onDismiss(DialogInterface dialog) {
49 | int selectedItem = loopView.getSelectedItem();
50 | Toast.makeText(DialogActivity.this, "current item " + selectedItem, Toast.LENGTH_SHORT).show();
51 | }
52 | });
53 |
54 | ArrayList list = new ArrayList<>();
55 | for (int i = 0; i < 15; i++) {
56 | list.add("item " + i);
57 | }
58 | // 滚动监听
59 | loopView.setListener(new OnItemSelectedListener() {
60 | @Override
61 | public void onItemSelected(int index) {
62 | if (toast == null) {
63 | toast = Toast.makeText(DialogActivity.this, "item " + index, Toast.LENGTH_SHORT);
64 | }
65 | toast.setText("item " + index);
66 | toast.show();
67 |
68 | }
69 | });
70 | // 设置原始数据 测试提交触发构建 travis CI 合并代码
71 | loopView.setItems(list);
72 |
73 | AlertDialog alertDialog = dialogBuilder.create();
74 | alertDialog.show();
75 | }
76 | }
77 |
--------------------------------------------------------------------------------
/app/src/main/java/com/weidongjian/meitu/wheelviewdemo/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.weidongjian.meitu.wheelviewdemo;
2 |
3 | import android.content.Intent;
4 | import android.os.Bundle;
5 | import android.support.v7.app.AppCompatActivity;
6 | import android.util.Log;
7 | import android.view.View;
8 | import android.widget.Toast;
9 |
10 | import com.weigan.loopview.LoopView;
11 | import com.weigan.loopview.OnItemScrollListener;
12 | import com.weigan.loopview.OnItemSelectedListener;
13 |
14 | import java.util.ArrayList;
15 |
16 | public class MainActivity extends AppCompatActivity {
17 |
18 | private Toast toast;
19 |
20 | @Override
21 | protected void onCreate(Bundle savedInstanceState) {
22 | super.onCreate(savedInstanceState);
23 | setContentView(R.layout.activity_main);
24 |
25 | LoopView loopView = (LoopView) findViewById(R.id.loopView);
26 | initLoopView(loopView);
27 |
28 | LoopView loopView2 = (LoopView) findViewById(R.id.loopView2);
29 | initLoopView(loopView2);
30 |
31 | findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
32 | @Override
33 | public void onClick(View v) {
34 | loopView.setCurrentPosition(4);
35 | }
36 | });
37 |
38 | findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
39 | @Override
40 | public void onClick(View v) {
41 | Intent intent = new Intent(MainActivity.this, ScrollViewActivity.class);
42 | startActivity(intent);
43 | }
44 | });
45 | findViewById(R.id.button3).setOnClickListener(new View.OnClickListener() {
46 | @Override
47 | public void onClick(View v) {
48 | Intent intent = new Intent(MainActivity.this, DialogActivity.class);
49 | startActivity(intent);
50 | }
51 | });
52 | }
53 |
54 | private void initLoopView(LoopView loopView) {
55 | ArrayList list = new ArrayList<>();
56 | for (int i = 0; i < 60; i++) {
57 | list.add("item gp" + i);
58 | }
59 | //滚动监听
60 | loopView.setListener(new OnItemSelectedListener() {
61 | @Override
62 | public void onItemSelected(int index) {
63 | if (toast == null) {
64 | toast = Toast.makeText(MainActivity.this, "item " + index, Toast.LENGTH_SHORT);
65 | }
66 | toast.setText("item " + index);
67 | toast.show();
68 | }
69 | });
70 | loopView.setOnItemScrollListener(new OnItemScrollListener() {
71 | @Override
72 | public void onItemScrollStateChanged(LoopView loopView, int currentPassItem, int oldScrollState, int scrollState, int totalScrollY) {
73 | Log.i("gy", String.format("onItemScrollStateChanged currentPassItem %d oldScrollState %d scrollState %d totalScrollY %d", currentPassItem, oldScrollState, scrollState, totalScrollY));
74 | }
75 |
76 | @Override
77 | public void onItemScrolling(LoopView loopView, int currentPassItem, int scrollState, int totalScrollY) {
78 | Log.i("gy", String.format("onItemScrolling currentPassItem %d scrollState %d totalScrollY %d", currentPassItem, scrollState, totalScrollY));
79 | }
80 | });
81 | //设置原始数据
82 | loopView.setItems(list);
83 | //设置初始位置
84 | loopView.setInitPosition(4);
85 | }
86 |
87 | @Override
88 | protected void onResume() {
89 | super.onResume();
90 | }
91 |
92 | }
93 |
--------------------------------------------------------------------------------
/app/src/main/java/com/weidongjian/meitu/wheelviewdemo/ScrollViewActivity.java:
--------------------------------------------------------------------------------
1 | package com.weidongjian.meitu.wheelviewdemo;
2 |
3 | import java.util.ArrayList;
4 |
5 | import android.os.Bundle;
6 | import android.support.annotation.Nullable;
7 | import android.support.v7.app.AppCompatActivity;
8 | import android.view.View;
9 | import android.widget.Toast;
10 |
11 | import com.weigan.loopview.LoopView;
12 | import com.weigan.loopview.OnItemSelectedListener;
13 |
14 | /**
15 | * Created by Weidongjian on 2017/3/25.
16 | */
17 |
18 | public class ScrollViewActivity extends AppCompatActivity {
19 |
20 | private Toast toast;
21 |
22 | @Override
23 | protected void onCreate(@Nullable Bundle savedInstanceState) {
24 | super.onCreate(savedInstanceState);
25 | setContentView(R.layout.activity_scroll_view);
26 |
27 | final LoopView loopView = (LoopView) findViewById(R.id.loopView);
28 |
29 | ArrayList list = new ArrayList<>();
30 | for (int i = 0; i < 15; i++) {
31 | list.add("item " + i);
32 | }
33 | // 滚动监听
34 | loopView.setListener(new OnItemSelectedListener() {
35 | @Override
36 | public void onItemSelected(int index) {
37 | if (toast == null) {
38 | toast = Toast.makeText(ScrollViewActivity.this, "item " + index, Toast.LENGTH_SHORT);
39 | }
40 | toast.setText("item " + index);
41 | toast.show();
42 | }
43 | });
44 | // 设置原始数据
45 | loopView.setItems(list);
46 |
47 | findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
48 | @Override
49 | public void onClick(View v) {
50 | loopView.setCurrentPosition(0);
51 | }
52 | });
53 |
54 | }
55 | }
56 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_dialog.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
9 |
10 |
17 |
18 |
25 |
26 |
31 |
32 |
33 |
40 |
41 |
50 |
51 |
52 |
58 |
59 |
65 |
66 |
67 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_scroll_view.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
11 |
12 |
17 |
18 |
24 |
25 |
32 |
33 |
39 |
40 |
46 |
47 |
48 |
54 |
55 |
56 |
62 |
63 |
64 |
70 |
71 |
72 |
78 |
79 |
80 |
81 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/dialog_view.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
13 |
14 |
21 |
22 |
25 |
26 |
--------------------------------------------------------------------------------
/app/src/main/res/menu/menu_main.xml:
--------------------------------------------------------------------------------
1 |
10 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/weidongjian/androidWheelView/47f07204434598fbb1a877a6ca3409b11f7cb3e4/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/weidongjian/androidWheelView/47f07204434598fbb1a877a6ca3409b11f7cb3e4/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/weidongjian/androidWheelView/47f07204434598fbb1a877a6ca3409b11f7cb3e4/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/weidongjian/androidWheelView/47f07204434598fbb1a877a6ca3409b11f7cb3e4/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/values-w820dp/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 64dp
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 16dp
4 | 16dp
5 |
6 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | WheelViewDemo
3 |
4 | Hello world!
5 | Settings
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
7 |
8 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/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 | mavenCentral()
6 | google()
7 | }
8 | dependencies {
9 | classpath 'com.android.tools.build:gradle:4.2.1'
10 |
11 | // NOTE: Do not place your application dependencies here; they belong
12 | // in the individual module build.gradle files
13 | }
14 | }
15 |
16 | allprojects {
17 | repositories {
18 | mavenCentral()
19 | maven { url 'https://jitpack.io' }
20 | google()
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/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 | # Default value: -Xmx10248m -XX:MaxPermSize=256m
13 | # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
14 |
15 | # When configured, Gradle will run in incubating parallel mode.
16 | # This option should only be used with decoupled projects. More details, visit
17 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
18 | # org.gradle.parallel=true
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/weidongjian/androidWheelView/47f07204434598fbb1a877a6ca3409b11f7cb3e4/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Mon Jun 24 12:36:23 CST 2019
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-6.7.1-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 | # For Cygwin, ensure paths are in UNIX format before anything is touched.
46 | if $cygwin ; then
47 | [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"`
48 | fi
49 |
50 | # Attempt to set APP_HOME
51 | # Resolve links: $0 may be a link
52 | PRG="$0"
53 | # Need this for relative symlinks.
54 | while [ -h "$PRG" ] ; do
55 | ls=`ls -ld "$PRG"`
56 | link=`expr "$ls" : '.*-> \(.*\)$'`
57 | if expr "$link" : '/.*' > /dev/null; then
58 | PRG="$link"
59 | else
60 | PRG=`dirname "$PRG"`"/$link"
61 | fi
62 | done
63 | SAVED="`pwd`"
64 | cd "`dirname \"$PRG\"`/" >&-
65 | APP_HOME="`pwd -P`"
66 | cd "$SAVED" >&-
67 |
68 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
69 |
70 | # Determine the Java command to use to start the JVM.
71 | if [ -n "$JAVA_HOME" ] ; then
72 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
73 | # IBM's JDK on AIX uses strange locations for the executables
74 | JAVACMD="$JAVA_HOME/jre/sh/java"
75 | else
76 | JAVACMD="$JAVA_HOME/bin/java"
77 | fi
78 | if [ ! -x "$JAVACMD" ] ; then
79 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
80 |
81 | Please set the JAVA_HOME variable in your environment to match the
82 | location of your Java installation."
83 | fi
84 | else
85 | JAVACMD="java"
86 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
87 |
88 | Please set the JAVA_HOME variable in your environment to match the
89 | location of your Java installation."
90 | fi
91 |
92 | # Increase the maximum file descriptors if we can.
93 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
94 | MAX_FD_LIMIT=`ulimit -H -n`
95 | if [ $? -eq 0 ] ; then
96 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
97 | MAX_FD="$MAX_FD_LIMIT"
98 | fi
99 | ulimit -n $MAX_FD
100 | if [ $? -ne 0 ] ; then
101 | warn "Could not set maximum file descriptor limit: $MAX_FD"
102 | fi
103 | else
104 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
105 | fi
106 | fi
107 |
108 | # For Darwin, add options to specify how the application appears in the dock
109 | if $darwin; then
110 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
111 | fi
112 |
113 | # For Cygwin, switch paths to Windows format before running java
114 | if $cygwin ; then
115 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
116 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
117 |
118 | # We build the pattern for arguments to be converted via cygpath
119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
120 | SEP=""
121 | for dir in $ROOTDIRSRAW ; do
122 | ROOTDIRS="$ROOTDIRS$SEP$dir"
123 | SEP="|"
124 | done
125 | OURCYGPATTERN="(^($ROOTDIRS))"
126 | # Add a user-defined pattern to the cygpath arguments
127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
129 | fi
130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
131 | i=0
132 | for arg in "$@" ; do
133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
135 |
136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
138 | else
139 | eval `echo args$i`="\"$arg\""
140 | fi
141 | i=$((i+1))
142 | done
143 | case $i in
144 | (0) set -- ;;
145 | (1) set -- "$args0" ;;
146 | (2) set -- "$args0" "$args1" ;;
147 | (3) set -- "$args0" "$args1" "$args2" ;;
148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
154 | esac
155 | fi
156 |
157 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
158 | function splitJvmOpts() {
159 | JVM_OPTS=("$@")
160 | }
161 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
162 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
163 |
164 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
165 |
--------------------------------------------------------------------------------
/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 |
--------------------------------------------------------------------------------
/loopview/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/loopview/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 |
3 | android {
4 | compileSdkVersion 29
5 | buildToolsVersion "29.0.3"
6 |
7 | defaultConfig {
8 | minSdkVersion 21
9 | targetSdkVersion 29
10 | versionCode 3
11 | versionName "1.0.0"
12 | }
13 | buildTypes {
14 | debug {
15 | minifyEnabled false
16 | }
17 | }
18 | }
19 |
20 | dependencies {
21 | api fileTree(dir: 'libs', include: ['*.jar'])
22 | }
23 |
24 |
25 |
--------------------------------------------------------------------------------
/loopview/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 D:\Android\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 |
--------------------------------------------------------------------------------
/loopview/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
3 |
4 |
6 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/loopview/src/main/java/com/weigan/loopview/InertiaTimerTask.java:
--------------------------------------------------------------------------------
1 | // Decompiled by Jad v1.5.8e. Copyright 2001 Pavel Kouznetsov.
2 | // Jad home page: http://www.geocities.com/kpdus/jad.html
3 | // Decompiler options: braces fieldsfirst space lnc
4 |
5 | package com.weigan.loopview;
6 |
7 | // Referenced classes of package com.qingchifan.view:
8 | // LoopView
9 |
10 | import android.util.Log;
11 |
12 | final class InertiaTimerTask implements Runnable {
13 |
14 | float a;
15 | final float velocityY;
16 | final LoopView loopView;
17 |
18 | InertiaTimerTask(LoopView loopview, float velocityY) {
19 | super();
20 | loopView = loopview;
21 | this.velocityY = velocityY;
22 | a = Integer.MAX_VALUE;
23 | }
24 |
25 | @Override
26 | public final void run() {
27 | if (a == Integer.MAX_VALUE) {
28 | if (Math.abs(velocityY) > 2000F) {
29 | if (velocityY > 0.0F) {
30 | a = 2000F;
31 | } else {
32 | a = -2000F;
33 | }
34 | } else {
35 | a = velocityY;
36 | }
37 | }
38 | if (Math.abs(a) >= 0.0F && Math.abs(a) <= 20F) {
39 | Log.i("gy","WHAT_SMOOTH_SCROLL_INERTIA");
40 | loopView.handler.sendEmptyMessageDelayed(MessageHandler.WHAT_SMOOTH_SCROLL_INERTIA,60);
41 | loopView.cancelFuture();
42 | loopView.handler.sendEmptyMessage(MessageHandler.WHAT_SMOOTH_SCROLL);
43 | return;
44 | }
45 | int i = (int) ((a * 10F) / 1000F);
46 | LoopView loopview = loopView;
47 | loopview.totalScrollY = loopview.totalScrollY - i;
48 | if (!loopView.isLoop) {
49 | float itemHeight = loopView.lineSpacingMultiplier * loopView.itemTextHeight;
50 | if (loopView.totalScrollY <= (int) ((float) (-loopView.initPosition) * itemHeight)) {
51 | a = 40F;
52 | loopView.totalScrollY = (int) ((float) (-loopView.initPosition) * itemHeight);
53 | } else if (loopView.totalScrollY >= (int) ((float) (loopView.items.size() - 1 - loopView.initPosition) * itemHeight)) {
54 | loopView.totalScrollY = (int) ((float) (loopView.items.size() - 1 - loopView.initPosition) * itemHeight);
55 | a = -40F;
56 | }
57 | }
58 | if (a < 0.0F) {
59 | a = a + 20F;
60 | } else {
61 | a = a - 20F;
62 | }
63 | loopView.handler.sendEmptyMessage(MessageHandler.WHAT_INVALIDATE_LOOP_VIEW);
64 | }
65 | }
66 |
--------------------------------------------------------------------------------
/loopview/src/main/java/com/weigan/loopview/LoopView.java:
--------------------------------------------------------------------------------
1 | package com.weigan.loopview;
2 |
3 | import android.content.Context;
4 | import android.content.res.Resources;
5 | import android.content.res.TypedArray;
6 | import android.graphics.Canvas;
7 | import android.graphics.Paint;
8 | import android.graphics.Rect;
9 | import android.graphics.Typeface;
10 | import android.os.Handler;
11 | import android.util.AttributeSet;
12 | import android.util.Log;
13 | import android.view.GestureDetector;
14 | import android.view.MotionEvent;
15 | import android.view.View;
16 |
17 | import java.util.ArrayList;
18 | import java.util.HashMap;
19 | import java.util.List;
20 | import java.util.concurrent.Executors;
21 | import java.util.concurrent.ScheduledExecutorService;
22 | import java.util.concurrent.ScheduledFuture;
23 | import java.util.concurrent.TimeUnit;
24 |
25 | /**
26 | * Created by Weidongjian on 2015/8/18.
27 | */
28 | public class LoopView extends View {
29 |
30 | private float scaleX = 1.05F;
31 |
32 | private static final int DEFAULT_TEXT_SIZE = (int) (Resources.getSystem().getDisplayMetrics().density * 15);
33 |
34 | private static final float DEFAULT_LINE_SPACE = 1f;
35 |
36 | private static final int DEFAULT_VISIBLE_ITEMS = 9;
37 |
38 | private static boolean ENABLE_CURVE = true;
39 |
40 | public static final int SCROLL_STATE_IDLE = 0; // 停止滚动
41 | public static final int SCROLL_STATE_SETTING = 1; // 用户设置
42 | public static final int SCROLL_STATE_DRAGGING = 2; // 用户按住滚轮拖拽
43 | public static final int SCROLL_STATE_SCROLLING = 3; // 依靠惯性滚动
44 |
45 | int lastScrollState = SCROLL_STATE_IDLE;
46 | int currentScrollState = SCROLL_STATE_SETTING;
47 |
48 | public enum ACTION {
49 | CLICK, FLING, DRAG
50 | }
51 |
52 | private Context context;
53 |
54 | Handler handler;
55 | private GestureDetector flingGestureDetector;
56 | OnItemSelectedListener onItemSelectedListener;
57 | OnItemScrollListener mOnItemScrollListener;
58 |
59 | // Timer mTimer;
60 | ScheduledExecutorService mExecutor = Executors.newSingleThreadScheduledExecutor();
61 | private ScheduledFuture> mFuture;
62 |
63 | private Paint paintOuterText;
64 | private Paint paintCenterText;
65 | private Paint paintIndicator;
66 |
67 | List items;
68 |
69 | int textSize;
70 | int itemTextHeight; //单个item的高度
71 |
72 | //文本的高度
73 | int textHeight;
74 |
75 | int outerTextColor;
76 |
77 | int centerTextColor;
78 | int dividerColor;
79 |
80 | float lineSpacingMultiplier;
81 | boolean isLoop;
82 |
83 | int firstLineY;
84 | int secondLineY;
85 |
86 | int totalScrollY;
87 | int initPosition;
88 | int preCurrentIndex;
89 | int change;
90 |
91 | int itemsVisibleCount;
92 |
93 | HashMap drawingStrings;
94 | // HashMap drawingStr
95 |
96 | int measuredHeight; //布局定义的高度
97 | int measuredWidth;
98 |
99 | int halfCircumference;//半圆的周长
100 | int radius; //半径
101 |
102 | private int mOffset = 0;
103 | private float previousY;
104 | long startTime = 0;
105 |
106 | private Rect tempRect = new Rect();
107 |
108 | private int paddingLeft, paddingRight;
109 |
110 | private Typeface typeface = Typeface.MONOSPACE;
111 |
112 | private boolean isEnableCurve = ENABLE_CURVE;
113 |
114 | /**
115 | * set text line space, must more than 1
116 | *
117 | * @param lineSpacingMultiplier
118 | */
119 | public void setLineSpacingMultiplier(float lineSpacingMultiplier) {
120 | if (lineSpacingMultiplier > 1.0f) {
121 | this.lineSpacingMultiplier = lineSpacingMultiplier;
122 | }
123 | }
124 |
125 | /**
126 | * set outer text color
127 | * @param centerTextColor
128 | */
129 | public void setCenterTextColor(int centerTextColor) {
130 | this.centerTextColor = centerTextColor;
131 | if(paintCenterText != null){
132 | paintCenterText.setColor(centerTextColor);
133 | }
134 |
135 | }
136 |
137 | /**
138 | * set center text color
139 | * @param outerTextColor
140 | */
141 | public void setOuterTextColor(int outerTextColor) {
142 | this.outerTextColor = outerTextColor;
143 | if(paintOuterText != null){
144 | paintOuterText.setColor(outerTextColor);
145 | }
146 | }
147 |
148 | /**
149 | * set divider color
150 | * @param dividerColor
151 | */
152 | public void setDividerColor(int dividerColor) {
153 | this.dividerColor = dividerColor;
154 | if(paintIndicator != null){
155 | paintIndicator.setColor(dividerColor);
156 | }
157 | }
158 |
159 | /**
160 | * set text typeface
161 | * @param typeface
162 | */
163 | public void setTypeface(Typeface typeface) {
164 | this.typeface = typeface;
165 | }
166 |
167 | public LoopView(Context context) {
168 | super(context);
169 | initLoopView(context, null);
170 | }
171 |
172 | public LoopView(Context context, AttributeSet attributeset) {
173 | super(context, attributeset);
174 | initLoopView(context, attributeset);
175 | }
176 |
177 | public LoopView(Context context, AttributeSet attributeset, int defStyleAttr) {
178 | super(context, attributeset, defStyleAttr);
179 | initLoopView(context, attributeset);
180 | }
181 |
182 | private void initLoopView(Context context, AttributeSet attributeset) {
183 | this.context = context;
184 | handler = new MessageHandler(this);
185 | flingGestureDetector = new GestureDetector(context, new LoopViewGestureListener(this));
186 | flingGestureDetector.setIsLongpressEnabled(false);
187 |
188 | TypedArray typedArray = context.obtainStyledAttributes(attributeset, R.styleable.LoopView);
189 | if (typedArray != null) {
190 | textSize = typedArray.getInteger(R.styleable.LoopView_awv_textsize, DEFAULT_TEXT_SIZE);
191 | textSize = (int) (Resources.getSystem().getDisplayMetrics().density * textSize);
192 | lineSpacingMultiplier = typedArray.getFloat(R.styleable.LoopView_awv_lineSpace, DEFAULT_LINE_SPACE);
193 | centerTextColor = typedArray.getInteger(R.styleable.LoopView_awv_centerTextColor, 0xff313131);
194 | outerTextColor = typedArray.getInteger(R.styleable.LoopView_awv_outerTextColor, 0xffafafaf);
195 | dividerColor = typedArray.getInteger(R.styleable.LoopView_awv_dividerTextColor, 0xffc5c5c5);
196 | itemsVisibleCount =
197 | typedArray.getInteger(R.styleable.LoopView_awv_itemsVisibleCount, DEFAULT_VISIBLE_ITEMS);
198 | if (itemsVisibleCount % 2 == 0) {
199 | itemsVisibleCount = DEFAULT_VISIBLE_ITEMS;
200 | }
201 | isLoop = typedArray.getBoolean(R.styleable.LoopView_awv_isLoop, true);
202 | isEnableCurve = typedArray.getBoolean(R.styleable.LoopView_awv_isCurve, ENABLE_CURVE);
203 | typedArray.recycle();
204 | }
205 |
206 | drawingStrings = new HashMap<>();
207 | totalScrollY = 0;
208 | initPosition = -1;
209 |
210 | //初始化画笔
211 | initPaintsIfPossible();
212 | }
213 |
214 |
215 | /**
216 | * visible item count, must be odd number
217 | *
218 | * @param visibleNumber
219 | */
220 | public void setItemsVisibleCount(int visibleNumber) {
221 | if (visibleNumber % 2 == 0) {
222 | return;
223 | }
224 | if (visibleNumber != itemsVisibleCount) {
225 | itemsVisibleCount = visibleNumber;
226 | drawingStrings=new HashMap<>();
227 | }
228 | }
229 |
230 | private void initPaintsIfPossible() {
231 | if (paintOuterText == null) {
232 | paintOuterText = new Paint();
233 | paintOuterText.setColor(outerTextColor);
234 | paintOuterText.setAntiAlias(true);
235 | paintOuterText.setTypeface(typeface);
236 | paintOuterText.setTextSize(textSize);
237 | }
238 |
239 |
240 | if (paintCenterText == null) {
241 | paintCenterText = new Paint();
242 | paintCenterText.setColor(centerTextColor);
243 | paintCenterText.setAntiAlias(true);
244 | paintCenterText.setTextScaleX(scaleX);
245 | paintCenterText.setTypeface(typeface);
246 | paintCenterText.setTextSize(textSize);
247 | }
248 |
249 | if (paintIndicator == null) {
250 | paintIndicator = new Paint();
251 | paintIndicator.setColor(dividerColor);
252 | paintIndicator.setAntiAlias(true);
253 | }
254 | }
255 |
256 | private void remeasure() {
257 | if (items == null || items.isEmpty()) {
258 | return;
259 | }
260 |
261 | measuredWidth = getMeasuredWidth();
262 |
263 | measuredHeight = getMeasuredHeight();
264 |
265 | if (measuredWidth == 0 || measuredHeight == 0) {
266 | return;
267 | }
268 |
269 | paddingLeft = getPaddingLeft();
270 | paddingRight = getPaddingRight();
271 |
272 | measuredWidth = measuredWidth - paddingRight;
273 |
274 | paintCenterText.getTextBounds("\u661F\u671F", 0, 2, tempRect); // 星期
275 | textHeight = tempRect.height();
276 | halfCircumference = (int) (measuredHeight * Math.PI / 2);
277 |
278 | if (isEnableCurve) {
279 | itemTextHeight = (int) (halfCircumference / (lineSpacingMultiplier * (itemsVisibleCount - 1)));
280 | } else {
281 | itemTextHeight = measuredHeight / itemsVisibleCount;
282 | }
283 |
284 |
285 | radius = measuredHeight / 2;
286 | firstLineY = (int) ((measuredHeight - lineSpacingMultiplier * itemTextHeight) / 2.0F);
287 | secondLineY = (int) ((measuredHeight + lineSpacingMultiplier * itemTextHeight) / 2.0F);
288 | if (initPosition == -1) {
289 | if (isLoop) {
290 | initPosition = (items.size() + 1) / 2;
291 | } else {
292 | initPosition = 0;
293 | }
294 | }
295 |
296 | preCurrentIndex = initPosition;
297 | }
298 |
299 | void smoothScroll(ACTION action) {
300 | cancelFuture();
301 | if (action == ACTION.FLING || action == ACTION.DRAG) {
302 | float itemHeight = lineSpacingMultiplier * itemTextHeight;
303 | mOffset = (int) ((totalScrollY % itemHeight + itemHeight) % itemHeight);
304 | if ((float) mOffset > itemHeight / 2.0F) {
305 | mOffset = (int) (itemHeight - (float) mOffset);
306 | } else {
307 | mOffset = -mOffset;
308 | }
309 | }
310 | mFuture =
311 | mExecutor.scheduleWithFixedDelay(new SmoothScrollTimerTask(this, mOffset), 0, 10, TimeUnit.MILLISECONDS);
312 | changeScrollState(SCROLL_STATE_SCROLLING);
313 | }
314 |
315 | protected final void scrollBy(float velocityY) {
316 | cancelFuture();
317 | // change this number, can change fling speed
318 | int velocityFling = 10;
319 | mFuture = mExecutor.scheduleWithFixedDelay(new InertiaTimerTask(this, velocityY), 0, velocityFling,
320 | TimeUnit.MILLISECONDS);
321 | changeScrollState(SCROLL_STATE_DRAGGING);
322 | }
323 |
324 | public void cancelFuture() {
325 | if (mFuture != null && !mFuture.isCancelled()) {
326 | mFuture.cancel(true);
327 | mFuture = null;
328 | changeScrollState(SCROLL_STATE_IDLE);
329 | }
330 | }
331 |
332 | /**
333 | * 打印方法调用堆栈链信息 用于调试
334 | * @param methodName
335 | */
336 | private void printMethodStackTrace(String methodName){
337 | StackTraceElement[] invokers = Thread.currentThread().getStackTrace();
338 | StringBuilder sb = new StringBuilder("printMethodStackTrace ");
339 | sb.append(methodName);
340 | sb.append(" ");
341 | for(int i= invokers.length -1;i >= 4;i--){
342 | StackTraceElement invoker = invokers[i];
343 | sb.append(String.format("%s(%d).%s",invoker.getFileName(),invoker.getLineNumber(),invoker.getMethodName()));
344 | if(i > 4){
345 | sb.append("-->");
346 | }
347 | }
348 | Log.i("printMethodStackTrace",sb.toString());
349 | }
350 |
351 | private void changeScrollState(int scrollState){
352 | if(scrollState != currentScrollState && !handler.hasMessages(MessageHandler.WHAT_SMOOTH_SCROLL_INERTIA)){
353 | lastScrollState = currentScrollState;
354 | currentScrollState = scrollState;
355 | // if(scrollState == SCROLL_STATE_SCROLLING || scrollState == SCROLL_STATE_IDLE){
356 | // printMethodStackTrace("changeScrollState");
357 | // }
358 | }
359 | }
360 |
361 | /**
362 | * set not loop
363 | */
364 | public void setNotLoop() {
365 | isLoop = false;
366 | }
367 |
368 | /**
369 | * set text size in dp
370 | * @param size
371 | */
372 | public final void setTextSize(float size) {
373 | if (size > 0.0F) {
374 | textSize = (int) (context.getResources().getDisplayMetrics().density * size);
375 | if(paintOuterText != null){
376 | paintOuterText.setTextSize(textSize);
377 | }
378 | if(paintCenterText != null){
379 | paintCenterText.setTextSize(textSize);
380 | }
381 |
382 | }
383 | }
384 |
385 | public final void setInitPosition(int initPosition) {
386 | if (initPosition < 0) {
387 | this.initPosition = 0;
388 | } else {
389 | if (items != null && items.size() > initPosition) {
390 | this.initPosition = initPosition;
391 | }
392 | }
393 | }
394 |
395 | public final void setListener(OnItemSelectedListener OnItemSelectedListener) {
396 | onItemSelectedListener = OnItemSelectedListener;
397 | }
398 |
399 | public final void setOnItemScrollListener(OnItemScrollListener mOnItemScrollListener){
400 | this.mOnItemScrollListener = mOnItemScrollListener;
401 | }
402 |
403 |
404 |
405 | public final void setItems(List items) {
406 |
407 | this.items = convertData(items);
408 | remeasure();
409 | invalidate();
410 | }
411 |
412 | public List convertData(List items){
413 | List data=new ArrayList<>();
414 | for (int i = 0; i < items.size(); i++) {
415 | data.add(new IndexString(i,items.get(i)));
416 | }
417 | return data;
418 | }
419 |
420 | public final int getSelectedItem() {
421 | return preCurrentIndex;
422 | }
423 | //
424 | // protected final void scrollBy(float velocityY) {
425 | // Timer timer = new Timer();
426 | // mTimer = timer;
427 | // timer.schedule(new InertiaTimerTask(this, velocityY, timer), 0L, 20L);
428 | // }
429 |
430 | protected final void onItemSelected() {
431 | if (onItemSelectedListener != null) {
432 | postDelayed(new OnItemSelectedRunnable(this), 200L);
433 | }
434 | }
435 |
436 | /**
437 | * link https://github.com/weidongjian/androidWheelView/issues/10
438 | *
439 | * @param scaleX
440 | */
441 | public void setScaleX(float scaleX) {
442 | this.scaleX = scaleX;
443 | }
444 |
445 | /**
446 | * set current item position
447 | * @param position
448 | */
449 | public void setCurrentPosition(int position) {
450 | if (items == null || items.isEmpty()) {
451 | return;
452 | }
453 | int size = items.size();
454 | if (position >= 0 && position < size && position != getSelectedItem()) {
455 | initPosition = position;
456 | totalScrollY = 0;
457 | mOffset = 0;
458 | changeScrollState(SCROLL_STATE_SETTING);
459 | remeasure();
460 | handler.sendEmptyMessage(MessageHandler.WHAT_ITEM_SELECTED);
461 | invalidate();
462 | }
463 | }
464 |
465 | @Override
466 | protected void onDraw(Canvas canvas) {
467 | super.onDraw(canvas);
468 | if (items == null || items.isEmpty()) {
469 | return;
470 | }
471 |
472 | change = (int) (totalScrollY / (lineSpacingMultiplier * itemTextHeight));
473 | preCurrentIndex = initPosition + change % items.size();
474 |
475 | if (!isLoop) {
476 | if (preCurrentIndex < 0) {
477 | preCurrentIndex = 0;
478 | }
479 | if (preCurrentIndex > items.size() - 1) {
480 | preCurrentIndex = items.size() - 1;
481 | }
482 | } else {
483 | if (preCurrentIndex < 0) {
484 | preCurrentIndex = items.size() + preCurrentIndex;
485 | }
486 | if (preCurrentIndex > items.size() - 1) {
487 | preCurrentIndex = preCurrentIndex - items.size();
488 | }
489 | }
490 |
491 | //偏移量
492 | int j2 = totalScrollY % itemTextHeight;
493 | // put value to drawingString
494 | int k1 = 0;
495 | while (k1 < itemsVisibleCount) {
496 | int l1 = preCurrentIndex - (itemsVisibleCount / 2 - k1);
497 | if (isLoop) {
498 | while (l1 < 0) {
499 | l1 = l1 + items.size();
500 | }
501 | while (l1 > items.size() - 1) {
502 | l1 = l1 - items.size();
503 | }
504 | drawingStrings.put(k1, items.get(l1));
505 | } else if (l1 < 0) {
506 | // drawingStrings[k1] = "";
507 | drawingStrings.put(k1,new IndexString());
508 | } else if (l1 > items.size() - 1) {
509 | // drawingStrings[k1] = "";
510 | drawingStrings.put(k1,new IndexString());
511 | } else {
512 | // drawingStrings[k1] = items.get(l1);
513 | drawingStrings.put(k1,items.get(l1));
514 | }
515 | k1++;
516 | }
517 | canvas.drawLine(paddingLeft, firstLineY, measuredWidth, firstLineY, paintIndicator);
518 | canvas.drawLine(paddingLeft, secondLineY, measuredWidth, secondLineY, paintIndicator);
519 |
520 | int i = 0;
521 | while (i < itemsVisibleCount) {
522 | canvas.save();
523 | float itemHeight = itemTextHeight * lineSpacingMultiplier;
524 | double radian = ((itemHeight * i - j2) * Math.PI) / halfCircumference;
525 | if ((radian >= Math.PI || radian <= 0) && isEnableCurve) {
526 | canvas.restore();
527 | } else {
528 | int translateY;
529 | if (isEnableCurve) {
530 | translateY = (int) (radius - Math.cos(radian) * radius - (Math.sin(radian) * itemTextHeight) / 2D);
531 | } else {
532 | translateY = (int) (itemHeight * i - j2);
533 | Log.d("weigan", "translateY " + translateY + " pos " + i + " j2 " + j2);
534 | }
535 | canvas.translate(0.0F, translateY);
536 | if (isEnableCurve) {
537 | canvas.scale(1.0F, (float) Math.sin(radian));
538 | }
539 | if (translateY <= firstLineY && itemTextHeight + translateY >= firstLineY) {
540 | // first divider
541 | canvas.save();
542 | canvas.clipRect(0, 0, measuredWidth, firstLineY - translateY);
543 | drawOuterText(canvas, i);
544 | canvas.restore();
545 | canvas.save();
546 | canvas.clipRect(0, firstLineY - translateY, measuredWidth, (int) (itemHeight));
547 | drawCenterText(canvas, i);
548 | canvas.restore();
549 | } else if (translateY <= secondLineY && itemTextHeight + translateY >= secondLineY) {
550 | // second divider
551 | canvas.save();
552 | canvas.clipRect(0, 0, measuredWidth, secondLineY - translateY);
553 | drawCenterText(canvas, i);
554 | canvas.restore();
555 | canvas.save();
556 | canvas.clipRect(0, secondLineY - translateY, measuredWidth, (int) (itemHeight));
557 | drawOuterText(canvas, i);
558 | canvas.restore();
559 | } else if (translateY >= firstLineY && itemTextHeight + translateY <= secondLineY) {
560 | // center item
561 | canvas.clipRect(0, 0, measuredWidth, (int) (itemHeight));
562 | drawCenterText(canvas, i);
563 | } else {
564 | // other item
565 | canvas.clipRect(0, 0, measuredWidth, (int) (itemHeight));
566 | drawOuterText(canvas, i);
567 | }
568 | canvas.restore();
569 | }
570 | i++;
571 | }
572 |
573 | if(currentScrollState != lastScrollState){
574 | int oldScrollState = lastScrollState;
575 | lastScrollState = currentScrollState;
576 | if(mOnItemScrollListener != null){
577 | mOnItemScrollListener.onItemScrollStateChanged(this,getSelectedItem(),oldScrollState,currentScrollState,totalScrollY);
578 | }
579 |
580 | }
581 | if(currentScrollState == SCROLL_STATE_DRAGGING || currentScrollState == SCROLL_STATE_SCROLLING){
582 | if(mOnItemScrollListener != null){
583 | mOnItemScrollListener.onItemScrolling(this,getSelectedItem(),currentScrollState,totalScrollY);
584 | }
585 | }
586 | }
587 |
588 |
589 | private void drawOuterText(Canvas canvas, int position) {
590 | canvas.drawText(drawingStrings.get(position).string, getTextX(drawingStrings.get(position).string, paintOuterText, tempRect),
591 | getDrawingY(), paintOuterText);
592 | }
593 |
594 | private void drawCenterText(Canvas canvas, int position) {
595 | canvas.drawText(drawingStrings.get(position).string, getTextX(drawingStrings.get(position).string, paintOuterText, tempRect),
596 | getDrawingY(), paintCenterText);
597 | }
598 |
599 |
600 | private int getDrawingY() {
601 | if (itemTextHeight > textHeight) {
602 | return itemTextHeight - ((itemTextHeight - textHeight) / 2);
603 | } else {
604 | return itemTextHeight;
605 | }
606 | }
607 |
608 |
609 | // text start drawing position
610 | private int getTextX(String a, Paint paint, Rect rect) {
611 | paint.getTextBounds(a, 0, a.length(), rect);
612 | int textWidth = rect.width();
613 | textWidth *= scaleX;
614 | return (measuredWidth - paddingLeft - textWidth) / 2 + paddingLeft;
615 | }
616 |
617 | @Override
618 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
619 | super.onMeasure(widthMeasureSpec, heightMeasureSpec);
620 | remeasure();
621 | }
622 |
623 |
624 | @Override
625 | public boolean onTouchEvent(MotionEvent event) {
626 | boolean eventConsumed = flingGestureDetector.onTouchEvent(event);
627 | float itemHeight = lineSpacingMultiplier * itemTextHeight;
628 |
629 | switch (event.getAction()) {
630 | case MotionEvent.ACTION_DOWN:
631 | startTime = System.currentTimeMillis();
632 | cancelFuture();
633 | previousY = event.getRawY();
634 | if (getParent() != null) {
635 | getParent().requestDisallowInterceptTouchEvent(true);
636 | }
637 | break;
638 |
639 | case MotionEvent.ACTION_MOVE:
640 | float dy = previousY - event.getRawY();
641 | previousY = event.getRawY();
642 |
643 | totalScrollY = (int) (totalScrollY + dy);
644 |
645 | if (!isLoop) {
646 | float top = -initPosition * itemHeight;
647 | float bottom = (items.size() - 1 - initPosition) * itemHeight;
648 |
649 | if (totalScrollY < top) {
650 | totalScrollY = (int) top;
651 | } else if (totalScrollY > bottom) {
652 | totalScrollY = (int) bottom;
653 | }
654 | }
655 | changeScrollState(SCROLL_STATE_DRAGGING);
656 | break;
657 |
658 | case MotionEvent.ACTION_UP:
659 | case MotionEvent.ACTION_CANCEL:
660 | default:
661 | if (!eventConsumed) {
662 | float y = event.getY();
663 | double l = Math.acos((radius - y) / radius) * radius;
664 | int circlePosition = (int) ((l + itemHeight / 2) / itemHeight);
665 |
666 | float extraOffset = (totalScrollY % itemHeight + itemHeight) % itemHeight;
667 | mOffset = (int) ((circlePosition - itemsVisibleCount / 2) * itemHeight - extraOffset);
668 |
669 | if ((System.currentTimeMillis() - startTime) > 120) {
670 | smoothScroll(ACTION.DRAG);
671 | } else {
672 | smoothScroll(ACTION.CLICK);
673 | }
674 | }
675 | if (getParent() != null) {
676 | getParent().requestDisallowInterceptTouchEvent(false);
677 | }
678 | break;
679 | }
680 |
681 | invalidate();
682 | return true;
683 | }
684 |
685 | class IndexString {
686 |
687 | public IndexString(){
688 | this.string="";
689 | }
690 |
691 | public IndexString(int index,String str){
692 | this.index=index;this.string=str;
693 | }
694 | private String string;
695 | private int index;
696 | }
697 | }
698 |
--------------------------------------------------------------------------------
/loopview/src/main/java/com/weigan/loopview/LoopViewGestureListener.java:
--------------------------------------------------------------------------------
1 | // Decompiled by Jad v1.5.8e. Copyright 2001 Pavel Kouznetsov.
2 | // Jad home page: http://www.geocities.com/kpdus/jad.html
3 | // Decompiler options: braces fieldsfirst space lnc
4 |
5 | package com.weigan.loopview;
6 |
7 | import android.view.MotionEvent;
8 |
9 | // Referenced classes of package com.qingchifan.view:
10 | // LoopView
11 |
12 | final class LoopViewGestureListener extends android.view.GestureDetector.SimpleOnGestureListener {
13 |
14 | final LoopView loopView;
15 |
16 | LoopViewGestureListener(LoopView loopview) {
17 | loopView = loopview;
18 | }
19 |
20 | @Override
21 | public final boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
22 | loopView.scrollBy(velocityY);
23 | return true;
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/loopview/src/main/java/com/weigan/loopview/MessageHandler.java:
--------------------------------------------------------------------------------
1 | // Decompiled by Jad v1.5.8e. Copyright 2001 Pavel Kouznetsov.
2 | // Jad home page: http://www.geocities.com/kpdus/jad.html
3 | // Decompiler options: braces fieldsfirst space lnc
4 |
5 | package com.weigan.loopview;
6 |
7 | import android.os.Handler;
8 | import android.os.Message;
9 |
10 | // Referenced classes of package com.qingchifan.view:
11 | // LoopView
12 |
13 | final class MessageHandler extends Handler {
14 | public static final int WHAT_INVALIDATE_LOOP_VIEW = 1000;
15 | public static final int WHAT_SMOOTH_SCROLL = 2000;
16 | public static final int WHAT_SMOOTH_SCROLL_INERTIA = 2001;
17 | public static final int WHAT_ITEM_SELECTED = 3000;
18 |
19 | final LoopView loopview;
20 |
21 | MessageHandler(LoopView loopview) {
22 | this.loopview = loopview;
23 | }
24 |
25 | @Override
26 | public final void handleMessage(Message msg) {
27 | switch (msg.what) {
28 | case WHAT_INVALIDATE_LOOP_VIEW:
29 | loopview.invalidate();
30 | break;
31 |
32 | case WHAT_SMOOTH_SCROLL:
33 | removeMessages(WHAT_SMOOTH_SCROLL_INERTIA);
34 | loopview.smoothScroll(LoopView.ACTION.FLING);
35 | break;
36 |
37 | case WHAT_ITEM_SELECTED:
38 | loopview.onItemSelected();
39 | break;
40 | }
41 | }
42 |
43 | }
44 |
--------------------------------------------------------------------------------
/loopview/src/main/java/com/weigan/loopview/OnItemScrollListener.java:
--------------------------------------------------------------------------------
1 | package com.weigan.loopview;
2 |
3 | public interface OnItemScrollListener {
4 | /**
5 | * 滚轮滚动状态变化监听
6 | * @param loopView
7 | * @param currentPassItem 当前经过的item
8 | * @param oldScrollState 上一次滚动状态
9 | * @param scrollState 当前滚动状态
10 | * @param totalScrollY 滚动距离
11 | */
12 | void onItemScrollStateChanged(LoopView loopView,int currentPassItem,int oldScrollState,int scrollState,int totalScrollY);
13 |
14 | /***
15 | * 滚轮滚动监听
16 | * @param loopView
17 | * @param currentPassItem 当前经过的item
18 | * @param scrollState 当前滚动状态
19 | * @param totalScrollY 滚动距离
20 | */
21 | void onItemScrolling(LoopView loopView,int currentPassItem,int scrollState,int totalScrollY);
22 | }
23 |
--------------------------------------------------------------------------------
/loopview/src/main/java/com/weigan/loopview/OnItemSelectedListener.java:
--------------------------------------------------------------------------------
1 | // Decompiled by Jad v1.5.8e. Copyright 2001 Pavel Kouznetsov.
2 | // Jad home page: http://www.geocities.com/kpdus/jad.html
3 | // Decompiler options: braces fieldsfirst space lnc
4 |
5 | package com.weigan.loopview;
6 |
7 |
8 | public interface OnItemSelectedListener {
9 | void onItemSelected(int index);
10 | }
11 |
--------------------------------------------------------------------------------
/loopview/src/main/java/com/weigan/loopview/OnItemSelectedRunnable.java:
--------------------------------------------------------------------------------
1 | // Decompiled by Jad v1.5.8e. Copyright 2001 Pavel Kouznetsov.
2 | // Jad home page: http://www.geocities.com/kpdus/jad.html
3 | // Decompiler options: braces fieldsfirst space lnc
4 |
5 | package com.weigan.loopview;
6 |
7 | // Referenced classes of package com.qingchifan.view:
8 | // LoopView, OnItemSelectedListener
9 |
10 | import android.util.Log;
11 |
12 | final class OnItemSelectedRunnable implements Runnable {
13 | final LoopView loopView;
14 |
15 | OnItemSelectedRunnable(LoopView loopview) {
16 | loopView = loopview;
17 | }
18 |
19 | @Override
20 | public final void run() {
21 | loopView.onItemSelectedListener.onItemSelected(loopView.getSelectedItem());
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/loopview/src/main/java/com/weigan/loopview/SmoothScrollTimerTask.java:
--------------------------------------------------------------------------------
1 | // Decompiled by Jad v1.5.8e. Copyright 2001 Pavel Kouznetsov.
2 | // Jad home page: http://www.geocities.com/kpdus/jad.html
3 | // Decompiler options: braces fieldsfirst space lnc
4 |
5 | package com.weigan.loopview;
6 |
7 | final class SmoothScrollTimerTask implements Runnable {
8 |
9 | int realTotalOffset;
10 | int realOffset;
11 | int offset;
12 | final LoopView loopView;
13 |
14 | SmoothScrollTimerTask(LoopView loopview, int offset) {
15 | this.loopView = loopview;
16 | this.offset = offset;
17 | realTotalOffset = Integer.MAX_VALUE;
18 | realOffset = 0;
19 | }
20 |
21 | @Override
22 | public final void run() {
23 | if (realTotalOffset == Integer.MAX_VALUE) {
24 | realTotalOffset = offset;
25 | }
26 | realOffset = (int) ((float) realTotalOffset * 0.1F);
27 |
28 | if (realOffset == 0) {
29 | if (realTotalOffset < 0) {
30 | realOffset = -1;
31 | } else {
32 | realOffset = 1;
33 | }
34 | }
35 | if (Math.abs(realTotalOffset) <= 0) {
36 | loopView.cancelFuture();
37 | loopView.handler.sendEmptyMessage(MessageHandler.WHAT_ITEM_SELECTED);
38 | } else {
39 | loopView.totalScrollY = loopView.totalScrollY + realOffset;
40 | loopView.handler.sendEmptyMessage(MessageHandler.WHAT_INVALIDATE_LOOP_VIEW);
41 | realTotalOffset = realTotalOffset - realOffset;
42 | }
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/loopview/src/main/res/values/attrs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/loopview/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/photo/1625735942774356.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/weidongjian/androidWheelView/47f07204434598fbb1a877a6ca3409b11f7cb3e4/photo/1625735942774356.gif
--------------------------------------------------------------------------------
/photo/Gif_20161230_162613.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/weidongjian/androidWheelView/47f07204434598fbb1a877a6ca3409b11f7cb3e4/photo/Gif_20161230_162613.gif
--------------------------------------------------------------------------------
/photo/circle.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/weidongjian/androidWheelView/47f07204434598fbb1a877a6ca3409b11f7cb3e4/photo/circle.jpg
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app' , ':loopview'
2 |
--------------------------------------------------------------------------------