├── .gitignore
├── AndroidManifest.xml
├── LICENSE
├── NOTICE
├── README.md
├── pom.xml
├── res
├── drawable-hdpi
│ └── icon.png
├── drawable-ldpi
│ └── icon.png
├── drawable-mdpi
│ └── icon.png
├── layout
│ ├── cubic.xml
│ ├── quad.xml
│ ├── simple.xml
│ └── square.xml
└── values
│ └── strings.xml
└── src
└── main
└── java
└── org
└── example
└── androiddraw
├── ActivityListItem.java
├── CubicDraw.java
├── CubicDrawView.java
├── QuadDraw.java
├── QuadDrawView.java
├── Selection.java
├── SimpleDraw.java
├── SimpleDrawView.java
├── SquareSignatureDraw.java
└── SquareSignatureView.java
/.gitignore:
--------------------------------------------------------------------------------
1 | # built application files
2 | *.apk
3 | *.ap_
4 |
5 | # files for the dex VM
6 | *.dex
7 |
8 | # Java class files
9 | *.class
10 |
11 | # generated files
12 | bin/
13 | gen/
14 |
15 | # Local configuration file (sdk path, etc)
16 | local.properties
17 |
18 | # Eclipse project files
19 | .classpath
20 | .project
21 |
22 | # Intellij project files
23 | .idea/*
24 | _site/*
25 | *.iml
26 | tmp
27 | out
28 |
29 | # Maven
30 | target
31 |
32 |
33 |
--------------------------------------------------------------------------------
/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 |
2 | Apache License
3 | Version 2.0, January 2004
4 | http://www.apache.org/licenses/
5 |
6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7 |
8 | 1. Definitions.
9 |
10 | "License" shall mean the terms and conditions for use, reproduction,
11 | and distribution as defined by Sections 1 through 9 of this document.
12 |
13 | "Licensor" shall mean the copyright owner or entity authorized by
14 | the copyright owner that is granting the License.
15 |
16 | "Legal Entity" shall mean the union of the acting entity and all
17 | other entities that control, are controlled by, or are under common
18 | control with that entity. For the purposes of this definition,
19 | "control" means (i) the power, direct or indirect, to cause the
20 | direction or management of such entity, whether by contract or
21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
22 | outstanding shares, or (iii) beneficial ownership of such entity.
23 |
24 | "You" (or "Your") shall mean an individual or Legal Entity
25 | exercising permissions granted by this License.
26 |
27 | "Source" form shall mean the preferred form for making modifications,
28 | including but not limited to software source code, documentation
29 | source, and configuration files.
30 |
31 | "Object" form shall mean any form resulting from mechanical
32 | transformation or translation of a Source form, including but
33 | not limited to compiled object code, generated documentation,
34 | and conversions to other media types.
35 |
36 | "Work" shall mean the work of authorship, whether in Source or
37 | Object form, made available under the License, as indicated by a
38 | copyright notice that is included in or attached to the work
39 | (an example is provided in the Appendix below).
40 |
41 | "Derivative Works" shall mean any work, whether in Source or Object
42 | form, that is based on (or derived from) the Work and for which the
43 | editorial revisions, annotations, elaborations, or other modifications
44 | represent, as a whole, an original work of authorship. For the purposes
45 | of this License, Derivative Works shall not include works that remain
46 | separable from, or merely link (or bind by name) to the interfaces of,
47 | the Work and Derivative Works thereof.
48 |
49 | "Contribution" shall mean any work of authorship, including
50 | the original version of the Work and any modifications or additions
51 | to that Work or Derivative Works thereof, that is intentionally
52 | submitted to Licensor for inclusion in the Work by the copyright owner
53 | or by an individual or Legal Entity authorized to submit on behalf of
54 | the copyright owner. For the purposes of this definition, "submitted"
55 | means any form of electronic, verbal, or written communication sent
56 | to the Licensor or its representatives, including but not limited to
57 | communication on electronic mailing lists, source code control systems,
58 | and issue tracking systems that are managed by, or on behalf of, the
59 | Licensor for the purpose of discussing and improving the Work, but
60 | excluding communication that is conspicuously marked or otherwise
61 | designated in writing by the copyright owner as "Not a Contribution."
62 |
63 | "Contributor" shall mean Licensor and any individual or Legal Entity
64 | on behalf of whom a Contribution has been received by Licensor and
65 | subsequently incorporated within the Work.
66 |
67 | 2. Grant of Copyright License. Subject to the terms and conditions of
68 | this License, each Contributor hereby grants to You a perpetual,
69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70 | copyright license to reproduce, prepare Derivative Works of,
71 | publicly display, publicly perform, sublicense, and distribute the
72 | Work and such Derivative Works in Source or Object form.
73 |
74 | 3. Grant of Patent License. Subject to the terms and conditions of
75 | this License, each Contributor hereby grants to You a perpetual,
76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77 | (except as stated in this section) patent license to make, have made,
78 | use, offer to sell, sell, import, and otherwise transfer the Work,
79 | where such license applies only to those patent claims licensable
80 | by such Contributor that are necessarily infringed by their
81 | Contribution(s) alone or by combination of their Contribution(s)
82 | with the Work to which such Contribution(s) was submitted. If You
83 | institute patent litigation against any entity (including a
84 | cross-claim or counterclaim in a lawsuit) alleging that the Work
85 | or a Contribution incorporated within the Work constitutes direct
86 | or contributory patent infringement, then any patent licenses
87 | granted to You under this License for that Work shall terminate
88 | as of the date such litigation is filed.
89 |
90 | 4. Redistribution. You may reproduce and distribute copies of the
91 | Work or Derivative Works thereof in any medium, with or without
92 | modifications, and in Source or Object form, provided that You
93 | meet the following conditions:
94 |
95 | (a) You must give any other recipients of the Work or
96 | Derivative Works a copy of this License; and
97 |
98 | (b) You must cause any modified files to carry prominent notices
99 | stating that You changed the files; and
100 |
101 | (c) You must retain, in the Source form of any Derivative Works
102 | that You distribute, all copyright, patent, trademark, and
103 | attribution notices from the Source form of the Work,
104 | excluding those notices that do not pertain to any part of
105 | the Derivative Works; and
106 |
107 | (d) If the Work includes a "NOTICE" text file as part of its
108 | distribution, then any Derivative Works that You distribute must
109 | include a readable copy of the attribution notices contained
110 | within such NOTICE file, excluding those notices that do not
111 | pertain to any part of the Derivative Works, in at least one
112 | of the following places: within a NOTICE text file distributed
113 | as part of the Derivative Works; within the Source form or
114 | documentation, if provided along with the Derivative Works; or,
115 | within a display generated by the Derivative Works, if and
116 | wherever such third-party notices normally appear. The contents
117 | of the NOTICE file are for informational purposes only and
118 | do not modify the License. You may add Your own attribution
119 | notices within Derivative Works that You distribute, alongside
120 | or as an addendum to the NOTICE text from the Work, provided
121 | that such additional attribution notices cannot be construed
122 | as modifying the License.
123 |
124 | You may add Your own copyright statement to Your modifications and
125 | may provide additional or different license terms and conditions
126 | for use, reproduction, or distribution of Your modifications, or
127 | for any such Derivative Works as a whole, provided Your use,
128 | reproduction, and distribution of the Work otherwise complies with
129 | the conditions stated in this License.
130 |
131 | 5. Submission of Contributions. Unless You explicitly state otherwise,
132 | any Contribution intentionally submitted for inclusion in the Work
133 | by You to the Licensor shall be under the terms and conditions of
134 | this License, without any additional terms or conditions.
135 | Notwithstanding the above, nothing herein shall supersede or modify
136 | the terms of any separate license agreement you may have executed
137 | with Licensor regarding such Contributions.
138 |
139 | 6. Trademarks. This License does not grant permission to use the trade
140 | names, trademarks, service marks, or product names of the Licensor,
141 | except as required for reasonable and customary use in describing the
142 | origin of the Work and reproducing the content of the NOTICE file.
143 |
144 | 7. Disclaimer of Warranty. Unless required by applicable law or
145 | agreed to in writing, Licensor provides the Work (and each
146 | Contributor provides its Contributions) on an "AS IS" BASIS,
147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148 | implied, including, without limitation, any warranties or conditions
149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150 | PARTICULAR PURPOSE. You are solely responsible for determining the
151 | appropriateness of using or redistributing the Work and assume any
152 | risks associated with Your exercise of permissions under this License.
153 |
154 | 8. Limitation of Liability. In no event and under no legal theory,
155 | whether in tort (including negligence), contract, or otherwise,
156 | unless required by applicable law (such as deliberate and grossly
157 | negligent acts) or agreed to in writing, shall any Contributor be
158 | liable to You for damages, including any direct, indirect, special,
159 | incidental, or consequential damages of any character arising as a
160 | result of this License or out of the use or inability to use the
161 | Work (including but not limited to damages for loss of goodwill,
162 | work stoppage, computer failure or malfunction, or any and all
163 | other commercial damages or losses), even if such Contributor
164 | has been advised of the possibility of such damages.
165 |
166 | 9. Accepting Warranty or Additional Liability. While redistributing
167 | the Work or Derivative Works thereof, You may choose to offer,
168 | and charge a fee for, acceptance of support, warranty, indemnity,
169 | or other liability obligations and/or rights consistent with this
170 | License. However, in accepting such obligations, You may act only
171 | on Your own behalf and on Your sole responsibility, not on behalf
172 | of any other Contributor, and only if You agree to indemnify,
173 | defend, and hold each Contributor harmless for any liability
174 | incurred by, or claims asserted against, such Contributor by reason
175 | of your accepting any such warranty or additional liability.
176 |
177 | END OF TERMS AND CONDITIONS
178 |
179 | APPENDIX: How to apply the Apache License to your work.
180 |
181 | To apply the Apache License to your work, attach the following
182 | boilerplate notice, with the fields enclosed by brackets "[]"
183 | replaced with your own identifying information. (Don't include
184 | the brackets!) The text should be enclosed in the appropriate
185 | comment syntax for the file format. We also recommend that a
186 | file or class name and description of purpose be included on the
187 | same "printed page" as the copyright notice for easier
188 | identification within third-party archives.
189 |
190 | Copyright [yyyy] [name of copyright owner]
191 |
192 | Licensed under the Apache License, Version 2.0 (the "License");
193 | you may not use this file except in compliance with the License.
194 | You may obtain a copy of the License at
195 |
196 | http://www.apache.org/licenses/LICENSE-2.0
197 |
198 | Unless required by applicable law or agreed to in writing, software
199 | distributed under the License is distributed on an "AS IS" BASIS,
200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201 | See the License for the specific language governing permissions and
202 | limitations under the License.
--------------------------------------------------------------------------------
/NOTICE:
--------------------------------------------------------------------------------
1 | Copyright 2012 John Ericksen
2 |
3 | Licensed under the Apache License, Version 2.0 (the "License");
4 | you may not use this file except in compliance with the License.
5 | You may obtain a copy of the License at
6 |
7 | http://www.apache.org/licenses/LICENSE-2.0
8 |
9 | Unless required by applicable law or agreed to in writing, software
10 | distributed under the License is distributed on an "AS IS" BASIS,
11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | See the License for the specific language governing permissions and
13 | limitations under the License.
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Example Android Drawing
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 | 4.0.0
5 | org.example.androiddraw
6 | androidDraw
7 | 1.0.0-SNAPSHOT
8 | apk
9 | Android Draw Demo
10 |
11 |
12 |
13 | com.google.android
14 | android
15 | 2.2.1
16 | provided
17 |
18 |
19 | org.androidtransfuse
20 | transfuse
21 | 0.1.2
22 | provided
23 |
24 |
25 | org.androidtransfuse
26 | transfuse-api
27 | 0.1.2
28 |
29 |
30 |
31 |
32 |
33 |
34 | com.jayway.maven.plugins.android.generation2
35 | android-maven-plugin
36 | 3.2.0
37 | true
38 |
39 |
40 | maven-compiler-plugin
41 | 2.3.2
42 |
43 | 1.6
44 | 1.6
45 |
46 |
47 |
48 | com.mycila.maven-license-plugin
49 | maven-license-plugin
50 | 1.9.0
51 |
52 | NOTICE
53 |
54 | **/*.java
55 | **/*.xml
56 |
57 |
58 | **/.*/**
59 | target/**
60 | **/AndroidManifest.xml
61 |
62 | true
63 | true
64 |
65 |
66 |
67 |
68 | check
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
--------------------------------------------------------------------------------
/res/drawable-hdpi/icon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/johncarl81/androiddraw/f6973c2a0ce8d61ad2751734b628e3a79eff3f89/res/drawable-hdpi/icon.png
--------------------------------------------------------------------------------
/res/drawable-ldpi/icon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/johncarl81/androiddraw/f6973c2a0ce8d61ad2751734b628e3a79eff3f89/res/drawable-ldpi/icon.png
--------------------------------------------------------------------------------
/res/drawable-mdpi/icon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/johncarl81/androiddraw/f6973c2a0ce8d61ad2751734b628e3a79eff3f89/res/drawable-mdpi/icon.png
--------------------------------------------------------------------------------
/res/layout/cubic.xml:
--------------------------------------------------------------------------------
1 |
2 |
19 |
24 |
29 |
33 |
34 |
35 |
--------------------------------------------------------------------------------
/res/layout/quad.xml:
--------------------------------------------------------------------------------
1 |
2 |
19 |
24 |
29 |
33 |
34 |
35 |
--------------------------------------------------------------------------------
/res/layout/simple.xml:
--------------------------------------------------------------------------------
1 |
2 |
19 |
24 |
29 |
33 |
34 |
35 |
--------------------------------------------------------------------------------
/res/layout/square.xml:
--------------------------------------------------------------------------------
1 |
2 |
19 |
24 |
29 |
33 |
34 |
35 |
--------------------------------------------------------------------------------
/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 |
19 |
20 | androidDraw
21 |
22 |
--------------------------------------------------------------------------------
/src/main/java/org/example/androiddraw/ActivityListItem.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2012 John Ericksen
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.example.androiddraw;
17 |
18 | import android.content.Intent;
19 | import org.apache.commons.lang.builder.EqualsBuilder;
20 | import org.apache.commons.lang.builder.HashCodeBuilder;
21 |
22 | public class ActivityListItem implements Comparable {
23 |
24 | private Intent intent;
25 | private String name;
26 |
27 | public ActivityListItem(Intent intent, String name) {
28 | this.intent = intent;
29 | this.name = name;
30 | }
31 |
32 | public Intent getIntent() {
33 | return intent;
34 | }
35 |
36 | public String getName() {
37 | return name;
38 | }
39 |
40 | public String toString() {
41 | return getName();
42 | }
43 |
44 | @Override
45 | public int compareTo(ActivityListItem activityListItem) {
46 | return getName().compareTo(activityListItem.getName());
47 | }
48 |
49 | @Override
50 | public boolean equals(Object obj) {
51 | if (!(obj instanceof ActivityListItem)) {
52 | return false;
53 | }
54 | if (this == obj) {
55 | return true;
56 | }
57 | ActivityListItem rhs = (ActivityListItem) obj;
58 | return new EqualsBuilder()
59 | .append(name, rhs.name)
60 | .isEquals();
61 | }
62 |
63 | @Override
64 | public int hashCode() {
65 | return new HashCodeBuilder().append(name).hashCode();
66 | }
67 | }
--------------------------------------------------------------------------------
/src/main/java/org/example/androiddraw/CubicDraw.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2012 John Ericksen
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.example.androiddraw;
17 |
18 | import android.view.View;
19 | import org.androidtransfuse.annotations.Activity;
20 | import org.androidtransfuse.annotations.Layout;
21 | import org.androidtransfuse.annotations.RegisterListener;
22 |
23 | import javax.inject.Inject;
24 |
25 | @Activity(label = "@string/app_name")
26 | @Layout(R.layout.cubic)
27 | public class CubicDraw {
28 |
29 | private CubicDrawView drawView;
30 |
31 | @Inject
32 | @org.androidtransfuse.annotations.View(R.id.cubicdrawview)
33 | public CubicDraw(CubicDrawView drawView) {
34 | this.drawView = drawView;
35 | }
36 |
37 | @RegisterListener(R.id.clearbutton)
38 | public View.OnClickListener clearListener = new View.OnClickListener() {
39 | @Override
40 | public void onClick(View v) {
41 | drawView.clear();
42 | }
43 | };
44 |
45 | }
--------------------------------------------------------------------------------
/src/main/java/org/example/androiddraw/CubicDrawView.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2012 John Ericksen
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.example.androiddraw;
17 |
18 | import android.content.Context;
19 | import android.graphics.Canvas;
20 | import android.graphics.Color;
21 | import android.graphics.Paint;
22 | import android.graphics.Path;
23 | import android.util.AttributeSet;
24 | import android.view.MotionEvent;
25 | import android.view.View;
26 | import android.view.View.OnTouchListener;
27 |
28 | import java.util.ArrayList;
29 | import java.util.List;
30 |
31 | public class CubicDrawView extends View implements OnTouchListener {
32 |
33 | private static final float STROKE_WIDTH = 5f;
34 |
35 | List points = new ArrayList();
36 | Paint paint = new Paint();
37 |
38 | public CubicDrawView(Context context, AttributeSet attributeSet) {
39 | super(context, attributeSet);
40 | setFocusable(true);
41 | setFocusableInTouchMode(true);
42 |
43 | this.setOnTouchListener(this);
44 |
45 | paint = new Paint(Paint.ANTI_ALIAS_FLAG);
46 | paint.setStyle(Paint.Style.STROKE);
47 | paint.setStrokeWidth(STROKE_WIDTH);
48 | paint.setColor(Color.WHITE);
49 | }
50 |
51 | public void onDraw(Canvas canvas) {
52 | Path path = new Path();
53 |
54 | if(points.size() > 1){
55 | for(int i = points.size() - 2; i < points.size(); i++){
56 | if(i >= 0){
57 | Point point = points.get(i);
58 |
59 | if(i == 0){
60 | Point next = points.get(i + 1);
61 | point.dx = ((next.x - point.x) / 3);
62 | point.dy = ((next.y - point.y) / 3);
63 | }
64 | else if(i == points.size() - 1){
65 | Point prev = points.get(i - 1);
66 | point.dx = ((point.x - prev.x) / 3);
67 | point.dy = ((point.y - prev.y) / 3);
68 | }
69 | else{
70 | Point next = points.get(i + 1);
71 | Point prev = points.get(i - 1);
72 | point.dx = ((next.x - prev.x) / 3);
73 | point.dy = ((next.y - prev.y) / 3);
74 | }
75 | }
76 | }
77 | }
78 |
79 | boolean first = true;
80 | for(int i = 0; i < points.size(); i++){
81 | Point point = points.get(i);
82 | if(first){
83 | first = false;
84 | path.moveTo(point.x, point.y);
85 | }
86 | else{
87 | Point prev = points.get(i - 1);
88 | path.cubicTo(prev.x + prev.dx, prev.y + prev.dy, point.x - point.dx, point.y - point.dy, point.x, point.y);
89 | }
90 | }
91 |
92 | canvas.drawPath(path, paint);
93 | }
94 |
95 | public boolean onTouch(View view, MotionEvent event) {
96 | if (event.getAction() != MotionEvent.ACTION_UP) {
97 | for (int i = 0; i < event.getHistorySize(); i++) {
98 | Point point = new Point();
99 | point.x = event.getHistoricalX(i);
100 | point.y = event.getHistoricalY(i);
101 | points.add(point);
102 | }
103 | invalidate();
104 | return true;
105 | }
106 | return super.onTouchEvent(event);
107 | }
108 |
109 | public void clear() {
110 | points.clear();
111 | }
112 |
113 | class Point {
114 | float x, y;
115 | float dx, dy;
116 |
117 | @Override
118 | public String toString() {
119 | return x + ", " + y;
120 | }
121 | }
122 | }
123 |
124 |
--------------------------------------------------------------------------------
/src/main/java/org/example/androiddraw/QuadDraw.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2012 John Ericksen
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.example.androiddraw;
17 |
18 | import android.view.View;
19 | import org.androidtransfuse.annotations.Activity;
20 | import org.androidtransfuse.annotations.Layout;
21 | import org.androidtransfuse.annotations.RegisterListener;
22 |
23 | import javax.inject.Inject;
24 |
25 | @Activity(label = "@string/app_name")
26 | @Layout(R.layout.quad)
27 | public class QuadDraw {
28 |
29 | private QuadDrawView drawView;
30 |
31 | @Inject
32 | @org.androidtransfuse.annotations.View(R.id.quaddrawview)
33 | public QuadDraw(QuadDrawView drawView) {
34 | this.drawView = drawView;
35 | }
36 |
37 | @RegisterListener(R.id.clearbutton)
38 | public View.OnClickListener clearListener = new View.OnClickListener() {
39 | @Override
40 | public void onClick(View v) {
41 | drawView.clear();
42 | }
43 | };
44 |
45 | }
--------------------------------------------------------------------------------
/src/main/java/org/example/androiddraw/QuadDrawView.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2012 John Ericksen
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.example.androiddraw;
17 |
18 | import android.content.Context;
19 | import android.graphics.Canvas;
20 | import android.graphics.Color;
21 | import android.graphics.Paint;
22 | import android.graphics.Path;
23 | import android.util.AttributeSet;
24 | import android.view.MotionEvent;
25 | import android.view.View;
26 | import android.view.View.OnTouchListener;
27 |
28 | import java.util.ArrayList;
29 | import java.util.List;
30 |
31 | public class QuadDrawView extends View implements OnTouchListener {
32 |
33 | private static final float STROKE_WIDTH = 5f;
34 |
35 | List points = new ArrayList();
36 | Paint paint = new Paint();
37 |
38 | public QuadDrawView(Context context, AttributeSet attributeSet) {
39 | super(context, attributeSet);
40 | setFocusable(true);
41 | setFocusableInTouchMode(true);
42 |
43 | this.setOnTouchListener(this);
44 |
45 | paint = new Paint(Paint.ANTI_ALIAS_FLAG);
46 | paint.setStyle(Paint.Style.STROKE);
47 | paint.setStrokeWidth(STROKE_WIDTH);
48 | paint.setColor(Color.WHITE);
49 | }
50 |
51 | public void onDraw(Canvas canvas) {
52 | Path path = new Path();
53 | boolean first = true;
54 | for(int i = 0; i < points.size(); i += 2){
55 | Point point = points.get(i);
56 | if(first){
57 | first = false;
58 | path.moveTo(point.x, point.y);
59 | }
60 |
61 | else if(i < points.size() - 1){
62 | Point next = points.get(i + 1);
63 | path.quadTo(point.x, point.y, next.x, next.y);
64 | }
65 | else{
66 | path.lineTo(point.x, point.y);
67 | }
68 | }
69 |
70 | canvas.drawPath(path, paint);
71 | }
72 |
73 | public boolean onTouch(View view, MotionEvent event) {
74 | if (event.getAction() != MotionEvent.ACTION_UP) {
75 | for (int i = 0; i < event.getHistorySize(); i++) {
76 | Point point = new Point();
77 | point.x = event.getHistoricalX(i);
78 | point.y = event.getHistoricalY(i);
79 | points.add(point);
80 | }
81 | invalidate();
82 | return true;
83 | }
84 | return super.onTouchEvent(event);
85 | }
86 |
87 | public void clear() {
88 | points.clear();
89 | }
90 |
91 | class Point {
92 | float x, y;
93 | float dx, dy;
94 |
95 | @Override
96 | public String toString() {
97 | return x + ", " + y;
98 | }
99 | }
100 | }
101 |
102 |
--------------------------------------------------------------------------------
/src/main/java/org/example/androiddraw/Selection.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2012 John Ericksen
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.example.androiddraw;
17 |
18 | import android.app.ListActivity;
19 | import android.widget.ArrayAdapter;
20 | import org.androidtransfuse.annotations.*;
21 | import org.androidtransfuse.intentFactory.IntentFactory;
22 |
23 | import javax.inject.Inject;
24 | import java.util.ArrayList;
25 | import java.util.Arrays;
26 | import java.util.List;
27 |
28 | /**
29 | * @author John Ericksen
30 | */
31 | @Activity(label = "@string/app_name", type = ListActivity.class)
32 | @IntentFilter({
33 | @Intent(type = IntentType.ACTION, name = android.content.Intent.ACTION_MAIN),
34 | @Intent(type = IntentType.CATEGORY, name = android.content.Intent.CATEGORY_LAUNCHER)
35 | })
36 | public class Selection {
37 | private static final long THE_ANSWER = 42;
38 | private static final String TEST_VALUE = "test value";
39 |
40 | private List values;
41 | private ListActivity listActivity;
42 | private IntentFactory intentFactory;
43 |
44 | @Inject
45 | public Selection(ListActivity listActivity, IntentFactory intentFactory) {
46 |
47 | this.intentFactory = intentFactory;
48 | this.listActivity = listActivity;
49 |
50 | values = new ArrayList(Arrays.asList(new ActivityListItem[]{
51 | createLI(SimpleDrawActivity.class, "Simple Draw"),
52 | createLI(QuadDrawActivity.class, "Quad Draw"),
53 | createLI(CubicDrawActivity.class, "Cubic Splines Draw"),
54 | createLI(SquareSignatureDrawActivity.class, "Square Signature"),
55 | }));
56 |
57 | ArrayAdapter adapter = new ArrayAdapter(listActivity, android.R.layout.simple_list_item_1, values);
58 | listActivity.setListAdapter(adapter);
59 | }
60 |
61 | @OnListItemClick
62 | public void onListItemClick(int position) {
63 | listActivity.startActivity(values.get(position).getIntent());
64 | }
65 |
66 | private ActivityListItem createLI(Class> clazz, String name) {
67 | android.content.Intent intent = new android.content.Intent(listActivity, clazz);
68 | return new ActivityListItem(intent, name);
69 | }
70 | }
71 |
--------------------------------------------------------------------------------
/src/main/java/org/example/androiddraw/SimpleDraw.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2012 John Ericksen
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.example.androiddraw;
17 |
18 | import android.view.View;
19 | import org.androidtransfuse.annotations.Activity;
20 | import org.androidtransfuse.annotations.Layout;
21 | import org.androidtransfuse.annotations.RegisterListener;
22 |
23 | import javax.inject.Inject;
24 |
25 | @Activity(label = "@string/app_name")
26 | @Layout(R.layout.simple)
27 | public class SimpleDraw {
28 |
29 | private SimpleDrawView drawView;
30 |
31 | @Inject
32 | @org.androidtransfuse.annotations.View(R.id.simpledrawview)
33 | public SimpleDraw(SimpleDrawView drawView) {
34 | this.drawView = drawView;
35 | }
36 |
37 | @RegisterListener(R.id.clearbutton)
38 | public View.OnClickListener clearListener = new View.OnClickListener() {
39 | @Override
40 | public void onClick(View v) {
41 | drawView.clear();
42 | }
43 | };
44 |
45 | }
--------------------------------------------------------------------------------
/src/main/java/org/example/androiddraw/SimpleDrawView.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2012 John Ericksen
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.example.androiddraw;
17 |
18 | import android.content.Context;
19 | import android.graphics.Canvas;
20 | import android.graphics.Color;
21 | import android.graphics.Paint;
22 | import android.graphics.Path;
23 | import android.util.AttributeSet;
24 | import android.view.MotionEvent;
25 | import android.view.View;
26 | import android.view.View.OnTouchListener;
27 |
28 | import java.util.ArrayList;
29 | import java.util.List;
30 |
31 | public class SimpleDrawView extends View implements OnTouchListener {
32 |
33 | private static final float STROKE_WIDTH = 5f;
34 |
35 | List points = new ArrayList();
36 | Paint paint = new Paint();
37 |
38 | public SimpleDrawView(Context context, AttributeSet attributeSet) {
39 | super(context, attributeSet);
40 | setFocusable(true);
41 | setFocusableInTouchMode(true);
42 |
43 | this.setOnTouchListener(this);
44 |
45 | paint = new Paint(Paint.ANTI_ALIAS_FLAG);
46 | paint.setStyle(Paint.Style.STROKE);
47 | paint.setStrokeWidth(STROKE_WIDTH);
48 | paint.setColor(Color.WHITE);
49 | }
50 |
51 | @Override
52 | public void onDraw(Canvas canvas) {
53 | Path path = new Path();
54 | boolean first = true;
55 | for (Point point : points) {
56 | if (first) {
57 | first = false;
58 | path.moveTo(point.x, point.y);
59 | } else {
60 | path.lineTo(point.x, point.y);
61 | }
62 | }
63 |
64 | canvas.drawPath(path, paint);
65 | }
66 |
67 | public boolean onTouch(View view, MotionEvent event) {
68 | if (event.getAction() != MotionEvent.ACTION_UP) {
69 | for (int i = 0; i < event.getHistorySize(); i++) {
70 | Point point = new Point();
71 | point.x = event.getHistoricalX(i);
72 | point.y = event.getHistoricalY(i);
73 | points.add(point);
74 | }
75 | invalidate();
76 | return true;
77 | }
78 | return super.onTouchEvent(event);
79 | }
80 |
81 | public void clear() {
82 | points.clear();
83 | }
84 |
85 | class Point {
86 | float x, y;
87 | float dx, dy;
88 |
89 | @Override
90 | public String toString() {
91 | return x + ", " + y;
92 | }
93 | }
94 | }
95 |
96 |
--------------------------------------------------------------------------------
/src/main/java/org/example/androiddraw/SquareSignatureDraw.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2012 John Ericksen
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.example.androiddraw;
17 |
18 | import android.view.View;
19 | import org.androidtransfuse.annotations.Activity;
20 | import org.androidtransfuse.annotations.Layout;
21 | import org.androidtransfuse.annotations.RegisterListener;
22 |
23 | import javax.inject.Inject;
24 |
25 | @Activity(label = "@string/app_name")
26 | @Layout(R.layout.square)
27 | public class SquareSignatureDraw {
28 |
29 | private SquareSignatureView drawView;
30 |
31 | @Inject
32 | @org.androidtransfuse.annotations.View(R.id.squareview)
33 | public SquareSignatureDraw(SquareSignatureView drawView) {
34 | this.drawView = drawView;
35 | }
36 |
37 | @RegisterListener(R.id.clearbutton)
38 | public View.OnClickListener clearListener = new View.OnClickListener() {
39 | @Override
40 | public void onClick(View v) {
41 | drawView.clear();
42 | }
43 | };
44 |
45 | }
--------------------------------------------------------------------------------
/src/main/java/org/example/androiddraw/SquareSignatureView.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2012 John Ericksen
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.example.androiddraw;
17 |
18 | import android.content.Context;
19 | import android.graphics.*;
20 | import android.util.AttributeSet;
21 | import android.util.Log;
22 | import android.view.MotionEvent;
23 | import android.view.View;
24 |
25 | public class SquareSignatureView extends View {
26 |
27 | private static final float STROKE_WIDTH = 5f;
28 |
29 | /**
30 | * Need to track this so the dirty region can accommodate the stroke. *
31 | */
32 | private static final float HALF_STROKE_WIDTH = STROKE_WIDTH / 2;
33 |
34 | private Paint paint = new Paint();
35 | private Path path = new Path();
36 |
37 | /**
38 | * Optimizes painting by invalidating the smallest possible area.
39 | */
40 | private float lastTouchX;
41 | private float lastTouchY;
42 | private final RectF dirtyRect = new RectF();
43 |
44 | public SquareSignatureView(Context context, AttributeSet attrs) {
45 | super(context, attrs);
46 |
47 | paint.setAntiAlias(true);
48 | paint.setColor(Color.BLUE);
49 | paint.setStyle(Paint.Style.STROKE);
50 | paint.setStrokeJoin(Paint.Join.ROUND);
51 | paint.setStrokeWidth(STROKE_WIDTH);
52 | }
53 |
54 | /**
55 | * Erases the signature.
56 | */
57 | public void clear() {
58 | path.reset();
59 |
60 | // Repaints the entire view.
61 | invalidate();
62 | }
63 |
64 | @Override
65 | protected void onDraw(Canvas canvas) {
66 | canvas.drawPath(path, paint);
67 | }
68 |
69 | @Override
70 | public boolean onTouchEvent(MotionEvent event) {
71 | float eventX = event.getX();
72 | float eventY = event.getY();
73 |
74 | switch (event.getAction()) {
75 | case MotionEvent.ACTION_DOWN:
76 | path.moveTo(eventX, eventY);
77 | lastTouchX = eventX;
78 | lastTouchY = eventY;
79 | // There is no end point yet, so don't waste cycles invalidating.
80 | return true;
81 |
82 | case MotionEvent.ACTION_MOVE:
83 | case MotionEvent.ACTION_UP:
84 | // Start tracking the dirty region.
85 | resetDirtyRect(eventX, eventY);
86 |
87 | // When the hardware tracks events faster than they are delivered, the
88 | // event will contain a history of those skipped points.
89 | int historySize = event.getHistorySize();
90 | for (int i = 0; i < historySize; i++) {
91 | float historicalX = event.getHistoricalX(i);
92 | float historicalY = event.getHistoricalY(i);
93 | expandDirtyRect(historicalX, historicalY);
94 | path.lineTo(historicalX, historicalY);
95 | }
96 |
97 | // After replaying history, connect the line to the touch point.
98 | path.lineTo(eventX, eventY);
99 | break;
100 |
101 | default:
102 | Log.d("default", "Ignored touch event: " + event.toString());
103 | return false;
104 | }
105 |
106 | // Include half the stroke width to avoid clipping.
107 | invalidate(
108 | (int) (dirtyRect.left - HALF_STROKE_WIDTH),
109 | (int) (dirtyRect.top - HALF_STROKE_WIDTH),
110 | (int) (dirtyRect.right + HALF_STROKE_WIDTH),
111 | (int) (dirtyRect.bottom + HALF_STROKE_WIDTH));
112 |
113 | lastTouchX = eventX;
114 | lastTouchY = eventY;
115 |
116 | return true;
117 | }
118 |
119 | /**
120 | * Called when replaying history to ensure the dirty region includes all
121 | * points.
122 | */
123 | private void expandDirtyRect(float historicalX, float historicalY) {
124 | if (historicalX < dirtyRect.left) {
125 | dirtyRect.left = historicalX;
126 | } else if (historicalX > dirtyRect.right) {
127 | dirtyRect.right = historicalX;
128 | }
129 | if (historicalY < dirtyRect.top) {
130 | dirtyRect.top = historicalY;
131 | } else if (historicalY > dirtyRect.bottom) {
132 | dirtyRect.bottom = historicalY;
133 | }
134 | }
135 |
136 | /**
137 | * Resets the dirty region when the motion event occurs.
138 | */
139 | private void resetDirtyRect(float eventX, float eventY) {
140 |
141 | // The lastTouchX and lastTouchY were set when the ACTION_DOWN
142 | // motion event occurred.
143 | dirtyRect.left = Math.min(lastTouchX, eventX);
144 | dirtyRect.right = Math.max(lastTouchX, eventX);
145 | dirtyRect.top = Math.min(lastTouchY, eventY);
146 | dirtyRect.bottom = Math.max(lastTouchY, eventY);
147 | }
148 | }
--------------------------------------------------------------------------------