├── .github
└── workflows
│ └── ci.yml
├── .gitignore
├── .idea
├── compiler.xml
├── copyright
│ └── profiles_settings.xml
├── encodings.xml
├── gradle.xml
├── inspectionProfiles
│ ├── Project_Default.xml
│ └── profiles_settings.xml
├── misc.xml
├── modules.xml
├── runConfigurations.xml
└── vcs.xml
├── LICENSE
├── README.md
├── app
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src
│ └── main
│ ├── AndroidManifest.xml
│ ├── java
│ └── cn
│ │ └── izouxiang
│ │ └── likeviewdemo
│ │ ├── MainActivity.java
│ │ ├── SSAdapter.java
│ │ └── SSEntity.java
│ └── res
│ ├── layout
│ ├── activity_main.xml
│ └── layout_item.xml
│ ├── mipmap-hdpi
│ └── ic_launcher.png
│ ├── mipmap-mdpi
│ └── ic_launcher.png
│ ├── mipmap-xhdpi
│ └── ic_launcher.png
│ ├── mipmap-xxhdpi
│ └── ic_launcher.png
│ ├── mipmap-xxxhdpi
│ └── ic_launcher.png
│ ├── values-w820dp
│ └── dimens.xml
│ └── values
│ ├── colors.xml
│ ├── dimens.xml
│ ├── strings.xml
│ └── styles.xml
├── build.gradle
├── gradle.properties
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── img
└── likeview.gif
├── likeview
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src
│ ├── androidTest
│ └── java
│ │ └── cn
│ │ └── izouxiang
│ │ └── likeview
│ │ └── ExampleInstrumentedTest.java
│ ├── main
│ ├── AndroidManifest.xml
│ ├── java
│ │ └── cn
│ │ │ └── izouxiang
│ │ │ └── likeview
│ │ │ ├── CommentPathAdapter.java
│ │ │ ├── LikeView.java
│ │ │ └── util
│ │ │ └── PathUtils.java
│ └── res
│ │ └── values
│ │ ├── attrs.xml
│ │ └── strings.xml
│ └── test
│ └── java
│ └── cn
│ └── izouxiang
│ └── likeview
│ └── ExampleUnitTest.java
└── settings.gradle
/.github/workflows/ci.yml:
--------------------------------------------------------------------------------
1 | name: ci
2 |
3 | on:
4 | push:
5 | branches:
6 | - master
7 | # schedule:
8 | # - cron: "0 2 * * *"
9 |
10 | jobs:
11 | autogreen:
12 | runs-on: ubuntu-latest
13 |
14 | permissions:
15 | contents: write
16 |
17 | steps:
18 | - name: Clone repository
19 | uses: actions/checkout@v3
20 |
21 | - name: Auto Commit
22 | run: |
23 | git config --local user.email "zouxiangq@qq.com"
24 | git config --local user.name "fxzou"
25 | git remote set-url origin https://${{ github.actor }}:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}
26 | git pull --rebase
27 | echo "// 添加时间备注 $(date -I)" >> likeview/src/main/java/cn/izouxiang/likeview/CommentPathAdapter.java
28 | git add likeview/src/main/java/cn/izouxiang/likeview/CommentPathAdapter.java
29 | git commit --allow-empty -m "随便提交点东西"
30 | git push
31 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /.idea/workspace.xml
5 | /.idea/libraries
6 | .DS_Store
7 | /build
8 | /captures
9 | .externalNativeBuild
10 |
--------------------------------------------------------------------------------
/.idea/compiler.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/.idea/copyright/profiles_settings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
--------------------------------------------------------------------------------
/.idea/encodings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/gradle.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
19 |
20 |
--------------------------------------------------------------------------------
/.idea/inspectionProfiles/Project_Default.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/.idea/inspectionProfiles/profiles_settings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 | 1.8
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
--------------------------------------------------------------------------------
/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/.idea/runConfigurations.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/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 [2023] [Fangxiang Zou]
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 | # 仿即刻APP点赞桃心的效果
2 | ## 2016-12-5更新
3 | - 修改测量逻辑
4 | - 添加了对齐方式
5 | - 添加了一个评论图形的GraphAdapter
6 | - 修改了已知BUG
7 |
8 | ## 先看效果图
9 | 
10 | ## 使用方法
11 | ### 布局配置
12 | ```
13 |
19 | ```
20 | > 注意,一般不需要直接指定宽高,内部会根据字体大小自动测量
21 |
22 | ### 代码配置
23 | ```java
24 | //点赞view的设置
25 | holder.like.setActivated(entity.isLike);
26 | holder.like.setNumber(entity.likeNum);
27 | holder.like.setCallback(new LikeView.SimpleCallback() {
28 | @Override
29 | public void activate(LikeView view) {
30 | Snackbar.make(view, "你觉得" + entity.name + "很赞!", Snackbar.LENGTH_SHORT).show();
31 | }
32 |
33 | @Override
34 | public void deactivate(LikeView view) {
35 | Snackbar.make(view, "你取消了对" + entity.name + "的赞!", Snackbar.LENGTH_SHORT).show();
36 | }
37 | });
38 | //评论view的设置
39 | holder.comment.setNumber(entity.commentNum);
40 | //设置图形适配器
41 | holder.comment.setGraphAdapter(CommentPathAdapter.getInstance());
42 | holder.comment.setCallback(new LikeView.SimpleCallback(){
43 | @Override
44 | public boolean onClick(LikeView view) {
45 | Snackbar.make(view, "你点击" + entity.name + "的评论按钮", Snackbar.LENGTH_SHORT).show();
46 | //返回true代表拦截此次点击,不使用默认的点击事件
47 | return true;
48 | }
49 | });
50 | ```
51 | ### 自定义图形适配器
52 | ```java
53 | public class CommentPathAdapter implements LikeView.GraphAdapter {
54 | private static CommentPathAdapter instance;
55 | private static final float xOffsetScale = 0.06f;
56 | private static final float yOffsetScale = 0.2f;
57 | //可用单例模式
58 | public static CommentPathAdapter getInstance() {
59 | synchronized (CommentPathAdapter.class) {
60 | if (null == instance) {
61 | instance = new CommentPathAdapter();
62 | }
63 | }
64 | return instance;
65 | }
66 | //这里绘制你想要的图形
67 | @Override
68 | public Path getGraphPath(LikeView view, int length) {
69 | Path path = new Path();
70 | int dx = (int) (length * xOffsetScale);
71 | int dy = (int) (length * yOffsetScale);
72 | int w = (int) (length * (1 - xOffsetScale * 2));
73 | int h = (int) (length * (1 - yOffsetScale * 2));
74 | path.moveTo(dx, dy);
75 | path.lineTo(dx + w, dy);
76 | path.lineTo(dx + w, dy + h);
77 | path.lineTo(dx + (w * 0.35f), dy + h);
78 | path.lineTo(dx + (w * 0.1f), dy + (h * 1.4f));
79 | path.lineTo(dx + (w * 0.1f), dy + h);
80 | path.lineTo(dx, dy + h);
81 | path.lineTo(dx, dy);
82 | return path;
83 | }
84 | }
85 | ```
86 | ## 自定义配置
87 | ```
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
104 |
105 |
106 |
107 |
108 |
109 |
110 |
111 |
112 |
113 |
114 |
115 |
116 |
117 |
118 |
119 |
120 |
121 |
122 |
123 |
124 |
125 |
126 |
127 |
128 |
129 | ```
130 | ## 接口
131 | ```java
132 | /**
133 | * 事件监听接口
134 | */
135 | public interface Callback {
136 | /**
137 | * 点击事件监听
138 | *
139 | * @param view 当前View
140 | * @return 返回true则代表不使用默认的点击事件
141 | */
142 | boolean onClick(LikeView view);
143 |
144 | /**
145 | * 变为激活状态
146 | *
147 | * @param view 当前View
148 | */
149 | void activate(LikeView view);
150 |
151 | /**
152 | * 变为不激活状态
153 | *
154 | * @param view 当前View
155 | */
156 | void deactivate(LikeView view);
157 | }
158 |
159 | /**
160 | * 获取图形Path接口
161 | */
162 | public interface GraphAdapter {
163 | /**
164 | * 获取图形的Path
165 | *
166 | * @param view 当前View
167 | * @param length 可绘制图形区域正方形的边长
168 | * @return 带有图形的Path
169 | */
170 | Path getGraphPath(LikeView view, int length);
171 | }
172 |
173 | ```
174 | ## 声明
175 | 此项目为练手项目,当中可能存在BUG,发现BUG请指出,谢谢
176 | # License
177 | ```
178 | Copyright 2016 zFxiang
179 |
180 | Licensed under the Apache License, Version 2.0 (the "License");
181 | you may not use this file except in compliance with the License.
182 | You may obtain a copy of the License at
183 |
184 | http://www.apache.org/licenses/LICENSE-2.0
185 |
186 | Unless required by applicable law or agreed to in writing, software
187 | distributed under the License is distributed on an "AS IS" BASIS,
188 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
189 | See the License for the specific language governing permissions and
190 | limitations under the License.
191 | ```
192 |
--------------------------------------------------------------------------------
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 24
5 | buildToolsVersion "25.0.0"
6 | enforceUniquePackageName = false
7 | defaultConfig {
8 | applicationId "cn.izouxiang.likeview"
9 | minSdkVersion 19
10 | targetSdkVersion 24
11 | versionCode 1
12 | versionName "1.0"
13 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
14 | }
15 | buildTypes {
16 | release {
17 | minifyEnabled false
18 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
19 | }
20 | }
21 | }
22 |
23 | dependencies {
24 | compile project(':likeview')
25 | compile fileTree(dir: 'libs', include: ['*.jar'])
26 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
27 | exclude group: 'com.android.support', module: 'support-annotations'
28 | })
29 | compile 'com.android.support:appcompat-v7:24.2.1'
30 | compile 'com.android.support:recyclerview-v7:24.2.1'
31 | compile 'com.android.support:design:24.2.1'
32 | testCompile 'junit:junit:4.12'
33 | }
34 |
--------------------------------------------------------------------------------
/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 H:\WorkApps\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 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
--------------------------------------------------------------------------------
/app/src/main/java/cn/izouxiang/likeviewdemo/MainActivity.java:
--------------------------------------------------------------------------------
1 | package cn.izouxiang.likeviewdemo;
2 |
3 | import android.os.Bundle;
4 | import android.support.v7.app.AppCompatActivity;
5 | import android.support.v7.widget.LinearLayoutManager;
6 | import android.support.v7.widget.RecyclerView;
7 |
8 | import java.util.ArrayList;
9 | import java.util.List;
10 |
11 | import cn.izouxiang.likeview.R;
12 |
13 | public class MainActivity extends AppCompatActivity {
14 | private List ssEntities;
15 | @Override
16 | protected void onCreate(Bundle savedInstanceState) {
17 | super.onCreate(savedInstanceState);
18 | setContentView(R.layout.activity_main);
19 | initData();
20 | RecyclerView rcv = (RecyclerView) findViewById(R.id.rcv);
21 | rcv.setAdapter(new SSAdapter(ssEntities,this));
22 | rcv.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
23 | }
24 | private void initData(){
25 | ssEntities = new ArrayList<>();
26 | ssEntities.add(new SSEntity("小明",true,100,1));
27 | ssEntities.add(new SSEntity("小红",false,99,0));
28 | ssEntities.add(new SSEntity("小强",true,102,20));
29 | ssEntities.add(new SSEntity("小黑",false,103,10));
30 | ssEntities.add(new SSEntity("小白",false,109,5));
31 | ssEntities.add(new SSEntity("小黑",true,110,2));
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/app/src/main/java/cn/izouxiang/likeviewdemo/SSAdapter.java:
--------------------------------------------------------------------------------
1 | package cn.izouxiang.likeviewdemo;
2 |
3 | import android.content.Context;
4 | import android.support.annotation.NonNull;
5 | import android.support.design.widget.Snackbar;
6 | import android.support.v7.widget.RecyclerView;
7 | import android.view.LayoutInflater;
8 | import android.view.View;
9 | import android.view.ViewGroup;
10 | import android.widget.ImageView;
11 | import android.widget.TextView;
12 |
13 | import java.util.List;
14 |
15 | import cn.izouxiang.likeview.CommentPathAdapter;
16 | import cn.izouxiang.likeview.LikeView;
17 | import cn.izouxiang.likeview.R;
18 |
19 | /**
20 | * Created by zouxiang on 2016/12/1.
21 | */
22 |
23 | public class SSAdapter extends RecyclerView.Adapter {
24 |
25 | private List ssEntities;
26 | private Context mContext;
27 |
28 | public SSAdapter(@NonNull List ssEntities, @NonNull Context mContext) {
29 | this.ssEntities = ssEntities;
30 | this.mContext = mContext;
31 | }
32 |
33 | @Override
34 | public SSHolder onCreateViewHolder(ViewGroup parent, int viewType) {
35 | View view = LayoutInflater.from(mContext).inflate(R.layout.layout_item, parent, false);
36 | return new SSHolder(view);
37 | }
38 |
39 | @Override
40 | public void onBindViewHolder(final SSHolder holder, int position) {
41 | final SSEntity entity = ssEntities.get(position);
42 | //点赞view的设置
43 | holder.like.setActivated(entity.isLike);
44 | holder.like.setNumber(entity.likeNum);
45 | holder.like.setCallback(new LikeView.SimpleCallback() {
46 | @Override
47 | public void activate(LikeView view) {
48 | Snackbar.make(view, "你觉得" + entity.name + "很赞!", Snackbar.LENGTH_SHORT).show();
49 | }
50 |
51 | @Override
52 | public void deactivate(LikeView view) {
53 | Snackbar.make(view, "你取消了对" + entity.name + "的赞!", Snackbar.LENGTH_SHORT).show();
54 | }
55 | });
56 | //评论view的设置
57 | holder.comment.setNumber(entity.commentNum);
58 | //设置图形适配器
59 | holder.comment.setGraphAdapter(CommentPathAdapter.getInstance());
60 | holder.comment.setCallback(new LikeView.SimpleCallback(){
61 | @Override
62 | public boolean onClick(LikeView view) {
63 | Snackbar.make(view, "你点击" + entity.name + "的评论按钮", Snackbar.LENGTH_SHORT).show();
64 | //返回true代表拦截此次点击,不使用默认的点击事件
65 | return true;
66 | }
67 | });
68 | holder.name.setText(entity.name);
69 | holder.content.setText(entity.content);
70 | }
71 |
72 | @Override
73 | public int getItemCount() {
74 | if (null != ssEntities) {
75 | return ssEntities.size();
76 | }
77 | return 0;
78 | }
79 |
80 | class SSHolder extends RecyclerView.ViewHolder {
81 | LikeView like;
82 | LikeView comment;
83 | ImageView icon;
84 | TextView name;
85 | TextView content;
86 |
87 | SSHolder(View itemView) {
88 | super(itemView);
89 | like = (LikeView) itemView.findViewById(R.id.lv);
90 | comment = (LikeView) itemView.findViewById(R.id.comment);
91 | icon = (ImageView) itemView.findViewById(R.id.icon);
92 | name = (TextView) itemView.findViewById(R.id.name);
93 | content = (TextView) itemView.findViewById(R.id.content);
94 | }
95 | }
96 | }
97 |
--------------------------------------------------------------------------------
/app/src/main/java/cn/izouxiang/likeviewdemo/SSEntity.java:
--------------------------------------------------------------------------------
1 | package cn.izouxiang.likeviewdemo;
2 |
3 | import cn.izouxiang.likeview.R;
4 |
5 | /**
6 | * Created by zouxiang on 2016/12/1.
7 | */
8 |
9 | public class SSEntity {
10 | public int icon = R.mipmap.ic_launcher;
11 | public String name;
12 | public String content = "今天贼嗨!今天贼嗨!今天贼嗨!今天贼嗨!今天贼嗨!今天贼嗨!今天贼嗨!今天贼嗨!今天贼嗨!今天贼嗨!今天贼嗨!今天贼嗨!今天贼嗨!";
13 | public boolean isLike;
14 | public int likeNum;
15 | public int commentNum;
16 |
17 | public SSEntity(String name, boolean isLike, int likeNum,int commentNum) {
18 | this.name = name;
19 | this.isLike = isLike;
20 | this.likeNum = likeNum;
21 | this.commentNum = commentNum;
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
15 |
16 |
17 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/layout_item.xml:
--------------------------------------------------------------------------------
1 |
2 |
11 |
12 |
17 |
18 |
24 |
25 |
32 |
33 |
34 |
41 |
42 |
47 |
48 |
54 |
55 |
71 |
72 |
79 |
80 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fxzou/LikeView/27455b9de3023de73615fa6e9ba530feae3885a3/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fxzou/LikeView/27455b9de3023de73615fa6e9ba530feae3885a3/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fxzou/LikeView/27455b9de3023de73615fa6e9ba530feae3885a3/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fxzou/LikeView/27455b9de3023de73615fa6e9ba530feae3885a3/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fxzou/LikeView/27455b9de3023de73615fa6e9ba530feae3885a3/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/values-w820dp/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 64dp
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #3F51B5
4 | #303F9F
5 | #FF4081
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 | LikeView
3 |
4 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/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 | }
7 | dependencies {
8 | classpath 'com.android.tools.build:gradle:2.2.2'
9 |
10 | // NOTE: Do not place your application dependencies here; they belong
11 | // in the individual module build.gradle files
12 | }
13 | }
14 |
15 | allprojects {
16 | repositories {
17 | jcenter()
18 | }
19 | }
20 |
21 | task clean(type: Delete) {
22 | delete rootProject.buildDir
23 | }
24 |
--------------------------------------------------------------------------------
/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/fxzou/LikeView/27455b9de3023de73615fa6e9ba530feae3885a3/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Mon Dec 28 10:00:20 PST 2015
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-2.14.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 | # 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 |
--------------------------------------------------------------------------------
/img/likeview.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fxzou/LikeView/27455b9de3023de73615fa6e9ba530feae3885a3/img/likeview.gif
--------------------------------------------------------------------------------
/likeview/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/likeview/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 |
3 | android {
4 | compileSdkVersion 24
5 | buildToolsVersion "25.0.0"
6 |
7 | defaultConfig {
8 | minSdkVersion 19
9 | targetSdkVersion 24
10 | versionCode 1
11 | versionName "1.0"
12 |
13 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
14 |
15 | }
16 | buildTypes {
17 | release {
18 | minifyEnabled false
19 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
20 | }
21 | }
22 | }
23 |
24 | dependencies {
25 | compile fileTree(dir: 'libs', include: ['*.jar'])
26 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
27 | exclude group: 'com.android.support', module: 'support-annotations'
28 | })
29 | compile 'com.android.support:appcompat-v7:24.2.1'
30 | testCompile 'junit:junit:4.12'
31 | }
32 |
--------------------------------------------------------------------------------
/likeview/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 H:\WorkApps\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 |
--------------------------------------------------------------------------------
/likeview/src/androidTest/java/cn/izouxiang/likeview/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package cn.izouxiang.likeview;
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("cn.izouxiang.likeview.test", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/likeview/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
3 |
4 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/likeview/src/main/java/cn/izouxiang/likeview/CommentPathAdapter.java:
--------------------------------------------------------------------------------
1 | package cn.izouxiang.likeview;
2 |
3 | import android.graphics.Path;
4 |
5 | /**
6 | * Created by zouxiang on 2016/12/5.
7 | */
8 |
9 | public class CommentPathAdapter implements LikeView.GraphAdapter {
10 | private static CommentPathAdapter instance;
11 | private static final float xOffsetScale = 0.06f;
12 | private static final float yOffsetScale = 0.2f;
13 | //可用单例模式
14 | public static CommentPathAdapter getInstance() {
15 | synchronized (CommentPathAdapter.class) {
16 | if (null == instance) {
17 | instance = new CommentPathAdapter();
18 | }
19 | }
20 | return instance;
21 | }
22 | //这里绘制你想要的图形
23 | @Override
24 | public Path getGraphPath(LikeView view, int length) {
25 | Path path = new Path();
26 | int dx = (int) (length * xOffsetScale);
27 | int dy = (int) (length * yOffsetScale);
28 | int w = (int) (length * (1 - xOffsetScale * 2));
29 | int h = (int) (length * (1 - yOffsetScale * 2));
30 | path.moveTo(dx, dy);
31 | path.lineTo(dx + w, dy);
32 | path.lineTo(dx + w, dy + h);
33 | path.lineTo(dx + (w * 0.35f), dy + h);
34 | path.lineTo(dx + (w * 0.1f), dy + (h * 1.4f));
35 | path.lineTo(dx + (w * 0.1f), dy + h);
36 | path.lineTo(dx, dy + h);
37 | path.lineTo(dx, dy);
38 | return path;
39 | }
40 | }
41 |
42 | // 添加时间备注 2023-06-13
43 | // 添加时间备注 2023-06-14
44 | // 添加时间备注 2023-06-15
45 | // 添加时间备注 2023-06-16
46 | // 添加时间备注 2023-06-17
47 | // 添加时间备注 2023-06-18
48 | // 添加时间备注 2023-06-19
49 | // 添加时间备注 2023-06-20
50 | // 添加时间备注 2023-06-21
51 | // 添加时间备注 2023-06-22
52 | // 添加时间备注 2023-06-23
53 | // 添加时间备注 2023-06-24
54 | // 添加时间备注 2023-06-25
55 | // 添加时间备注 2023-06-26
56 | // 添加时间备注 2023-06-27
57 | // 添加时间备注 2023-06-28
58 | // 添加时间备注 2023-06-29
59 | // 添加时间备注 2023-06-30
60 | // 添加时间备注 2023-07-01
61 | // 添加时间备注 2023-07-02
62 | // 添加时间备注 2023-07-03
63 | // 添加时间备注 2023-07-04
64 | // 添加时间备注 2023-07-05
65 | // 添加时间备注 2023-07-06
66 | // 添加时间备注 2023-07-07
67 | // 添加时间备注 2023-07-08
68 | // 添加时间备注 2023-07-09
69 | // 添加时间备注 2023-07-10
70 | // 添加时间备注 2023-07-11
71 | // 添加时间备注 2023-07-12
72 | // 添加时间备注 2023-07-13
73 | // 添加时间备注 2023-07-14
74 | // 添加时间备注 2023-07-15
75 | // 添加时间备注 2023-07-16
76 | // 添加时间备注 2023-07-17
77 | // 添加时间备注 2023-07-18
78 | // 添加时间备注 2023-07-19
79 | // 添加时间备注 2023-07-20
80 | // 添加时间备注 2023-07-22
81 | // 添加时间备注 2023-07-23
82 | // 添加时间备注 2023-07-24
83 | // 添加时间备注 2023-07-25
84 | // 添加时间备注 2023-07-26
85 | // 添加时间备注 2023-07-27
86 | // 添加时间备注 2023-07-28
87 | // 添加时间备注 2023-07-29
88 | // 添加时间备注 2023-07-30
89 | // 添加时间备注 2023-07-31
90 | // 添加时间备注 2023-08-01
91 | // 添加时间备注 2023-08-02
92 | // 添加时间备注 2023-08-03
93 | // 添加时间备注 2023-08-04
94 | // 添加时间备注 2023-08-05
95 | // 添加时间备注 2023-08-06
96 | // 添加时间备注 2023-08-07
97 | // 添加时间备注 2023-08-08
98 | // 添加时间备注 2023-08-09
99 | // 添加时间备注 2023-08-10
100 | // 添加时间备注 2023-08-11
101 | // 添加时间备注 2023-08-12
102 | // 添加时间备注 2023-08-13
103 | // 添加时间备注 2023-08-14
104 | // 添加时间备注 2023-08-15
105 | // 添加时间备注 2023-08-16
106 | // 添加时间备注 2023-08-17
107 | // 添加时间备注 2023-08-18
108 | // 添加时间备注 2023-08-19
109 | // 添加时间备注 2023-08-20
110 | // 添加时间备注 2023-08-21
111 | // 添加时间备注 2023-08-22
112 | // 添加时间备注 2023-08-23
113 | // 添加时间备注 2023-08-24
114 | // 添加时间备注 2023-08-25
115 | // 添加时间备注 2023-08-26
116 | // 添加时间备注 2023-08-27
117 | // 添加时间备注 2023-08-28
118 | // 添加时间备注 2023-08-29
119 | // 添加时间备注 2023-08-30
120 | // 添加时间备注 2023-08-31
121 | // 添加时间备注 2023-09-01
122 | // 添加时间备注 2023-09-02
123 | // 添加时间备注 2023-09-03
124 | // 添加时间备注 2023-09-04
125 | // 添加时间备注 2023-09-04
126 |
--------------------------------------------------------------------------------
/likeview/src/main/java/cn/izouxiang/likeview/LikeView.java:
--------------------------------------------------------------------------------
1 | package cn.izouxiang.likeview;
2 |
3 | import android.animation.Animator;
4 | import android.animation.AnimatorListenerAdapter;
5 | import android.animation.ObjectAnimator;
6 | import android.content.Context;
7 | import android.content.res.TypedArray;
8 | import android.graphics.Canvas;
9 | import android.graphics.Color;
10 | import android.graphics.Paint;
11 | import android.graphics.Path;
12 | import android.support.annotation.ColorInt;
13 | import android.text.TextPaint;
14 | import android.util.AttributeSet;
15 | import android.util.TypedValue;
16 | import android.view.View;
17 |
18 | import cn.izouxiang.likeview.util.PathUtils;
19 |
20 | /**
21 | * Created by zouxiang on 2016/11/23.
22 | */
23 |
24 | public class LikeView extends View {
25 | private static final String TAG = "LikeView";
26 | public static final int GRAVITY_CENTER = 1;
27 | public static final int GRAVITY_LEFT = 2;
28 | public static final int GRAVITY_RIGHT = 3;
29 | public static final int GRAVITY_START = 4;
30 | private long number;
31 | private String preNumStr = "";
32 | private String postOldNumStr = "";
33 | private String postNewNumStr = "";
34 | private String oldNumStr = "";
35 | private String newNumStr = "";
36 | private float numAnimateScale = 0;
37 | private float graphAnimateScale = 0;
38 | private boolean isNumAnimateRunning = false;
39 | private boolean isGraphAnimateRunning = false;
40 | private TextPaint textPaint;
41 | private Paint graphPaint;
42 | private Paint animatePaint;
43 | private float graphStartX;
44 | private float graphStartY;
45 | private float graphLength;
46 | private float textStartX;
47 | private float textStartY;
48 | private float textWidth;
49 | private float preNumWidth;
50 | private float textHeight;
51 | private float textBaseLineHeight;
52 | private int direction;
53 | @ColorInt
54 | //文本颜色
55 | private int textColor;
56 | @ColorInt
57 | //图形颜色
58 | private int graphColor;
59 | @ColorInt
60 | //图形动画颜色
61 | private int animateColor;
62 | private int textSize;
63 | private ObjectAnimator numAnimator;
64 | private ObjectAnimator graphAnimator;
65 | private boolean isActivated;
66 | //动画时间
67 | private long animateDuration;
68 | private int defWidth;
69 | private int defHeight;
70 | //图形与文本间的距离
71 | private int distance;
72 | //图形与文本间的高度比
73 | private float graphTextHeightRatio;
74 | private int graphStrokeWidth;
75 | private int textStrokeWidth;
76 | private Callback callback = new SimpleCallback();
77 | private GraphAdapter graphAdapter = HeartGraphAdapter.getInstance();
78 | private Path graphPath;
79 | //是否开启预先处理文本宽度,即测量当前值+1/-1后文本宽度变化,取最大值
80 | private boolean autoMeasureMaxTextWidth;
81 | //是否允许取消
82 | private boolean notAllowedCancel;
83 | private int gravity;
84 |
85 | public LikeView(Context context) {
86 | super(context);
87 | init(null);
88 | }
89 |
90 | public LikeView(Context context, AttributeSet attrs) {
91 | super(context, attrs);
92 | init(attrs);
93 | }
94 |
95 | public LikeView(Context context, AttributeSet attrs, int defStyleAttr) {
96 | super(context, attrs, defStyleAttr);
97 | init(attrs);
98 | }
99 |
100 | private void init(AttributeSet attrs) {
101 | if (null == attrs) {
102 | number = 0;
103 | graphColor = textColor = Color.parseColor("#888888");
104 | animateColor = Color.parseColor("#ca5f5f");
105 | textSize = sp2px(14);
106 | isActivated = false;
107 | animateDuration = 300L;
108 | distance = dp2px(3);
109 | graphStrokeWidth = dp2px(3);
110 | textStrokeWidth = dp2px(2);
111 | graphTextHeightRatio = 1.3f;
112 | autoMeasureMaxTextWidth = true;
113 | notAllowedCancel = false;
114 | gravity = GRAVITY_CENTER;
115 | } else {
116 | TypedArray ta = getContext().obtainStyledAttributes(attrs, R.styleable.LikeView);
117 | try {
118 | number = ta.getInt(R.styleable.LikeView_number, 0);
119 | graphColor = ta.getColor(R.styleable.LikeView_graphColor, Color.parseColor("#888888"));
120 | textColor = ta.getColor(R.styleable.LikeView_textColor, Color.parseColor("#888888"));
121 | animateColor = ta.getColor(R.styleable.LikeView_animateColor, Color.parseColor("#ca5f5f"));
122 | textSize = ta.getDimensionPixelSize(R.styleable.LikeView_textSize, sp2px(14));
123 | isActivated = ta.getBoolean(R.styleable.LikeView_isActivated, false);
124 | autoMeasureMaxTextWidth = ta.getBoolean(R.styleable.LikeView_autoMeasureMaxWidth, true);
125 | notAllowedCancel = ta.getBoolean(R.styleable.LikeView_notAllowedCancel, false);
126 | animateDuration = ta.getInt(R.styleable.LikeView_animateDuration, 300);
127 | distance = ta.getDimensionPixelSize(R.styleable.LikeView_distance, dp2px(3));
128 | graphStrokeWidth = ta.getDimensionPixelSize(R.styleable.LikeView_graphStrokeWidth, dp2px(3));
129 | textStrokeWidth = ta.getDimensionPixelSize(R.styleable.LikeView_textStrokeWidth, dp2px(2));
130 | graphTextHeightRatio = ta.getFloat(R.styleable.LikeView_graphTextHeightRatio, 1.3f);
131 | gravity = ta.getInteger(R.styleable.LikeView_gravity,GRAVITY_CENTER);
132 | } finally {
133 | ta.recycle();
134 | }
135 | }
136 | textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
137 | textPaint.setColor(textColor);
138 | textPaint.setTextSize(textSize);
139 | textPaint.setStrokeWidth(textStrokeWidth);
140 | graphPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
141 | graphPaint.setStyle(Paint.Style.STROKE);
142 | graphPaint.setColor(graphColor);
143 | graphPaint.setStrokeWidth(graphStrokeWidth);
144 | animatePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
145 | animatePaint.setColor(animateColor);
146 | animatePaint.setStyle(Paint.Style.FILL_AND_STROKE);
147 | newNumStr = String.valueOf(number);
148 | measureTextHeightAndBaseLineHeight();
149 | measureTextWidth();
150 | measureDefWidthAndDefHeight();
151 | this.setOnClickListener(new OnClickListener() {
152 | @Override
153 | public void onClick(View v) {
154 | if (!callback.onClick(LikeView.this)) {
155 | trigger();
156 | }
157 | }
158 | });
159 | }
160 |
161 | /**
162 | * 修改数值
163 | *
164 | * @param number 新值
165 | */
166 | public void setNumber(long number) {
167 | setNumber(number, false);
168 | }
169 |
170 | /**
171 | * 修改数值
172 | *
173 | * @param number 新值
174 | * @param animate 是否启用动画
175 | */
176 | public void setNumber(long number, boolean animate) {
177 | if (animate) {
178 | add(number - this.number);
179 | } else {
180 | this.number = number;
181 | postInvalidate();
182 | }
183 | }
184 |
185 | /**
186 | * 在原先的数值上加值
187 | */
188 | public void add(long num) {
189 | if (num == 0) {
190 | return;
191 | }
192 | long newNum = number + num;
193 | oldNumStr = String.valueOf(number);
194 | newNumStr = String.valueOf(newNum);
195 | number = newNum;
196 | handleNewString();
197 | roll(num > 0);
198 | }
199 |
200 | /**
201 | * 激活
202 | */
203 | public void activate() {
204 | graphAnimate(0f, 1f);
205 | isActivated = true;
206 | callback.activate(this);
207 | }
208 |
209 | /**
210 | * 取消激活
211 | */
212 | public void deactivate() {
213 | graphAnimate(1f, 0f);
214 | isActivated = false;
215 | callback.deactivate(this);
216 | }
217 |
218 | /**
219 | * 切换当前状态
220 | */
221 | public void trigger() {
222 | if (isActivated) {
223 | if (!notAllowedCancel) {
224 | deactivateAndTallyDown();
225 | }
226 | } else {
227 | activateAndPlusOne();
228 | }
229 | }
230 |
231 | /**
232 | * 激活并且给数值加1
233 | */
234 | public void activateAndPlusOne() {
235 | add(1);
236 | activate();
237 | }
238 |
239 | /**
240 | * 取消激活并且给数组减1
241 | */
242 | public void deactivateAndTallyDown() {
243 | add(-1);
244 | deactivate();
245 | }
246 |
247 | /**
248 | * 测量文本高度和基线高度
249 | */
250 | private void measureTextHeightAndBaseLineHeight() {
251 | Paint.FontMetrics metrics = textPaint.getFontMetrics();
252 | textHeight = metrics.bottom - metrics.top;
253 | textBaseLineHeight = -metrics.top;
254 | }
255 |
256 | /**
257 | * 测量View的默认宽高
258 | */
259 | private void measureDefWidthAndDefHeight() {
260 | int oldDefHeight = defHeight;
261 | defHeight = (int) (getPaddingBottom() + getPaddingTop() + textHeight * graphTextHeightRatio);
262 | defWidth = (int) (getPaddingLeft() + getPaddingRight() + defHeight + textWidth + distance);
263 | if(defHeight != oldDefHeight){
264 | measureGraphLength();
265 | getGraphPath();
266 | measureStartPoint();
267 | }
268 | }
269 |
270 | private void measureGraphLength(){
271 | graphLength = defHeight - getPaddingTop() - getPaddingBottom();
272 | }
273 | private void getGraphPath(){
274 | graphPath = graphAdapter.getGraphPath(this, (int) graphLength);
275 | }
276 | /**
277 | * 对比字符串,并测量文本宽度
278 | */
279 | private void handleNewString() {
280 | if (oldNumStr == null || newNumStr == null || oldNumStr.equals(newNumStr)) {
281 | return;
282 | }
283 | if (oldNumStr.length() != newNumStr.length()) {
284 | preNumStr = "";
285 | postOldNumStr = oldNumStr;
286 | postNewNumStr = newNumStr;
287 | } else {
288 | int length = oldNumStr.length();
289 | int i;
290 | for (i = 0; i < length; i++) {
291 | if (newNumStr.charAt(i) != oldNumStr.charAt(i)) {
292 | break;
293 | }
294 | }
295 | preNumStr = oldNumStr.substring(0, i);
296 | postOldNumStr = oldNumStr.substring(i);
297 | postNewNumStr = newNumStr.substring(i);
298 | }
299 | measureTextWidthChanged();
300 | }
301 | private void measureTextWidthChanged(){
302 | float oldTextWidth = textWidth;
303 | measureTextWidth();
304 | //如果宽度改变,则要重新测量默认宽高
305 | if (oldTextWidth != textWidth) {
306 | int oldDefWidth = defWidth;
307 | int oldDefHeight = defHeight;
308 | measureDefWidthAndDefHeight();
309 | //如果新的默认宽高不等于原来的值,就需要请求重新布局
310 | if ((defWidth != oldDefWidth || defHeight != oldDefHeight)) {
311 | requestLayout();
312 | }
313 | }
314 | }
315 | /**
316 | * 测量文本宽度
317 | */
318 | private void measureTextWidth() {
319 | //测量共同文本的宽度
320 | preNumWidth = textPaint.measureText(preNumStr);
321 |
322 | if (autoMeasureMaxTextWidth) {//是否自动测量变化文本宽度
323 | //先算出新旧字符串的最大宽度
324 | float width = maxTextWidth(oldNumStr, newNumStr);
325 | //与加一比较还是与减一比较
326 | int offset = isActivated ? -1 : 1;
327 | //再和修改后的字符比较
328 | textWidth = Math.max(width, textPaint.measureText(String.valueOf(number + offset)));
329 | } else {
330 | textWidth = maxTextWidth(oldNumStr, newNumStr);
331 | }
332 |
333 | }
334 |
335 | /**
336 | * 获取最大的文本宽度
337 | */
338 | private float maxTextWidth(String oldStr, String newStr) {
339 | return Math.max(textPaint.measureText(oldStr), textPaint.measureText(newStr));
340 | }
341 |
342 | /**
343 | * 滑动数字
344 | *
345 | * @param rollUp 为true时向上滑
346 | */
347 | private void roll(boolean rollUp) {
348 | if (numAnimator != null && numAnimator.isRunning()) {
349 | numAnimator.cancel();
350 | }
351 | direction = rollUp ? 1 : -1;
352 | numAnimator = ObjectAnimator.ofFloat(this, "numAnimateScale", 0f, 1f);
353 | numAnimator.setDuration(animateDuration);
354 | isNumAnimateRunning = true;
355 | numAnimator.addListener(new AnimatorListenerAdapter() {
356 | @Override
357 | public void onAnimationEnd(Animator animation) {
358 | isNumAnimateRunning = false;
359 | }
360 | });
361 | numAnimator.start();
362 | }
363 |
364 | /**
365 | * 图形动画
366 | *
367 | * @param start 开始值
368 | * @param end 结束值
369 | */
370 | private void graphAnimate(float start, float end) {
371 | if (graphAnimator != null && graphAnimator.isRunning()) {
372 | graphAnimator.cancel();
373 | }
374 | graphAnimator = ObjectAnimator.ofFloat(this, "graphAnimateScale", start, end);
375 | graphAnimator.setDuration(animateDuration);
376 | isGraphAnimateRunning = true;
377 | graphAnimator.addListener(new AnimatorListenerAdapter() {
378 | @Override
379 | public void onAnimationEnd(Animator animation) {
380 | isGraphAnimateRunning = false;
381 | }
382 | });
383 | graphAnimator.start();
384 | }
385 |
386 | @Override
387 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
388 | setMeasuredDimension(measureMeasureSpec(widthMeasureSpec, defWidth), measureMeasureSpec(heightMeasureSpec, defHeight));
389 | }
390 |
391 | /**
392 | * 测量View的宽高
393 | *
394 | * @param measureSpec MeasureSpec实例
395 | * @param defaultVal 默认值,当mode为UNSPECIFIED和AT_MOST时使用默认值
396 | * @return 测量后的宽高
397 | */
398 | private int measureMeasureSpec(int measureSpec, int defaultVal) {
399 | int result = 0;
400 | int mode = MeasureSpec.getMode(measureSpec);
401 | int size = MeasureSpec.getSize(measureSpec);
402 | switch (mode) {
403 | case MeasureSpec.EXACTLY:
404 | result = size;
405 | break;
406 | case MeasureSpec.UNSPECIFIED:
407 | case MeasureSpec.AT_MOST:
408 | result = defaultVal;
409 | break;
410 | }
411 | return result;
412 | }
413 |
414 | @Override
415 | protected void onSizeChanged(int w, int h, int oldw, int oldh) {
416 | super.onSizeChanged(w, h, oldw, oldh);
417 | measureStartPoint();
418 | }
419 |
420 | private void measureStartPoint(){
421 | int w = getWidth();
422 | int h = getHeight();
423 | //对齐显示偏移量
424 | int dx;
425 | int dy;
426 | switch (gravity){
427 | case GRAVITY_CENTER :
428 | dx = (w - defWidth) / 2;
429 | dy = (h - defHeight) / 2;
430 | break;
431 | case GRAVITY_LEFT :
432 | dx = 0;
433 | dy = (h - defHeight) / 2;
434 | break;
435 | case GRAVITY_RIGHT :
436 | dx = w - defWidth;
437 | dy = (h - defHeight) / 2;
438 | break;
439 | case GRAVITY_START :
440 | default:
441 | dx = 0;
442 | dy = 0;
443 |
444 | }
445 | graphStartX = getPaddingLeft() + dx;
446 | graphStartY = getPaddingTop() + dy;
447 | textStartX = graphStartX + graphLength + distance;
448 | textStartY = graphStartY + textHeight * (graphTextHeightRatio - 1) / 2;
449 | }
450 |
451 | @Override
452 | protected void onDraw(Canvas canvas) {
453 | drawGraph(canvas);
454 | drawNumber(canvas);
455 |
456 | }
457 |
458 | /**
459 | * 画数字
460 | */
461 | private void drawNumber(Canvas canvas) {
462 | canvas.save();
463 | canvas.translate(textStartX, textStartY);
464 | if (!isNumAnimateRunning) { //是否正在动画中
465 | //直接绘制数字
466 | canvas.drawText(String.valueOf(number), 0, textBaseLineHeight, textPaint);
467 | } else {
468 | //根据方向来计算位置
469 | numAnimateScale *= -direction;
470 | //裁剪文本框
471 | canvas.clipRect(0, 0, 0 + textWidth, 0 + textHeight);
472 | //绘制相同文本
473 | canvas.drawText(preNumStr, 0, textBaseLineHeight, textPaint);
474 | //绘制旧文本
475 | canvas.drawText(postOldNumStr, preNumWidth, textBaseLineHeight + textHeight * numAnimateScale, textPaint);
476 | //绘制新文本
477 | canvas.drawText(postNewNumStr, preNumWidth, textBaseLineHeight + textHeight * (numAnimateScale + direction), textPaint);
478 | }
479 | canvas.restore();
480 | }
481 |
482 | /**
483 | * 画图形
484 | */
485 | private void drawGraph(Canvas canvas) {
486 | canvas.save();
487 | canvas.translate(graphStartX, graphStartY);
488 | canvas.clipPath(graphPath);
489 | float radial = (float) (graphLength * Math.sqrt(2) / 2f);
490 |
491 | if (!isGraphAnimateRunning) {//如果不是动画中,就直接绘画
492 | if (isActivated) {
493 | //绘制激活状态
494 | canvas.drawCircle(graphLength / 2, graphLength / 2, radial, animatePaint);
495 | } else {
496 | //绘制图形
497 | canvas.drawPath(graphPath, graphPaint);
498 | }
499 | } else { //是动画中就要按比例绘画
500 | //修改外圈图形比例
501 | drawGraphPathByScale(canvas, 1 - graphAnimateScale);
502 | //修改内圈圆形比例
503 | canvas.drawCircle(graphLength / 2, graphLength / 2, radial * graphAnimateScale, animatePaint);
504 | }
505 | canvas.restore();
506 | }
507 |
508 | /**
509 | * 将图形缩放绘画
510 | *
511 | * @param canvas 画布
512 | * @param scale 比例
513 | */
514 | private void drawGraphPathByScale(Canvas canvas, float scale) {
515 | canvas.save();
516 | canvas.scale(scale, scale, graphLength / 2, graphLength / 2);
517 | canvas.drawPath(graphPath, graphPaint);
518 | canvas.restore();
519 | }
520 |
521 | private int dp2px(int dp) {
522 | return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, getResources().getDisplayMetrics());
523 | }
524 |
525 | private int sp2px(int sp) {
526 | return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, sp, getResources().getDisplayMetrics());
527 | }
528 |
529 | public int getTextColor() {
530 | return textColor;
531 | }
532 |
533 | public void setTextColor(@ColorInt int textColor) {
534 | this.textColor = textColor;
535 | textPaint.setColor(textColor);
536 | postInvalidate();
537 | }
538 |
539 | public int getGraphColor() {
540 | return graphColor;
541 | }
542 |
543 | public void setGraphColor(@ColorInt int graphColor) {
544 | this.graphColor = graphColor;
545 | graphPaint.setColor(graphColor);
546 | postInvalidate();
547 | }
548 |
549 | public int getAnimateColor() {
550 | return animateColor;
551 | }
552 |
553 | public void setAnimateColor(@ColorInt int animateColor) {
554 | this.animateColor = animateColor;
555 | animatePaint.setColor(animateColor);
556 | postInvalidate();
557 | }
558 |
559 | public int getTextSize() {
560 | return textSize;
561 | }
562 |
563 | public void setTextSize(int textSize) {
564 | this.textSize = textSize;
565 | textPaint.setTextSize(textSize);
566 | measureTextWidth();
567 | measureTextHeightAndBaseLineHeight();
568 | measureDefWidthAndDefHeight();
569 | requestLayout();
570 | }
571 |
572 | public boolean isActivated() {
573 | return isActivated;
574 | }
575 |
576 | @Override
577 | public void setActivated(boolean activated) {
578 | isActivated = activated;
579 | postInvalidate();
580 | }
581 |
582 | public long getAnimateDuration() {
583 | return animateDuration;
584 | }
585 |
586 | public void setAnimateDuration(long animateDuration) {
587 | this.animateDuration = animateDuration;
588 | }
589 |
590 | public int getDistance() {
591 | return distance;
592 | }
593 |
594 | public void setDistance(int distance) {
595 | this.distance = distance;
596 | measureDefWidthAndDefHeight();
597 | requestLayout();
598 | }
599 |
600 | public float getGraphTextHeightRatio() {
601 | return graphTextHeightRatio;
602 | }
603 |
604 | public void setGraphTextHeightRatio(float graphTextHeightRatio) {
605 | this.graphTextHeightRatio = graphTextHeightRatio;
606 | measureDefWidthAndDefHeight();
607 | requestLayout();
608 | }
609 |
610 | public int getTextStrokeWidth() {
611 | return textStrokeWidth;
612 | }
613 |
614 | public void setTextStrokeWidth(int textStrokeWidth) {
615 | this.textStrokeWidth = textStrokeWidth;
616 | textPaint.setStrokeWidth(textStrokeWidth);
617 | postInvalidate();
618 | }
619 |
620 | public int getGraphStrokeWidth() {
621 | return graphStrokeWidth;
622 | }
623 |
624 | public void setGraphStrokeWidth(int graphStrokeWidth) {
625 | this.graphStrokeWidth = graphStrokeWidth;
626 | graphPaint.setStrokeWidth(graphStrokeWidth);
627 | postInvalidate();
628 | }
629 |
630 | public void setGraphAdapter(GraphAdapter graphAdapter) {
631 | if (graphAdapter != null) {
632 | this.graphAdapter = graphAdapter;
633 | getGraphPath();
634 | postInvalidate();
635 | }
636 | }
637 |
638 | public void setCallback(Callback callback) {
639 | if (callback != null) {
640 | this.callback = callback;
641 | }
642 | }
643 |
644 | public boolean isAutoMeasureMaxTextWidth() {
645 | return autoMeasureMaxTextWidth;
646 | }
647 |
648 | public void setAutoMeasureMaxTextWidth(boolean autoMeasureMaxTextWidth) {
649 | this.autoMeasureMaxTextWidth = autoMeasureMaxTextWidth;
650 | measureTextWidth();
651 | }
652 |
653 | public boolean isNotAllowedCancel() {
654 | return notAllowedCancel;
655 | }
656 |
657 | public void setNotAllowedCancel(boolean notAllowedCancel) {
658 | this.notAllowedCancel = notAllowedCancel;
659 | }
660 |
661 | public int getGravity() {
662 | return gravity;
663 | }
664 |
665 | public void setGravity(int gravity) {
666 | this.gravity = gravity;
667 | }
668 |
669 | /**
670 | * 动画
671 | */
672 | public void setNumAnimateScale(float numAnimateScale) {
673 | this.numAnimateScale = numAnimateScale;
674 | postInvalidate();
675 | }
676 |
677 | public void setGraphAnimateScale(float graphAnimateScale) {
678 | this.graphAnimateScale = graphAnimateScale;
679 | postInvalidate();
680 | }
681 |
682 | /**
683 | * 事件监听接口
684 | */
685 | public interface Callback {
686 | /**
687 | * 点击事件监听
688 | *
689 | * @param view 当前View
690 | * @return 返回true则代表不使用默认的点击事件
691 | */
692 | boolean onClick(LikeView view);
693 |
694 | /**
695 | * 变为激活状态
696 | *
697 | * @param view 当前View
698 | */
699 | void activate(LikeView view);
700 |
701 | /**
702 | * 变为不激活状态
703 | *
704 | * @param view 当前View
705 | */
706 | void deactivate(LikeView view);
707 | }
708 |
709 | /**
710 | * 事件监听的默认实现
711 | */
712 | public static class SimpleCallback implements Callback {
713 |
714 | @Override
715 | public boolean onClick(LikeView view) {
716 | return false;
717 | }
718 |
719 | @Override
720 | public void activate(LikeView view) {
721 |
722 | }
723 |
724 | @Override
725 | public void deactivate(LikeView view) {
726 |
727 | }
728 | }
729 |
730 | /**
731 | * 获取图形Path接口
732 | */
733 | public interface GraphAdapter {
734 | /**
735 | * 获取图形的Path
736 | *
737 | * @param view 当前View
738 | * @param length 可绘制图形区域正方形的边长
739 | * @return 带有图形的Path
740 | */
741 | Path getGraphPath(LikeView view, int length);
742 | }
743 |
744 | /**
745 | * 桃心图形类
746 | */
747 | public static class HeartGraphAdapter implements GraphAdapter {
748 | private static HeartGraphAdapter instance;
749 | public static HeartGraphAdapter getInstance(){
750 | synchronized (HeartGraphAdapter.class){
751 | if(null == instance){
752 | instance = new HeartGraphAdapter();
753 | }
754 | }
755 | return instance;
756 | }
757 | @Override
758 | public Path getGraphPath(LikeView view, int length) {
759 | return PathUtils.getHeartPath(length);
760 | }
761 | }
762 | }
763 |
--------------------------------------------------------------------------------
/likeview/src/main/java/cn/izouxiang/likeview/util/PathUtils.java:
--------------------------------------------------------------------------------
1 | package cn.izouxiang.likeview.util;
2 |
3 | import android.graphics.Path;
4 |
5 | /**
6 | * Created by zouxiang on 2016/11/22.
7 | */
8 |
9 | public class PathUtils {
10 | /**
11 | * 在一个正方形中画一个爱心
12 | *
13 | * @param length 正方形边长
14 | * @param scale 缩放比
15 | * @return 包含Heart路径的Path
16 | */
17 | public static Path getHeartPath(int length, float scale) {
18 | if (length < 0 || scale < 0) {
19 | throw new IllegalArgumentException("参数异常");
20 | }
21 | float s = length / 242f * scale;
22 | float offset = length / 2f * (1 - scale);
23 | Path path = new Path();
24 | path.moveTo(121 * s + offset, 225 * s + offset);
25 | path.cubicTo(-96 * s + offset, 119 * s + offset, 40 * s + offset, -55 * s + offset, 121 * s + offset, 55 * s + offset);
26 | path.cubicTo(202 * s + offset, -55 * s + offset, 338 * s + offset, 119 * s + offset, 121 * s + offset, 225 * s + offset);
27 | return path;
28 | }
29 |
30 |
31 | public static Path getHeartPath(int length) {
32 | return getHeartPath(length, 1f);
33 | }
34 |
35 | /**
36 | * 添加一个路径到指定的Path
37 | * @param path 被添加的Path
38 | * @param x 起点x
39 | * @param y 起点y
40 | * @param length 正方形边长
41 | * @param scale 缩放比例
42 | */
43 | public static void addHeart(Path path, int x, int y, int length, float scale) {
44 | Path temp = getHeartPath(length, scale);
45 | temp.offset(x, y);
46 | path.addPath(temp);
47 | }
48 |
49 | public static void addHeart(Path path, int x, int y, int length) {
50 | Path temp = getHeartPath(length);
51 | temp.offset(x, y);
52 | path.addPath(temp);
53 | }
54 |
55 | }
56 |
--------------------------------------------------------------------------------
/likeview/src/main/res/values/attrs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
--------------------------------------------------------------------------------
/likeview/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | LikeView
3 |
4 |
--------------------------------------------------------------------------------
/likeview/src/test/java/cn/izouxiang/likeview/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package cn.izouxiang.likeview;
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', ':likeview'
2 |
--------------------------------------------------------------------------------