From 19b1885b4a180014d8bebddf338b4a32278b6829 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A7=8B=E9=80=B8?= Date: Wed, 15 Mar 2017 17:00:29 +0800 Subject: [PATCH] update RxCaptcha update RxTextUtils update RxPopupView --- RxTools-library/src/main/AndroidManifest.xml | 1 - .../com/vondear/rxtools/RxAnimationUtils.java | 100 +++++++ .../com/vondear/rxtools/RxBroadcastUtils.java | 40 +++ .../java/com/vondear/rxtools/RxDataUtils.java | 3 - .../java/com/vondear/rxtools/RxRegUtils.java | 213 ++++++++++++++ .../rxtools/interfaces/onUpdateListener.java | 9 + .../com/vondear/rxtools/view/RxCaptcha.java | 264 ++++++++++++++++++ 7 files changed, 626 insertions(+), 4 deletions(-) create mode 100644 RxTools-library/src/main/java/com/vondear/rxtools/RxAnimationUtils.java create mode 100644 RxTools-library/src/main/java/com/vondear/rxtools/RxBroadcastUtils.java create mode 100644 RxTools-library/src/main/java/com/vondear/rxtools/RxRegUtils.java create mode 100644 RxTools-library/src/main/java/com/vondear/rxtools/interfaces/onUpdateListener.java create mode 100644 RxTools-library/src/main/java/com/vondear/rxtools/view/RxCaptcha.java diff --git a/RxTools-library/src/main/AndroidManifest.xml b/RxTools-library/src/main/AndroidManifest.xml index 200f91e3..7f998715 100644 --- a/RxTools-library/src/main/AndroidManifest.xml +++ b/RxTools-library/src/main/AndroidManifest.xml @@ -5,7 +5,6 @@ - diff --git a/RxTools-library/src/main/java/com/vondear/rxtools/RxAnimationUtils.java b/RxTools-library/src/main/java/com/vondear/rxtools/RxAnimationUtils.java new file mode 100644 index 00000000..883eef2a --- /dev/null +++ b/RxTools-library/src/main/java/com/vondear/rxtools/RxAnimationUtils.java @@ -0,0 +1,100 @@ +package com.vondear.rxtools; + +import android.animation.Animator; +import android.animation.ArgbEvaluator; +import android.animation.ObjectAnimator; +import android.animation.ValueAnimator; +import android.view.View; +import android.view.animation.AccelerateInterpolator; +import android.view.animation.DecelerateInterpolator; +import android.view.animation.Interpolator; + +import com.vondear.rxtools.interfaces.onUpdateListener; + +import static com.vondear.rxtools.RxImageUtils.invisToVis; +import static com.vondear.rxtools.RxImageUtils.visToInvis; + +/** + * Created by Administrator on 2017/3/15. + */ + +public class RxAnimationUtils { + + /** + * 颜色渐变动画 + * @param beforeColor 变化之前的颜色 + * @param afterColor 变化之后的颜色 + * @param listener 变化事件 + */ + public static void animationColorGradient(int beforeColor, int afterColor, final onUpdateListener listener) { + ValueAnimator valueAnimator = ValueAnimator.ofObject(new ArgbEvaluator(), beforeColor, afterColor).setDuration(3000); + valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { + @Override + public void onAnimationUpdate(ValueAnimator animation) { +// textView.setTextColor((Integer) animation.getAnimatedValue()); + listener.onUpdate((Integer) animation.getAnimatedValue()); + } + }); + valueAnimator.start(); + } + + /** + * 卡片翻转动画 + * @param beforeView + * @param AfterView + */ + public static void FilpAnimation(final View beforeView, final View AfterView) { + Interpolator accelerator = new AccelerateInterpolator(); + Interpolator decelerator = new DecelerateInterpolator(); + if (beforeView.getVisibility() == View.GONE) { + // 局部layout可达到字体翻转 背景不翻转 + invisToVis = ObjectAnimator.ofFloat(beforeView, + "rotationY", -90f, 0f); + visToInvis = ObjectAnimator.ofFloat(AfterView, + "rotationY", 0f, 90f); + } else if (AfterView.getVisibility() == View.GONE) { + invisToVis = ObjectAnimator.ofFloat(AfterView, + "rotationY", -90f, 0f); + visToInvis = ObjectAnimator.ofFloat(beforeView, + "rotationY", 0f, 90f); + } + + visToInvis.setDuration(250);// 翻转速度 + visToInvis.setInterpolator(accelerator);// 在动画开始的地方速率改变比较慢,然后开始加速 + invisToVis.setDuration(250); + invisToVis.setInterpolator(decelerator); + visToInvis.addListener(new Animator.AnimatorListener() { + + @Override + public void onAnimationEnd(Animator arg0) { + if (beforeView.getVisibility() == View.GONE) { + AfterView.setVisibility(View.GONE); + invisToVis.start(); + beforeView.setVisibility(View.VISIBLE); + } else { + AfterView.setVisibility(View.GONE); + visToInvis.start(); + beforeView.setVisibility(View.VISIBLE); + } + } + + @Override + public void onAnimationCancel(Animator arg0) { + + } + + @Override + public void onAnimationRepeat(Animator arg0) { + + } + + @Override + public void onAnimationStart(Animator arg0) { + + } + }); + visToInvis.start(); + } + + +} diff --git a/RxTools-library/src/main/java/com/vondear/rxtools/RxBroadcastUtils.java b/RxTools-library/src/main/java/com/vondear/rxtools/RxBroadcastUtils.java new file mode 100644 index 00000000..c20a192e --- /dev/null +++ b/RxTools-library/src/main/java/com/vondear/rxtools/RxBroadcastUtils.java @@ -0,0 +1,40 @@ +package com.vondear.rxtools; + +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.content.IntentFilter; +import android.net.ConnectivityManager; + +/** + * Created by Administrator on 2017/3/15. + */ + +public class RxBroadcastUtils { + + + /** + * 网络状态改变广播 + */ + public class BroadcastReceiverNetWork extends BroadcastReceiver { + @Override + public void onReceive(Context context, Intent intent) { + RxNetUtils.getNetWorkType(context); + } + } + + + /** + * 注册监听网络状态的广播 + * @param context + * @return + */ + private BroadcastReceiverNetWork initRegisterReceiverNetWork(Context context) { + // 注册监听网络状态的服务 + BroadcastReceiverNetWork mReceiverNetWork = new BroadcastReceiverNetWork(); + IntentFilter mFilter = new IntentFilter(); + mFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION); + context.registerReceiver(mReceiverNetWork, mFilter); + return mReceiverNetWork; + } +} diff --git a/RxTools-library/src/main/java/com/vondear/rxtools/RxDataUtils.java b/RxTools-library/src/main/java/com/vondear/rxtools/RxDataUtils.java index b327777e..16d64e7c 100644 --- a/RxTools-library/src/main/java/com/vondear/rxtools/RxDataUtils.java +++ b/RxTools-library/src/main/java/com/vondear/rxtools/RxDataUtils.java @@ -874,9 +874,6 @@ public static OutputStream string2OutputStream(String string, String charsetName } - - - /** * string替换 * %n$ms:代表输出的是字符串,n代表是第几个参数,设置m的值可以在输出之前放置空格 diff --git a/RxTools-library/src/main/java/com/vondear/rxtools/RxRegUtils.java b/RxTools-library/src/main/java/com/vondear/rxtools/RxRegUtils.java new file mode 100644 index 00000000..2d7df399 --- /dev/null +++ b/RxTools-library/src/main/java/com/vondear/rxtools/RxRegUtils.java @@ -0,0 +1,213 @@ +package com.vondear.rxtools; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import static com.vondear.rxtools.RxConstUtils.REGEX_CHZ; +import static com.vondear.rxtools.RxConstUtils.REGEX_DATE; +import static com.vondear.rxtools.RxConstUtils.REGEX_EMAIL; +import static com.vondear.rxtools.RxConstUtils.REGEX_IDCARD; +import static com.vondear.rxtools.RxConstUtils.REGEX_IDCARD15; +import static com.vondear.rxtools.RxConstUtils.REGEX_IDCARD18; +import static com.vondear.rxtools.RxConstUtils.REGEX_IP; +import static com.vondear.rxtools.RxConstUtils.REGEX_MOBILE_EXACT; +import static com.vondear.rxtools.RxConstUtils.REGEX_MOBILE_SIMPLE; +import static com.vondear.rxtools.RxConstUtils.REGEX_TEL; +import static com.vondear.rxtools.RxConstUtils.REGEX_URL; +import static com.vondear.rxtools.RxConstUtils.REGEX_USERNAME; +import static com.vondear.rxtools.RxDataUtils.isNullString; + +/** + * Created by Administrator on 2017/3/15. + */ + +public class RxRegUtils { + //--------------------------------------------正则表达式----------------------------------------- + /** + * 原文链接:http://caibaojian.com/regexp-example.html + * 提取信息中的网络链接:(h|H)(r|R)(e|E)(f|F) *= *('|")?(\w|\\|\/|\.)+('|"| *|>)? + * 提取信息中的邮件地址:\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)* + * 提取信息中的图片链接:(s|S)(r|R)(c|C) *= *('|")?(\w|\\|\/|\.)+('|"| *|>)? + * 提取信息中的IP地址:(\d+)\.(\d+)\.(\d+)\.(\d+) + * 提取信息中的中国电话号码(包括移动和固定电话):(\(\d{3,4}\)|\d{3,4}-|\s)?\d{7,14} + * 提取信息中的中国邮政编码:[1-9]{1}(\d+){5} + * 提取信息中的中国身份证号码:\d{18}|\d{15} + * 提取信息中的整数:\d+ + * 提取信息中的浮点数(即小数):(-?\d*)\.?\d+ + * 提取信息中的任何数字 :(-?\d*)(\.\d+)? + * 提取信息中的中文字符串:[\u4e00-\u9fa5]* + * 提取信息中的双字节字符串 (汉字):[^\x00-\xff]* + */ + + + /** + * 判断是否为真实手机号 + * + * @param mobiles + * @return + */ + public static boolean isMobile(String mobiles) { + Pattern p = Pattern.compile("^1(3[0-9]|5[012356789]|8[0256789]|7[0678])\\d{8}$"); + Matcher m = p.matcher(mobiles); + return m.matches(); + } + + /** + * 验证银卡卡号 + * + * @param cardNo + * @return + */ + public static boolean isBankCard(String cardNo) { + Pattern p = Pattern.compile("^\\d{16,19}$|^\\d{6}[- ]\\d{10,13}$|^\\d{4}[- ]\\d{4}[- ]\\d{4}[- ]\\d{4,7}$"); + Matcher m = p.matcher(cardNo); + return m.matches(); + } + + /** + * 15位和18位身份证号码的正则表达式 身份证验证 + * + * @param idCard + * @return + */ + public static boolean validateIdCard(String idCard) { + // 15位和18位身份证号码的正则表达式 + String regIdCard = "^(^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$)|(^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])((\\d{4})|\\d{3}[Xx])$)$"; + Pattern p = Pattern.compile(regIdCard); + return p.matcher(idCard).matches(); + } + //=========================================正则表达式============================================= + + /** + * 验证手机号(简单) + * + * @param string 待验证文本 + * @return {@code true}: 匹配
{@code false}: 不匹配 + */ + public static boolean isMobileSimple(String string) { + return isMatch(REGEX_MOBILE_SIMPLE, string); + } + + /** + * 验证手机号(精确) + * + * @param string 待验证文本 + * @return {@code true}: 匹配
{@code false}: 不匹配 + */ + public static boolean isMobileExact(String string) { + return isMatch(REGEX_MOBILE_EXACT, string); + } + + /** + * 验证电话号码 + * + * @param string 待验证文本 + * @return {@code true}: 匹配
{@code false}: 不匹配 + */ + public static boolean isTel(String string) { + return isMatch(REGEX_TEL, string); + } + + /** + * 验证身份证号码15位 + * + * @param string 待验证文本 + * @return {@code true}: 匹配
{@code false}: 不匹配 + */ + public static boolean isIDCard15(String string) { + return isMatch(REGEX_IDCARD15, string); + } + + /** + * 验证身份证号码18位 + * + * @param string 待验证文本 + * @return {@code true}: 匹配
{@code false}: 不匹配 + */ + public static boolean isIDCard18(String string) { + return isMatch(REGEX_IDCARD18, string); + } + + /** + * 验证身份证号码15或18位 包含以x结尾 + * + * @param string 待验证文本 + * @return {@code true}: 匹配
{@code false}: 不匹配 + */ + public static boolean isIDCard(String string) { + return isMatch(REGEX_IDCARD, string); + } + + + /** + * 验证邮箱 + * + * @param string 待验证文本 + * @return {@code true}: 匹配
{@code false}: 不匹配 + */ + public static boolean isEmail(String string) { + return isMatch(REGEX_EMAIL, string); + } + + /** + * 验证URL + * + * @param string 待验证文本 + * @return {@code true}: 匹配
{@code false}: 不匹配 + */ + public static boolean isURL(String string) { + return isMatch(REGEX_URL, string); + } + + /** + * 验证汉字 + * + * @param string 待验证文本 + * @return {@code true}: 匹配
{@code false}: 不匹配 + */ + public static boolean isChz(String string) { + return isMatch(REGEX_CHZ, string); + } + + /** + * 验证用户名 + *

取值范围为a-z,A-Z,0-9,"_",汉字,不能以"_"结尾,用户名必须是6-20位

+ * + * @param string 待验证文本 + * @return {@code true}: 匹配
{@code false}: 不匹配 + */ + public static boolean isUsername(String string) { + return isMatch(REGEX_USERNAME, string); + } + + /** + * 验证yyyy-MM-dd格式的日期校验,已考虑平闰年 + * + * @param string 待验证文本 + * @return {@code true}: 匹配
{@code false}: 不匹配 + */ + public static boolean isDate(String string) { + return isMatch(REGEX_DATE, string); + } + + /** + * 验证IP地址 + * + * @param string 待验证文本 + * @return {@code true}: 匹配
{@code false}: 不匹配 + */ + public static boolean isIP(String string) { + return isMatch(REGEX_IP, string); + } + + /** + * string是否匹配regex正则表达式字符串 + * + * @param regex 正则表达式字符串 + * @param string 要匹配的字符串 + * @return {@code true}: 匹配
{@code false}: 不匹配 + */ + public static boolean isMatch(String regex, String string) { + return !isNullString(string) && Pattern.matches(regex, string); + } +} diff --git a/RxTools-library/src/main/java/com/vondear/rxtools/interfaces/onUpdateListener.java b/RxTools-library/src/main/java/com/vondear/rxtools/interfaces/onUpdateListener.java new file mode 100644 index 00000000..03a5a569 --- /dev/null +++ b/RxTools-library/src/main/java/com/vondear/rxtools/interfaces/onUpdateListener.java @@ -0,0 +1,9 @@ +package com.vondear.rxtools.interfaces; + +/** + * Created by Administrator on 2017/3/10. + */ + +public interface onUpdateListener { + void onUpdate(int intValue); +} diff --git a/RxTools-library/src/main/java/com/vondear/rxtools/view/RxCaptcha.java b/RxTools-library/src/main/java/com/vondear/rxtools/view/RxCaptcha.java new file mode 100644 index 00000000..81972a22 --- /dev/null +++ b/RxTools-library/src/main/java/com/vondear/rxtools/view/RxCaptcha.java @@ -0,0 +1,264 @@ +package com.vondear.rxtools.view; + +import android.graphics.Bitmap; +import android.graphics.Bitmap.Config; +import android.graphics.Canvas; +import android.graphics.Color; +import android.graphics.Paint; +import android.widget.ImageView; + +import java.util.Random; + +/** + * 随机生成验证码,使用方法: + *

+ * 拿到验证码图片ImageView + * mIvCode.setImageBitmap(RxCaptcha.getInstance().createBitmap()); + * int code=RxCaptcha.getInstance().getCode(); + *

+ * 只需生成验证码值 String + * + *

+ * RxCaptcha + * 2015年2月10日 上午11:32:34 + * + * @version 1.0.0 + */ +public class RxCaptcha { + + public static RxCaptcha getInstance() { + if (rxCaptcha == null) { + rxCaptcha = new RxCaptcha(); + } + return rxCaptcha; + } + + private static final char[] CHARS_NUMBER = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}; + + private static final char[] CHARS_LETTER = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', + 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', + 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', + 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', + 'X', 'Y', 'Z'}; + + private static final char[] CHARS = {'0', '1', '2', '3', '4', '5', '6', + '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', + 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', + 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', + 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', + 'X', 'Y', 'Z'}; + + private static RxCaptcha rxCaptcha; + + private int type = 2; + + private RxCaptcha() { + + } + + private RxCaptcha(int types) { + this.type = types; + } + + public static RxCaptcha getInstance(int types) { + if (rxCaptcha == null) { + rxCaptcha = new RxCaptcha(types); + } + return rxCaptcha; + } + + // default settings + private static final int DEFAULT_CODE_LENGTH = 4;// 验证码的长度 这里是4位 + private static final int DEFAULT_FONT_SIZE = 60;// 字体大小 + private static final int DEFAULT_LINE_NUMBER = 0;// 多少条干扰线 + private static final int BASE_PADDING_LEFT = 20; // 左边距 + private static final int RANGE_PADDING_LEFT = 20;// 左边距范围值 + private static final int BASE_PADDING_TOP = 42;// 上边距 + private static final int RANGE_PADDING_TOP = 15;// 上边距范围值 + private static int DEFAULT_WIDTH = 200;// 默认宽度.图片的总宽 + private static int DEFAULT_HEIGHT = 70;// 默认高度.图片的总高 + private int DEFAULT_COLOR = 0xdf;// 默认背景颜色值 + + // settings decided by the layout xml + // canvas width and height + private int width = DEFAULT_WIDTH; + private int height = DEFAULT_HEIGHT; + + // random word space and pading_top + private int base_padding_left = BASE_PADDING_LEFT; + private int range_padding_left = RANGE_PADDING_LEFT; + private int base_padding_top = BASE_PADDING_TOP; + private int range_padding_top = RANGE_PADDING_TOP; + + // number of chars, lines; font size + private int codeLength = DEFAULT_CODE_LENGTH; + private int line_number = DEFAULT_LINE_NUMBER; + private int font_size = DEFAULT_FONT_SIZE; + + // variables + private String code;// 保存生成的验证码 + private int padding_left, padding_top; + private Random random = new Random(); + + /** + * @param length 验证码的长度 + * @return + */ + public RxCaptcha codeLength(int length) { + codeLength = length; + return rxCaptcha; + } + + /** + * @param size 字体大小 + * @return + */ + public RxCaptcha fontSize(int size) { + font_size = size; + return rxCaptcha; + } + + /** + * @param number 干扰线 数量 + * @return + */ + public RxCaptcha lineNumber(int number) { + line_number = number; + return rxCaptcha; + } + + /** + * @return 背景颜色值 + */ + public RxCaptcha backColor(int colorInt) { + DEFAULT_COLOR = colorInt; + return rxCaptcha; + } + + public RxCaptcha type(int type) { + this.type = type; + return rxCaptcha; + } + + public RxCaptcha size(int width, int height) { + this.width = width; + this.height = height; + return rxCaptcha; + } + + private Bitmap makeBitmap() { + padding_left = 0; + + Bitmap bp = Bitmap.createBitmap(width, height, Config.ARGB_8888); + Canvas c = new Canvas(bp); + + code = makeCode(); + + c.drawColor(Color.rgb(DEFAULT_COLOR, DEFAULT_COLOR, DEFAULT_COLOR)); + Paint paint = new Paint(); + paint.setTextSize(font_size); + + for (int i = 0; i < code.length(); i++) { + randomTextStyle(paint); + randomPadding(); + c.drawText(code.charAt(i) + "", padding_left, padding_top, paint); + } + + for (int i = 0; i < line_number; i++) { + drawLine(c, paint); + } + + c.save(Canvas.ALL_SAVE_FLAG);// 保存 + c.restore();// + return bp; + } + + public String getCode() { + return code.toLowerCase(); + } + + public Bitmap into(ImageView imageView){ + Bitmap bitmap = createBitmap(); + if (imageView != null) { + imageView.setImageBitmap(bitmap); + } + return bitmap; + } + + public String createCode() { + return makeCode(); + } + + private Bitmap mBitmapCode; + + public Bitmap createBitmap() { + mBitmapCode = makeBitmap(); + return mBitmapCode; + } + + private String makeCode() { + StringBuilder buffer = new StringBuilder(); + switch (type) { + case 0: + for (int i = 0; i < codeLength; i++) { + buffer.append(CHARS_NUMBER[random.nextInt(CHARS_NUMBER.length)]); + } + break; + case 1: + for (int i = 0; i < codeLength; i++) { + buffer.append(CHARS_LETTER[random.nextInt(CHARS_LETTER.length)]); + } + break; + case 2: + for (int i = 0; i < codeLength; i++) { + buffer.append(CHARS[random.nextInt(CHARS.length)]); + } + break; + default: + for (int i = 0; i < codeLength; i++) { + buffer.append(CHARS[random.nextInt(CHARS.length)]); + } + break; + } + + return buffer.toString(); + } + + private void drawLine(Canvas canvas, Paint paint) { + int color = randomColor(); + int startX = random.nextInt(width); + int startY = random.nextInt(height); + int stopX = random.nextInt(width); + int stopY = random.nextInt(height); + paint.setStrokeWidth(1); + paint.setColor(color); + canvas.drawLine(startX, startY, stopX, stopY, paint); + } + + private int randomColor() { + return randomColor(1); + } + + private int randomColor(int rate) { + int red = random.nextInt(256) / rate; + int green = random.nextInt(256) / rate; + int blue = random.nextInt(256) / rate; + return Color.rgb(red, green, blue); + } + + private void randomTextStyle(Paint paint) { + int color = randomColor(); + paint.setColor(color); + paint.setFakeBoldText(random.nextBoolean()); // true为粗体,false为非粗体 + float skewX = random.nextInt(11) / 10; + skewX = random.nextBoolean() ? skewX : -skewX; + // paint.setTextSkewX(skewX); // float类型参数,负数表示右斜,整数左斜 + // paint.setUnderlineText(true); //true为下划线,false为非下划线 + // paint.setStrikeThruText(true); //true为删除线,false为非删除线 + } + + private void randomPadding() { + padding_left += base_padding_left + random.nextInt(range_padding_left); + padding_top = base_padding_top + random.nextInt(range_padding_top); + } +}