├── README.md
├── cardview.gif
├── assets
├── VeraMoBI.ttf
├── VeraMoBd.ttf
└── liberation-sans.ttf
├── libs
└── nineoldandroids-2.4.0.jar
├── res
├── drawable-hdpi
│ └── ic_launcher.png
├── drawable-mdpi
│ └── ic_launcher.png
├── drawable-xhdpi
│ ├── card_pattern.jpg
│ └── ic_launcher.png
└── values
│ └── attrs.xml
├── src
└── br
│ └── com
│ └── zynger
│ └── cardview
│ ├── CardBackFaceView.java
│ ├── CardFrontFaceView.java
│ ├── MissingTypefaceException.java
│ ├── TypefaceUtil.java
│ ├── CardFaceView.java
│ └── CardView.java
├── .settings
└── org.eclipse.jdt.core.prefs
├── .gitignore
├── AndroidManifest.xml
├── .classpath
├── project.properties
├── proguard-project.txt
├── .project
└── LICENSE
/README.md:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/julioz/CardView/HEAD/README.md
--------------------------------------------------------------------------------
/cardview.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/julioz/CardView/HEAD/cardview.gif
--------------------------------------------------------------------------------
/assets/VeraMoBI.ttf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/julioz/CardView/HEAD/assets/VeraMoBI.ttf
--------------------------------------------------------------------------------
/assets/VeraMoBd.ttf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/julioz/CardView/HEAD/assets/VeraMoBd.ttf
--------------------------------------------------------------------------------
/assets/liberation-sans.ttf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/julioz/CardView/HEAD/assets/liberation-sans.ttf
--------------------------------------------------------------------------------
/libs/nineoldandroids-2.4.0.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/julioz/CardView/HEAD/libs/nineoldandroids-2.4.0.jar
--------------------------------------------------------------------------------
/res/drawable-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/julioz/CardView/HEAD/res/drawable-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/res/drawable-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/julioz/CardView/HEAD/res/drawable-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/res/drawable-xhdpi/card_pattern.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/julioz/CardView/HEAD/res/drawable-xhdpi/card_pattern.jpg
--------------------------------------------------------------------------------
/res/drawable-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/julioz/CardView/HEAD/res/drawable-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/src/br/com/zynger/cardview/CardBackFaceView.java:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/julioz/CardView/HEAD/src/br/com/zynger/cardview/CardBackFaceView.java
--------------------------------------------------------------------------------
/src/br/com/zynger/cardview/CardFrontFaceView.java:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/julioz/CardView/HEAD/src/br/com/zynger/cardview/CardFrontFaceView.java
--------------------------------------------------------------------------------
/.settings/org.eclipse.jdt.core.prefs:
--------------------------------------------------------------------------------
1 | eclipse.preferences.version=1
2 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
3 | org.eclipse.jdt.core.compiler.compliance=1.6
4 | org.eclipse.jdt.core.compiler.source=1.6
5 |
--------------------------------------------------------------------------------
/src/br/com/zynger/cardview/MissingTypefaceException.java:
--------------------------------------------------------------------------------
1 | package br.com.zynger.cardview;
2 |
3 | public class MissingTypefaceException extends RuntimeException {
4 | private static final long serialVersionUID = 1L;
5 |
6 | public MissingTypefaceException(String message) {
7 | super(message);
8 | }
9 |
10 | }
11 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Built application files
2 | *.apk
3 | *.ap_
4 |
5 | # Files for the Dalvik VM
6 | *.dex
7 |
8 | # Java class files
9 | *.class
10 |
11 | # Generated files
12 | bin/
13 | gen/
14 |
15 | # Gradle files
16 | .gradle/
17 | build/
18 |
19 | # Local configuration file (sdk path, etc)
20 | local.properties
21 |
22 | # Proguard folder generated by Eclipse
23 | proguard/
24 |
--------------------------------------------------------------------------------
/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
10 |
11 |
14 |
15 |
16 |
--------------------------------------------------------------------------------
/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/project.properties:
--------------------------------------------------------------------------------
1 | # This file is automatically generated by Android Tools.
2 | # Do not modify this file -- YOUR CHANGES WILL BE ERASED!
3 | #
4 | # This file must be checked in Version Control Systems.
5 | #
6 | # To customize properties used by the Ant build system edit
7 | # "ant.properties", and override values to adapt the script to your
8 | # project structure.
9 | #
10 | # To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home):
11 | #proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
12 |
13 | # Project target.
14 | target=android-19
15 | android.library=true
16 |
--------------------------------------------------------------------------------
/res/values/attrs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/proguard-project.txt:
--------------------------------------------------------------------------------
1 | # To enable ProGuard in your project, edit project.properties
2 | # to define the proguard.config property as described in that file.
3 | #
4 | # Add project specific ProGuard rules here.
5 | # By default, the flags in this file are appended to flags specified
6 | # in ${sdk.dir}/tools/proguard/proguard-android.txt
7 | # You can edit the include path and order by changing the ProGuard
8 | # include property in project.properties.
9 | #
10 | # For more details, see
11 | # http://developer.android.com/guide/developing/tools/proguard.html
12 |
13 | # Add any project specific keep options here:
14 |
15 | # If your project uses WebView with JS, uncomment the following
16 | # and specify the fully qualified class name to the JavaScript interface
17 | # class:
18 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
19 | # public *;
20 | #}
21 |
--------------------------------------------------------------------------------
/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | cardview
4 |
5 |
6 |
7 |
8 |
9 | com.android.ide.eclipse.adt.ResourceManagerBuilder
10 |
11 |
12 |
13 |
14 | com.android.ide.eclipse.adt.PreCompilerBuilder
15 |
16 |
17 |
18 |
19 | org.eclipse.jdt.core.javabuilder
20 |
21 |
22 |
23 |
24 | com.android.ide.eclipse.adt.ApkBuilder
25 |
26 |
27 |
28 |
29 |
30 | com.android.ide.eclipse.adt.AndroidNature
31 | org.eclipse.jdt.core.javanature
32 |
33 |
34 |
--------------------------------------------------------------------------------
/src/br/com/zynger/cardview/TypefaceUtil.java:
--------------------------------------------------------------------------------
1 | package br.com.zynger.cardview;
2 |
3 | import android.content.Context;
4 | import android.graphics.Typeface;
5 |
6 | public class TypefaceUtil {
7 | private static TypefaceUtil instance;
8 | private Typeface mVeraMonoBold;
9 | private Typeface mVeraMonoBoldItalic;
10 | private Typeface mLiberationSans;
11 |
12 | public TypefaceUtil(Context context) {
13 | mVeraMonoBold = loadTypeface(context, "VeraMoBd.ttf");
14 | mVeraMonoBoldItalic = loadTypeface(context, "VeraMoBI.ttf");
15 | mLiberationSans = loadTypeface(context, "liberation-sans.ttf");
16 | }
17 |
18 | private Typeface loadTypeface(Context context, String name) {
19 | try {
20 | return Typeface.createFromAsset(context.getResources().getAssets(), name);
21 | } catch (RuntimeException e) {
22 | throw new MissingTypefaceException("Typeface " + name + " not found in your assets folder. Did you forget to add it?");
23 | }
24 | }
25 |
26 | public static TypefaceUtil getInstance(Context context) {
27 | if (instance == null) {
28 | instance = new TypefaceUtil(context);
29 | }
30 | return instance;
31 | }
32 |
33 | public Typeface getVeraMonoBold() {
34 | return mVeraMonoBold;
35 | }
36 |
37 | public Typeface getVeraMonoBoldItalic() {
38 | return mVeraMonoBoldItalic;
39 | }
40 |
41 | public Typeface getLiberationSans() {
42 | return mLiberationSans;
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/src/br/com/zynger/cardview/CardFaceView.java:
--------------------------------------------------------------------------------
1 | package br.com.zynger.cardview;
2 |
3 | import android.content.Context;
4 | import android.graphics.Canvas;
5 | import android.graphics.Color;
6 | import android.graphics.LinearGradient;
7 | import android.graphics.Paint;
8 | import android.graphics.Paint.Align;
9 | import android.graphics.RectF;
10 | import android.graphics.Shader;
11 | import android.view.View;
12 |
13 | import com.nineoldandroids.animation.ArgbEvaluator;
14 | import com.nineoldandroids.animation.ObjectAnimator;
15 | import com.nineoldandroids.animation.ValueAnimator;
16 | import com.nineoldandroids.animation.ValueAnimator.AnimatorUpdateListener;
17 |
18 | public abstract class CardFaceView extends View {
19 | public enum CardFlag {
20 | VISA("VISA", 0xFF191278, "4.*"), MASTERCARD("MasterCard", 0xFF0061A8, "5[1-5].*"), DISCOVER(
21 | "Discover", 0xFF86B8CF, "(6011|644|65).*"), AMEX("American Express", 0xFF108168, "(34|37).*");
22 |
23 | private String mText;
24 | private String mRegex;
25 | private int mColor;
26 | private CardFlag(String text, int color, String regex) {
27 | mText = text;
28 | mColor = color;
29 | mRegex = regex;
30 | }
31 |
32 | public int getColor() {
33 | return mColor;
34 | }
35 |
36 | public String getText() {
37 | return mText;
38 | }
39 |
40 | public String getRegex() {
41 | return mRegex;
42 | }
43 | }
44 |
45 | protected static final int CARD_BACKGROUND_COLOR_NOFLAG = 0xFFCCCCCC;
46 |
47 | protected static final int CARD_WIDTH = Math.round(525 * 1.0f); //TODO extract to variables
48 | protected static final int CARD_HEIGHT = Math.round(300 * 1.0f);
49 |
50 | protected static final int CARD_BORDER_RADIUS = 10;
51 | protected static final int CHIP_BORDER_RADIUS = 5;
52 |
53 | protected static final int CARD_CONTENT_LEFT = Math
54 | .round((float) CARD_WIDTH / 17.5f);
55 | protected static final int CARD_CONTENT_RIGHT = CARD_WIDTH
56 | - Math.round((float) CARD_WIDTH / 17.5f);
57 | protected static final int CARD_CONTENT_TOP = Math
58 | .round((float) CARD_HEIGHT / 15.0f);
59 |
60 | protected static final float CARD_TEXT_SIZE_MULTIPLIER = CARD_WIDTH / 300.0f;
61 |
62 | private CardFlag mFlag;
63 |
64 | private Paint mCardPaint;
65 | private RectF mCardRect;
66 | private Paint mChipBasePaint;
67 | private Paint mChipOverPaint;
68 | private Paint mCardBackgroundGradientPaint;
69 |
70 | public CardFaceView(Context context) {
71 | super(context);
72 |
73 | mCardPaint = new Paint();
74 | mCardPaint.setColor(0xFFDDDDDD);
75 |
76 | mCardBackgroundGradientPaint = new Paint();
77 | int[] colors = {Color.TRANSPARENT, Color.WHITE, Color.TRANSPARENT, Color.WHITE, Color.TRANSPARENT};
78 | float[] positions = {0, 0.3f, 0.5f, 0.7f, 1.0f};
79 | mCardBackgroundGradientPaint.setAlpha(Math.round(0.35f * 255));
80 | mCardBackgroundGradientPaint.setShader(new LinearGradient(0, 0, CARD_WIDTH, CARD_HEIGHT, colors, positions, Shader.TileMode.REPEAT));
81 |
82 | mChipBasePaint = new Paint();
83 | mChipBasePaint.setColor(CARD_BACKGROUND_COLOR_NOFLAG);
84 |
85 | mChipOverPaint = new Paint();
86 | mChipOverPaint.setColor(0xFFD9D9D9);
87 |
88 | mCardRect = new RectF(0, 0, CARD_WIDTH, CARD_HEIGHT);
89 | }
90 |
91 | @Override
92 | protected void onDraw(Canvas canvas) {
93 | super.onDraw(canvas);
94 |
95 | canvas.drawRoundRect(mCardRect, CARD_BORDER_RADIUS, CARD_BORDER_RADIUS,
96 | mCardPaint);
97 | canvas.drawRoundRect(mCardRect, CARD_BORDER_RADIUS, CARD_BORDER_RADIUS, mCardBackgroundGradientPaint);
98 | }
99 |
100 | protected void drawChip(Canvas canvas, RectF baseChipRect, RectF overChipRect) {
101 | canvas.drawRoundRect(baseChipRect, CHIP_BORDER_RADIUS,
102 | CHIP_BORDER_RADIUS, mChipBasePaint);
103 | canvas.drawRoundRect(overChipRect, CHIP_BORDER_RADIUS,
104 | CHIP_BORDER_RADIUS, mChipOverPaint);
105 | }
106 |
107 | protected void drawTextInRectF(String text, float offsetX, float offsetY, Canvas canvas, RectF r, Paint textPaint) {
108 | textPaint.setTextAlign(Align.CENTER);
109 | float width = r.width();
110 |
111 | int numOfChars = textPaint.breakText(text, true, width, null);
112 | int start = (text.length() - numOfChars) / 2;
113 | canvas.drawText(text, start, start + numOfChars, r.centerX() + offsetX,
114 | r.centerY() + offsetY, textPaint);
115 | }
116 |
117 | private void onFlagChanged(final CardFlag oldFlag, final CardFlag newFlag) {
118 | int flagColor = CARD_BACKGROUND_COLOR_NOFLAG;
119 | if (newFlag != null) {
120 | flagColor = newFlag.getColor();
121 | }
122 |
123 | ValueAnimator fade = ObjectAnimator.ofInt(mCardPaint,
124 | "color", mCardPaint.getColor(), flagColor);
125 | fade.setDuration(500);
126 | fade.setEvaluator(new ArgbEvaluator());
127 | fade.addUpdateListener(new AnimatorUpdateListener() {
128 | @Override
129 | public void onAnimationUpdate(ValueAnimator value) {
130 | onFlagChangedUpdate(oldFlag, newFlag, value);
131 | invalidate();
132 | }
133 | });
134 |
135 | fade.start();
136 | }
137 |
138 | protected abstract void onFlagChangedUpdate(CardFlag oldFlag, CardFlag newFlag, ValueAnimator value);
139 |
140 | protected void setPaintsAlpha(Paint[] paints, int alpha) {
141 | for (Paint paint : paints) {
142 | paint.setAlpha(alpha);
143 | }
144 | }
145 |
146 | public void setFlag(CardFlag mFlag) {
147 | CardFlag oldFlag = this.mFlag;
148 | this.mFlag = mFlag;
149 |
150 | onFlagChanged(oldFlag, mFlag);
151 | }
152 |
153 | public CardFlag getFlag() {
154 | return mFlag;
155 | }
156 |
157 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
158 | super.onMeasure(
159 | MeasureSpec.makeMeasureSpec(Math.round(CARD_WIDTH), MeasureSpec.EXACTLY),
160 | MeasureSpec.makeMeasureSpec(Math.round(CARD_HEIGHT), MeasureSpec.EXACTLY));
161 | }
162 | }
163 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "{}"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright {yyyy} {name of copyright owner}
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
--------------------------------------------------------------------------------
/src/br/com/zynger/cardview/CardView.java:
--------------------------------------------------------------------------------
1 | package br.com.zynger.cardview;
2 |
3 | import android.content.Context;
4 | import android.content.res.TypedArray;
5 | import android.text.Editable;
6 | import android.text.InputFilter;
7 | import android.text.InputType;
8 | import android.text.TextWatcher;
9 | import android.util.AttributeSet;
10 | import android.view.View;
11 | import android.view.animation.AccelerateInterpolator;
12 | import android.view.animation.DecelerateInterpolator;
13 | import android.widget.EditText;
14 | import android.widget.RelativeLayout;
15 | import br.com.zynger.cardview.CardFaceView.CardFlag;
16 |
17 | import com.nineoldandroids.animation.Animator;
18 | import com.nineoldandroids.animation.Animator.AnimatorListener;
19 | import com.nineoldandroids.animation.ObjectAnimator;
20 |
21 | public class CardView extends RelativeLayout {
22 |
23 | private final static int ANIMATION_DURATION = 400;
24 | private static final int MAX_CARD_NUMBER_LENGTH = 16;
25 | private static final int MAX_CARD_DATE_LENGTH = 6;
26 | private static final int MAX_CARD_CVV_LENGTH = 4;
27 |
28 | private boolean isShowingFront = true;
29 | private boolean isAnimating = false;
30 |
31 | private CardFrontFaceView mCardFrontView;
32 | private CardBackFaceView mCardBackView;
33 |
34 | private ObjectAnimator mFrontOutAnim;
35 | private ObjectAnimator mFrontInAnim;
36 | private ObjectAnimator mBackOutAnim;
37 | private ObjectAnimator mBackInAnim;
38 |
39 | public CardView(Context context) {
40 | this(context, null, 0);
41 | }
42 |
43 | public CardView(Context context, AttributeSet attrs) {
44 | this(context, attrs, 0);
45 | }
46 |
47 | public CardView(Context context, AttributeSet attrs, int defStyle) {
48 | super(context, attrs, defStyle);
49 | init(context, attrs);
50 | }
51 |
52 | private void init(Context context, AttributeSet attrs) {
53 | mCardFrontView = new CardFrontFaceView(context);
54 | mCardBackView = new CardBackFaceView(context);
55 |
56 | mCardBackView.setVisibility(View.INVISIBLE);
57 | addView(mCardBackView);
58 | addView(mCardFrontView);
59 |
60 | initAnimations();
61 |
62 | initFromXML(attrs);
63 | }
64 |
65 | private void initFromXML(AttributeSet attrs) {
66 | TypedArray array = getContext().obtainStyledAttributes(attrs,
67 | R.styleable.CardView);
68 |
69 | String cardNumber = array.getString(R.styleable.CardView_cardNumber);
70 | if (cardNumber != null) {
71 | String maskedNumber = getMaskedNumber(cardNumber);
72 | setCardNumber(maskedNumber);
73 | }
74 |
75 | String cardholder = array.getString(R.styleable.CardView_cardholderName);
76 | if (cardholder != null) {
77 | setCardName(cardholder);
78 | }
79 |
80 | String validThru = array.getString(R.styleable.CardView_cardValidThru);
81 | if (validThru != null) {
82 | int[] date = getMaskedDate(validThru);
83 | setCardValidThru(date[0], date[1]);
84 | }
85 |
86 | Integer cvv = array.getInteger(R.styleable.CardView_cardholderCvv, -1);
87 | if (cvv != -1) {
88 | setCardCvv(cvv);
89 | }
90 |
91 | String validThruOneText = array.getString(R.styleable.CardView_cardValidThruWordOneText);
92 | String validThruTwoText = array.getString(R.styleable.CardView_cardValidThruWordTwoText);
93 | if (validThruOneText != null && validThruTwoText != null) {
94 | setValidThruText(validThruOneText, validThruTwoText);
95 | }
96 |
97 | String monthYearText = array.getString(R.styleable.CardView_cardMonthYearText);
98 | if (monthYearText != null) {
99 | setMonthYearText(monthYearText);
100 | }
101 |
102 | String informationText = array.getString(R.styleable.CardView_cardInformationText);
103 | if (informationText != null) {
104 | setInformationText(informationText);
105 | }
106 |
107 | array.recycle();
108 | }
109 |
110 | private void initAnimations() {
111 | mFrontOutAnim = ObjectAnimator.ofFloat(mCardFrontView, "rotationY", 0f, -90f);
112 | mFrontOutAnim.setDuration(ANIMATION_DURATION);
113 | mFrontOutAnim.setInterpolator(new AccelerateInterpolator());
114 |
115 | mFrontInAnim = ObjectAnimator.ofFloat(mCardFrontView, "rotationY", -90f, 0f);
116 | mFrontInAnim.setDuration(ANIMATION_DURATION);
117 | mFrontInAnim.setInterpolator(new DecelerateInterpolator());
118 |
119 | mBackOutAnim = ObjectAnimator.ofFloat(mCardBackView, "rotationY", 0f, 90f);
120 | mBackOutAnim.setDuration(ANIMATION_DURATION);
121 | mBackOutAnim.setInterpolator(new AccelerateInterpolator());
122 |
123 | mBackInAnim = ObjectAnimator.ofFloat(mCardBackView, "rotationY", 90f, 0f);
124 | mBackInAnim.setDuration(ANIMATION_DURATION);
125 | mBackInAnim.setInterpolator(new DecelerateInterpolator());
126 |
127 | mFrontOutAnim.addListener(new AnimatorListener() {
128 | @Override
129 | public void onAnimationStart(Animator arg0) {
130 | isAnimating = true;
131 | }
132 |
133 | @Override
134 | public void onAnimationRepeat(Animator arg0) {
135 | }
136 |
137 | @Override
138 | public void onAnimationEnd(Animator arg0) {
139 | mCardFrontView.setVisibility(View.INVISIBLE);
140 | mCardBackView.setVisibility(View.VISIBLE);
141 | mBackInAnim.start();
142 | }
143 |
144 | @Override
145 | public void onAnimationCancel(Animator arg0) {
146 | }
147 | });
148 |
149 | mFrontInAnim.addListener(new AnimatorListener() {
150 | @Override
151 | public void onAnimationStart(Animator arg0) {
152 | }
153 |
154 | @Override
155 | public void onAnimationRepeat(Animator arg0) {
156 | }
157 |
158 | @Override
159 | public void onAnimationEnd(Animator arg0) {
160 | isShowingFront = true;
161 | isAnimating = false;
162 | }
163 |
164 | @Override
165 | public void onAnimationCancel(Animator arg0) {
166 | }
167 | });
168 |
169 | mBackOutAnim.addListener(new AnimatorListener() {
170 | @Override
171 | public void onAnimationStart(Animator arg0) {
172 | isAnimating = true;
173 | }
174 |
175 | @Override
176 | public void onAnimationRepeat(Animator arg0) {
177 | }
178 |
179 | @Override
180 | public void onAnimationEnd(Animator arg0) {
181 | mCardFrontView.setVisibility(View.VISIBLE);
182 | mCardBackView.setVisibility(View.INVISIBLE);
183 | mFrontInAnim.start();
184 | }
185 |
186 | @Override
187 | public void onAnimationCancel(Animator arg0) {
188 | }
189 | });
190 |
191 | mBackInAnim.addListener(new AnimatorListener() {
192 | @Override
193 | public void onAnimationStart(Animator arg0) {
194 | }
195 |
196 | @Override
197 | public void onAnimationRepeat(Animator arg0) {
198 | }
199 |
200 | @Override
201 | public void onAnimationEnd(Animator arg0) {
202 | isShowingFront = false;
203 | isAnimating = false;
204 | }
205 |
206 | @Override
207 | public void onAnimationCancel(Animator arg0) {
208 | }
209 | });
210 | }
211 |
212 | public void setCardCvv(Integer cardCvv) {
213 | mCardBackView.setCardCvv(cardCvv);
214 | }
215 |
216 | public void setCardNumber(String cardNumber) {
217 | mCardFrontView.setCardNumber(cardNumber);
218 |
219 | CardFlag matchFlag = null;
220 | for (CardFlag flag : CardFlag.values()) {
221 | if (cardNumber.matches(flag.getRegex())) {
222 | matchFlag = flag;
223 | break;
224 | }
225 | }
226 |
227 | setFlag(matchFlag);
228 | }
229 |
230 | public void setCardName(String cardName) {
231 | mCardFrontView.setCardName(cardName);
232 | }
233 |
234 | public void setCardValidThru(int month, int year) {
235 | mCardFrontView.setCardValidThru(month, year);
236 | }
237 |
238 | public void setValidThruText(String valid, String thru) {
239 | mCardFrontView.setValidThruText(valid, thru);
240 | }
241 |
242 | public void setMonthYearText(String text) {
243 | mCardFrontView.setMonthYearText(text);
244 | }
245 |
246 | public void setInformationText(String text) {
247 | mCardBackView.setInformationText(text);
248 | }
249 |
250 | public void setFlag(CardFlag flag) {
251 | if (mCardFrontView.getFlag() != flag) {
252 | mCardFrontView.setFlag(flag);
253 | mCardBackView.setFlag(flag);
254 | }
255 | }
256 |
257 | public boolean isShowingFront() {
258 | return isShowingFront;
259 | }
260 |
261 | public void toggleCardFace() {
262 | if (isAnimating) return;
263 |
264 | if (isShowingFront) {
265 | mFrontOutAnim.start();
266 | } else {
267 | mBackOutAnim.start();
268 | }
269 | }
270 |
271 | public void setNameTextInput(EditText etName) {
272 | etName.addTextChangedListener(new TextWatcher() {
273 | @Override
274 | public void onTextChanged(CharSequence s, int start, int before, int count) {
275 | }
276 |
277 | @Override
278 | public void beforeTextChanged(CharSequence s, int start, int count,
279 | int after) {
280 | }
281 |
282 | @Override
283 | public void afterTextChanged(Editable s) {
284 | setCardName(s.toString());
285 | }
286 | });
287 | }
288 |
289 | public void setNumberTextInput(EditText etNumber) {
290 | etNumber.setInputType(InputType.TYPE_CLASS_NUMBER);
291 | InputFilter[] filterArray = new InputFilter[1];
292 | filterArray[0] = new InputFilter.LengthFilter(MAX_CARD_NUMBER_LENGTH);
293 | etNumber.setFilters(filterArray);
294 | etNumber.addTextChangedListener(new TextWatcher() {
295 | @Override
296 | public void onTextChanged(CharSequence s, int start, int before, int count) {
297 | }
298 |
299 | @Override
300 | public void beforeTextChanged(CharSequence s, int start, int count,
301 | int after) {
302 | }
303 |
304 | @Override
305 | public void afterTextChanged(Editable s) {
306 | String input = s.toString();
307 | String cardNumber = getMaskedNumber(input);
308 | setCardNumber(cardNumber);
309 | }
310 | });
311 | }
312 |
313 | public void setExpirationTextInput(EditText etDate) {
314 | etDate.setInputType(InputType.TYPE_CLASS_NUMBER);
315 | InputFilter[] filterArray = new InputFilter[1];
316 | filterArray[0] = new InputFilter.LengthFilter(MAX_CARD_DATE_LENGTH);
317 | etDate.setFilters(filterArray);
318 | etDate.addTextChangedListener(new TextWatcher() {
319 | @Override
320 | public void onTextChanged(CharSequence s, int start, int before, int count) {
321 | }
322 |
323 | @Override
324 | public void beforeTextChanged(CharSequence s, int start, int count,
325 | int after) {
326 | }
327 |
328 | @Override
329 | public void afterTextChanged(Editable s) {
330 | String input = s.toString();
331 | int[] date = getMaskedDate(input);
332 | setCardValidThru(date[0], date[1]);
333 | }
334 | });
335 | }
336 |
337 | public void setCvvTextInput(EditText etCvv) {
338 | etCvv.setInputType(InputType.TYPE_CLASS_NUMBER);
339 | InputFilter[] filterArray = new InputFilter[1];
340 | filterArray[0] = new InputFilter.LengthFilter(MAX_CARD_CVV_LENGTH);
341 | etCvv.setFilters(filterArray);
342 |
343 | etCvv.setOnFocusChangeListener(new OnFocusChangeListener() {
344 | @Override
345 | public void onFocusChange(View v, boolean hasFocus) {
346 | toggleCardFace();
347 | }
348 | });
349 |
350 | etCvv.addTextChangedListener(new TextWatcher() {
351 | @Override
352 | public void onTextChanged(CharSequence s, int start, int before, int count) {
353 | }
354 |
355 | @Override
356 | public void beforeTextChanged(CharSequence s, int start, int count,
357 | int after) {
358 | }
359 |
360 | @Override
361 | public void afterTextChanged(Editable s) {
362 | String input = s.toString();
363 | Integer cardCvv = null;
364 | if (input.length() > 0) {
365 | cardCvv = Integer.valueOf(input);
366 | }
367 | setCardCvv(cardCvv);
368 | }
369 | });
370 | }
371 |
372 | private int[] getMaskedDate(String input) {
373 | input = input.replaceAll("/", "");
374 |
375 | int[] date = new int[2];
376 | date[0] = -1;
377 | date[1] = -1;
378 |
379 | if (input.length() > 1) {
380 | date[0] = Integer.valueOf(input.substring(0, 2));
381 | if (date[0] < 1 || date[0] > 12) {
382 | date[0] = -1;
383 | }
384 | }
385 |
386 | if (input.length() > 2) {
387 | date[1] = Integer.valueOf(input.substring(2, input.length()));
388 | }
389 |
390 | return date;
391 | }
392 |
393 | private String getMaskedNumber(String input) {
394 | input = input.replaceAll(" ", "");
395 |
396 | StringBuilder out = new StringBuilder();
397 |
398 | for (int index = 0; index < input.length(); index++) {
399 | out.append(input.charAt(index));
400 | if ((index % 4) == 3) {
401 | out.append(' ');
402 | }
403 | }
404 | return out.toString();
405 | }
406 | }
407 |
--------------------------------------------------------------------------------