d
This commit is contained in:
1
baselibs/.gitignore
vendored
Normal file
1
baselibs/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
/build
|
||||
32
baselibs/build.gradle
Normal file
32
baselibs/build.gradle
Normal file
@@ -0,0 +1,32 @@
|
||||
apply plugin: 'com.android.library'
|
||||
|
||||
android {
|
||||
compileSdkVersion 28
|
||||
buildToolsVersion '28.0.3'
|
||||
|
||||
defaultConfig {
|
||||
minSdkVersion 15
|
||||
targetSdkVersion 28
|
||||
versionCode 1
|
||||
versionName "1.0"
|
||||
}
|
||||
}
|
||||
|
||||
dependencies {
|
||||
compile fileTree(include: ['*.jar'], dir: 'libs')
|
||||
compile 'com.123ufo:BannerView:1.0'
|
||||
//Rxjava
|
||||
compile 'io.reactivex.rxjava2:rxjava:2.1.5'
|
||||
compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
|
||||
compile 'com.squareup.retrofit2:retrofit:2.4.0'
|
||||
compile 'com.squareup.retrofit2:converter-gson:2.3.0'
|
||||
compile 'com.squareup.okhttp3:logging-interceptor:3.5.0'
|
||||
compile 'com.squareup.retrofit2:adapter-rxjava2:2.3.0'
|
||||
//地址选择器 https://github.com/chihane/JDAddressSelector
|
||||
compile 'com.github.chihane:JDAddressSelector:1.1.4'
|
||||
compile 'com.github.bumptech.glide:glide:4.7.1'
|
||||
//dagger
|
||||
// compile 'com.google.dagger:dagger:2.6'
|
||||
// annotationProcessor 'com.google.dagger:dagger-compiler:2.6'
|
||||
}
|
||||
|
||||
53
baselibs/proguard-rules.pro
vendored
Normal file
53
baselibs/proguard-rules.pro
vendored
Normal file
@@ -0,0 +1,53 @@
|
||||
# Add project specific ProGuard rules here.
|
||||
# By default, the flags in this file are appended to flags specified
|
||||
# in D:\android_studo_1.4\AS_ASK_1.4/tools/proguard/proguard-android.txt
|
||||
# You can edit the include path and order by changing the proguardFiles
|
||||
# directive in build.gradle.
|
||||
#
|
||||
# For more details, see
|
||||
# http://developer.android.com/guide/developing/tools/proguard.html
|
||||
|
||||
# Add any project specific keep options here:
|
||||
|
||||
# If your project uses WebView with JS, uncomment the following
|
||||
# and specify the fully qualified class name to the JavaScript interface
|
||||
# class:
|
||||
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
|
||||
# public *;
|
||||
#}
|
||||
-optimizationpasses 5 # 指定代码的压缩级别
|
||||
-dontusemixedcaseclassnames # 是否使用大小写混合
|
||||
-dontpreverify # 混淆时是否做预校验
|
||||
-verbose # 混淆时是否记录日志
|
||||
|
||||
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/* # 混淆时所采用的算法
|
||||
|
||||
-keep public class * extends android.app.Activity # 保持哪些类不被混淆
|
||||
-keep public class * extends android.app.Application # 保持哪些类不被混淆
|
||||
-keep public class * extends android.app.Service # 保持哪些类不被混淆
|
||||
-keep public class * extends android.content.BroadcastReceiver # 保持哪些类不被混淆
|
||||
-keep public class * extends android.content.ContentProvider # 保持哪些类不被混淆
|
||||
-keep public class * extends android.app.backup.BackupAgentHelper # 保持哪些类不被混淆
|
||||
-keep public class * extends android.preference.Preference # 保持哪些类不被混淆
|
||||
-keep public class com.android.vending.licensing.ILicensingService # 保持哪些类不被混淆
|
||||
|
||||
-keepclasseswithmembernames class * { # 保持 native 方法不被混淆
|
||||
native <methods>;
|
||||
}
|
||||
-keepclasseswithmembers class * { # 保持自定义控件类不被混淆
|
||||
public <init>(android.content.Context, android.util.AttributeSet);
|
||||
}
|
||||
-keepclasseswithmembers class * {# 保持自定义控件类不被混淆
|
||||
public <init>(android.content.Context, android.util.AttributeSet, int);
|
||||
}
|
||||
-keepclassmembers class * extends android.app.Activity { # 保持自定义控件类不被混淆
|
||||
public void *(android.view.View);
|
||||
}
|
||||
-keepclassmembers enum * { # 保持枚举 enum 类不被混淆
|
||||
public static **[] values();
|
||||
public static ** valueOf(java.lang.String);
|
||||
}
|
||||
-keep class * implements android.os.Parcelable { # 保持 Parcelable 不被混淆
|
||||
public static final android.os.Parcelable$Creator *;
|
||||
}
|
||||
#第三方混淆添加
|
||||
11
baselibs/src/main/AndroidManifest.xml
Normal file
11
baselibs/src/main/AndroidManifest.xml
Normal file
@@ -0,0 +1,11 @@
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
package="com.antiphon.baselibs">
|
||||
|
||||
<application android:allowBackup="true"
|
||||
android:label="@string/app_name"
|
||||
android:supportsRtl="true"
|
||||
>
|
||||
|
||||
</application>
|
||||
|
||||
</manifest>
|
||||
166
baselibs/src/main/java/com/baselibs/helper/CharacterParser.java
Normal file
166
baselibs/src/main/java/com/baselibs/helper/CharacterParser.java
Normal file
@@ -0,0 +1,166 @@
|
||||
package com.baselibs.helper;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
/**
|
||||
* Java汉字转换为拼音
|
||||
*
|
||||
* @author Mr.Z
|
||||
*/
|
||||
public class CharacterParser {
|
||||
private static final String TAG = "CharacterParser";
|
||||
private static int[] pyvalue = new int[]{-20319, -20317, -20304, -20295, -20292, -20283, -20265, -20257,
|
||||
-20242, -20230, -20051, -20036, -20032, -20026, -20002, -19990, -19986, -19982, -19976, -19805, -19784, -19775, -19774, -19763,
|
||||
-19756, -19751, -19746, -19741, -19739, -19728, -19725, -19715, -19540, -19531, -19525, -19515, -19500, -19484, -19479, -19467,
|
||||
-19289, -19288, -19281, -19275, -19270, -19263, -19261, -19249, -19243, -19242, -19238, -19235, -19227, -19224, -19218, -19212,
|
||||
-19038, -19023, -19018, -19006, -19003, -18996, -18977, -18961, -18952, -18783, -18774, -18773, -18763, -18756, -18741, -18735,
|
||||
-18731, -18722, -18710, -18697, -18696, -18526, -18518, -18501, -18490, -18478, -18463, -18448, -18447, -18446, -18239, -18237,
|
||||
-18231, -18220, -18211,
|
||||
-18201, -18184, -18183, -18181, -18012, -17997, -17988, -17970, -17964, -17961, -17950, -17947, -17931, -17928, -17922,
|
||||
-17759, -17752, -17733, -17730, -17721, -17703, -17701, -17697, -17692, -17683, -17676, -17496, -17487, -17482, -17468,
|
||||
-17454, -17433, -17427, -17417, -17202, -17185, -16983, -16970, -16942, -16915, -16733, -16708, -16706, -16689, -16664,
|
||||
-16657, -16647, -16474, -16470, -16465, -16459, -16452, -16448, -16433, -16429, -16427, -16423, -16419, -16412, -16407,
|
||||
-16403, -16401, -16393, -16220, -16216, -16212, -16205, -16202, -16187, -16180, -16171, -16169, -16158, -16155, -15959,
|
||||
-15958, -15944, -15933, -15920, -15915, -15903, -15889, -15878, -15707, -15701, -15681, -15667, -15661, -15659, -15652,
|
||||
-15640, -15631, -15625, -15454, -15448, -15436, -15435, -15419,
|
||||
-15416, -15408, -15394, -15385, -15377, -15375, -15369, -15363, -15362, -15183, -15180, -15165, -15158, -15153, -15150,
|
||||
-15149, -15144, -15143, -15141, -15140, -15139, -15128, -15121, -15119, -15117, -15110, -15109, -14941, -14937, -14933,
|
||||
-14930, -14929, -14928, -14926, -14922, -14921, -14914, -14908, -14902, -14894, -14889, -14882, -14873, -14871, -14857,
|
||||
-14678, -14674, -14670, -14668, -14663, -14654, -14645, -14630, -14594, -14429, -14407, -14399, -14384, -14379, -14368,
|
||||
-14355, -14353, -14345, -14170, -14159, -14151, -14149, -14145, -14140, -14137, -14135, -14125, -14123, -14122, -14112,
|
||||
-14109, -14099, -14097, -14094, -14092, -14090, -14087, -14083, -13917, -13914, -13910, -13907, -13906, -13905, -13896,
|
||||
-13894, -13878, -13870, -13859, -13847, -13831, -13658, -13611,
|
||||
-13601, -13406, -13404, -13400, -13398, -13395, -13391, -13387, -13383, -13367, -13359, -13356, -13343, -13340, -13329,
|
||||
-13326, -13318, -13147, -13138, -13120, -13107, -13096, -13095, -13091, -13076, -13068, -13063, -13060, -12888, -12875,
|
||||
-12871, -12860, -12858, -12852, -12849, -12838, -12831, -12829, -12812, -12802, -12607, -12597, -12594, -12585, -12556,
|
||||
-12359, -12346, -12320, -12300, -12120, -12099, -12089, -12074, -12067, -12058, -12039, -11867, -11861, -11847, -11831,
|
||||
-11798, -11781, -11604, -11589, -11536, -11358, -11340, -11339, -11324, -11303, -11097, -11077, -11067, -11055, -11052,
|
||||
-11045, -11041, -11038, -11024, -11020, -11019, -11018, -11014, -10838, -10832, -10815, -10800, -10790, -10780, -10764,
|
||||
-10587, -10544, -10533, -10519, -10331, -10329, -10328, -10322,
|
||||
-10315, -10309, -10307, -10296, -10281, -10274, -10270, -10262, -10260, -10256, -10254};
|
||||
public static String[] pystr = new String[]{"a", "ai", "an", "ang", "ao", "ba", "bai", "ban",
|
||||
"bang", "bao", "bei", "ben", "beng", "bi", "bian", "biao", "bie", "bin", "bing", "bo", "bu", "ca", "cai", "can",
|
||||
"cang", "cao", "ce", "ceng", "cha", "chai", "chan", "chang", "chao", "che", "chen", "cheng", "chi", "chong", "chou",
|
||||
"chu", "chuai", "chuan", "chuang", "chui", "chun", "chuo", "ci", "cong", "cou", "cu", "cuan", "cui", "cun", "cuo", "da",
|
||||
"dai", "dan", "dang", "dao", "de", "deng", "di", "dian", "diao", "die", "ding", "diu", "dong", "dou", "du", "duan", "dui",
|
||||
"dun", "duo", "e", "en", "er", "fa", "fan", "fang", "fei", "fen", "feng", "fo", "fou", "fu", "ga", "gai", "gan", "gang",
|
||||
"gao", "ge", "gei", "gen", "geng", "gong", "gou", "gu", "gua", "guai", "guan",
|
||||
"guang", "gui", "gun", "guo", "ha", "hai", "han", "hang", "hao", "he", "hei", "hen", "heng", "hong", "hou", "hu",
|
||||
"hua", "huai", "huan", "huang", "hui", "hun", "huo", "ji", "jia", "jian", "jiang", "jiao", "jie", "jin", "jing",
|
||||
"jiong", "jiu", "ju", "juan", "jue", "jun", "ka", "kai", "kan", "kang", "kao", "ke", "ken", "keng", "kong", "kou",
|
||||
"ku", "kua", "kuai", "kuan", "kuang", "kui", "kun", "kuo", "la", "lai", "lan", "lang", "lao", "le", "lei", "leng",
|
||||
"li", "lia", "lian", "liang", "liao", "lie", "lin", "ling", "liu", "long", "lou", "lu", "lv", "luan", "lue", "lun",
|
||||
"luo", "ma", "mai", "man", "mang", "mao", "me", "mei", "men", "meng", "mi", "mian", "miao", "mie", "min", "ming",
|
||||
"miu", "mo", "mou", "mu", "na", "nai", "nan", "nang", "nao", "ne", "nei", "nen", "neng", "ni",
|
||||
"nian", "niang", "niao", "nie", "nin", "ning", "niu", "nong", "nu", "nv", "nuan", "nue", "nuo", "o", "ou", "pa",
|
||||
"pai", "pan", "pang", "pao", "pei", "pen", "peng", "pi", "pian", "piao", "pie", "pin", "ping", "po", "pu", "qi",
|
||||
"qia", "qian", "qiang", "qiao", "qie", "qin", "qing", "qiong", "qiu", "qu", "quan", "que", "qun", "ran", "rang",
|
||||
"rao", "re", "ren", "reng", "ri", "rong", "rou", "ru", "ruan", "rui", "run", "ruo", "sa", "sai", "san", "sang",
|
||||
"sao", "se", "sen", "seng", "sha", "shai", "shan", "shang", "shao", "she", "shen", "sheng", "shi", "shou", "shu",
|
||||
"shua", "shuai", "shuan", "shuang", "shui", "shun", "shuo", "si", "song", "sou", "su", "suan", "sui", "sun", "suo",
|
||||
"ta", "tai", "tan", "tang", "tao", "te", "teng", "ti", "tian", "tiao", "tie", "ting", "tong", "tou", "tu",
|
||||
"tuan", "tui", "tun", "tuo", "wa", "wai", "wan", "wang", "wei", "wen", "weng", "wo", "wu", "xi", "xia", "xian",
|
||||
"xiang", "xiao", "xie", "xin", "xing", "xiong", "xiu", "xu", "xuan", "xue", "xun", "ya", "yan", "yang", "yao",
|
||||
"ye", "yi", "yin", "ying", "yo", "yong", "you", "yu", "yuan", "yue", "yun", "za", "zai", "zan", "zang", "zao",
|
||||
"ze", "zei", "zen", "zeng", "zha", "zhai", "zhan", "zhang", "zhao", "zhe", "zhen", "zheng", "zhi", "zhong", "zhou",
|
||||
"zhu", "zhua", "zhuai", "zhuan", "zhuang", "zhui", "zhun", "zhuo", "zi", "zong", "zou", "zu", "zuan", "zui", "zun", "zuo"};
|
||||
private StringBuilder buffer;
|
||||
private String resource;
|
||||
private static CharacterParser characterParser = new CharacterParser();
|
||||
|
||||
public static CharacterParser getInstance() {
|
||||
return characterParser;
|
||||
}
|
||||
|
||||
public String getResource() {
|
||||
return resource;
|
||||
}
|
||||
|
||||
public void setResource(String resource) {
|
||||
this.resource = resource;
|
||||
}
|
||||
|
||||
/**
|
||||
* 汉字转成ASCII码 * * @param chs * @return
|
||||
*/
|
||||
private int getChsAscii(String chs) {
|
||||
int asc = 0;
|
||||
try {
|
||||
byte[] bytes = chs.getBytes("gb2312");
|
||||
if (bytes == null || bytes.length > 2 || bytes.length <= 0) {
|
||||
throw new RuntimeException("illegal resource string");
|
||||
}
|
||||
if (bytes.length == 1) {
|
||||
asc = bytes[0];
|
||||
}
|
||||
if (bytes.length == 2) {
|
||||
int hightByte = 256 + bytes[0];
|
||||
int lowByte = 256 + bytes[1];
|
||||
asc = (256 * hightByte + lowByte) - 256 * 256;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
System.out.println("ERROR:ChineseSpelling.class-getChsAscii(String chs)" + e);
|
||||
}
|
||||
return asc;
|
||||
}
|
||||
|
||||
/**
|
||||
* 单字解析 * * @param str * @return
|
||||
*/
|
||||
public String convert(String str) {
|
||||
String result = null;
|
||||
int ascii = getChsAscii(str);
|
||||
if (ascii > 0 && ascii < 160) {
|
||||
result = String.valueOf((char) ascii);
|
||||
} else {
|
||||
for (int i = (pyvalue.length - 1); i >= 0; i--) {
|
||||
if (pyvalue[i] <= ascii) {
|
||||
result = pystr[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 词组解析 * * @param chs * @return
|
||||
*/
|
||||
public String getSelling(String chs) {
|
||||
String key, value = null;
|
||||
buffer = new StringBuilder();
|
||||
for (int i = 0; i < chs.length(); i++) {
|
||||
key = chs.substring(i, i + 1);
|
||||
if (key.getBytes().length >= 2) {
|
||||
value = (String) convert(key);
|
||||
if (value == null) {
|
||||
value = "unknown";
|
||||
}
|
||||
} else {
|
||||
value = key;
|
||||
}
|
||||
buffer.append(value);
|
||||
}
|
||||
Log.d(TAG, "buffer: " + buffer.toString());
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取一个字符串的首字母,或是#
|
||||
* @param chs
|
||||
* @return
|
||||
*/
|
||||
public String getFirstLetter(String chs) {
|
||||
String firstLetter = getSelling(chs).substring(0, 1).toUpperCase();
|
||||
if (!firstLetter.matches("[A-Z]")) {
|
||||
firstLetter = "#";
|
||||
}
|
||||
return firstLetter;
|
||||
|
||||
}
|
||||
|
||||
public String getSpelling() {
|
||||
return this.getSelling(this.getResource());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,248 @@
|
||||
package com.baselibs.helper;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.app.AlertDialog;
|
||||
import android.content.Context;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.net.Uri;
|
||||
import android.os.Build;
|
||||
import android.provider.Settings;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.v4.app.ActivityCompat;
|
||||
|
||||
/**
|
||||
* Created by: UFO on: 2016/11/25.
|
||||
* <p>
|
||||
* 描述:Android 6.0权限工具类
|
||||
*/
|
||||
|
||||
public class PermissionsManager {
|
||||
|
||||
/*权限请求码*/
|
||||
public static final int REQUEST_CODE_PERMISSIONS = 10001;
|
||||
/*应用设置页面请求码*/
|
||||
public static final int REQUEST_CODE_SETTING = 10002;
|
||||
private OnPermissionsCallback mCallback;
|
||||
private String[] permissions;
|
||||
private Activity mActivity;
|
||||
private AlertDialog mAlertDialog;
|
||||
|
||||
public PermissionsManager(Activity activity) {
|
||||
mActivity = activity;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置回调
|
||||
*
|
||||
* @param callback
|
||||
*/
|
||||
public void setPermissionCallback(OnPermissionsCallback callback) {
|
||||
this.mCallback = callback;
|
||||
}
|
||||
|
||||
public String[] getPermissions() {
|
||||
return permissions;
|
||||
}
|
||||
|
||||
public void setPermissions(String[] permissions) {
|
||||
this.permissions = permissions;
|
||||
}
|
||||
|
||||
/**
|
||||
* 权限请求,结果会在@see {@link #resultPermissionsProcess(Activity, int, String[], int[])}进行处理
|
||||
*
|
||||
* @param permissionsArr
|
||||
* @deprecated @see requestPermission();
|
||||
*/
|
||||
public void requestPermission(String[] permissionsArr) {
|
||||
if (!isM()) {
|
||||
mCallback.hasPermissions();
|
||||
return;
|
||||
}
|
||||
// this.permissions = permissionsArr;
|
||||
/*判断是否有权限*/
|
||||
if (!hasSelfPermission(mActivity, this.permissions)) {
|
||||
ActivityCompat.requestPermissions(mActivity, this.permissions, REQUEST_CODE_PERMISSIONS);
|
||||
} else {
|
||||
if (null != mCallback) {
|
||||
mCallback.hasPermissions();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 发起权限申请,
|
||||
* 设置权限通过@see setPermission(permissions);
|
||||
*/
|
||||
public void requestPermission() {
|
||||
this.requestPermission(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 权限请求结果处理,处理@see {@link #requestPermission(String[])}的请求结果
|
||||
* 此方法应在{@link android.support.v4.app.FragmentActivity#onRequestPermissionsResult(int, String[], int[])}
|
||||
* 里调用
|
||||
*
|
||||
* @param requestCode
|
||||
* @param permissions
|
||||
* @param grantResults
|
||||
*/
|
||||
public void resultPermissionsProcess(Activity activity, int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
|
||||
if (requestCode == REQUEST_CODE_PERMISSIONS) {
|
||||
boolean hasAllPermissions = true;
|
||||
for (int i = 0; i < grantResults.length; i++) {
|
||||
if (grantResults[i] != PackageManager.PERMISSION_GRANTED) {
|
||||
hasAllPermissions = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (null != mCallback) {
|
||||
if (hasAllPermissions) {
|
||||
mCallback.hasPermissions();
|
||||
} else {
|
||||
mCallback.noPermissions();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/*if (ActivityCompat.shouldShowRequestPermissionRationale(activity, permissions[i])) {
|
||||
//第二次申请
|
||||
requestPermission(new String[]{permissions[i]});
|
||||
return;
|
||||
} else {
|
||||
//当第二次申请还是不允许的话就走这里了
|
||||
if (null != mCallback) {
|
||||
mCallback.noPermissions();
|
||||
}
|
||||
return;
|
||||
}*/
|
||||
|
||||
/**
|
||||
* 打开设置界面返回后处理
|
||||
*
|
||||
* @param requestCode
|
||||
* @param resultCode
|
||||
* @param data
|
||||
*/
|
||||
public void onActivityResult(int requestCode, int resultCode, Intent data) {
|
||||
if (requestCode == REQUEST_CODE_SETTING) {
|
||||
requestPermission(this.permissions);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断当前系统是不是Android 6.0.因为权限申请只有6.0以上(包括6.0才需要)
|
||||
*
|
||||
* @return 如果是返回true, 否则返回false.
|
||||
*/
|
||||
private boolean isM() {
|
||||
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.M;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断是否有指定需要请求的权限,只有有一个没有就返回false.否则返回true
|
||||
*
|
||||
* @param context
|
||||
* @param permissionsArr
|
||||
*/
|
||||
private boolean hasSelfPermission(Context context, String[] permissionsArr) {
|
||||
for (int i = 0; i < permissionsArr.length; i++) {
|
||||
if (PackageManager.PERMISSION_GRANTED != ActivityCompat.checkSelfPermission(context, permissionsArr[i])) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 显示打开应用的设置页面对话框
|
||||
*
|
||||
* @param context
|
||||
*/
|
||||
public void showOpenSettingDialog(final Activity context) {
|
||||
|
||||
if (null != mAlertDialog && mAlertDialog.isShowing()) {
|
||||
mAlertDialog.dismiss();
|
||||
mAlertDialog = null;
|
||||
}
|
||||
mAlertDialog = new AlertDialog.Builder(context).setTitle("提示")
|
||||
.setMessage("当前应用所需要的权限已经被你禁用,你只能找到-权限管理,然后手动打开所需的权限")
|
||||
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
|
||||
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
dialog.dismiss();
|
||||
openSettingActivity(context);
|
||||
}
|
||||
})
|
||||
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
dialog.dismiss();
|
||||
context.finish();
|
||||
}
|
||||
})
|
||||
.setCancelable(false)
|
||||
.show();
|
||||
}
|
||||
|
||||
/**
|
||||
* 显示打开应用的设置页面对话框 只有一个按钮的
|
||||
*
|
||||
* @param context
|
||||
*/
|
||||
public void showOpenSettingDialogSingle(final Activity context) {
|
||||
if (null != mAlertDialog && mAlertDialog.isShowing()) {
|
||||
mAlertDialog.dismiss();
|
||||
mAlertDialog = null;
|
||||
}
|
||||
mAlertDialog = new AlertDialog.Builder(context).setTitle("提示")
|
||||
.setMessage("当前应用所需要的权限已经被你禁用,你只能找到-权限管理,然后手动打开所需的权限")
|
||||
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
|
||||
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
openSettingActivity(context);
|
||||
}
|
||||
})
|
||||
.show();
|
||||
}
|
||||
|
||||
/**
|
||||
* 关闭对话框
|
||||
*/
|
||||
public void dismissDialog() {
|
||||
if (null != mAlertDialog && mAlertDialog.isShowing()) {
|
||||
mAlertDialog.dismiss();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void openSettingActivity(Activity activity) {
|
||||
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, Uri.parse("package:" + activity.getPackageName()));
|
||||
intent.addCategory(Intent.CATEGORY_DEFAULT);
|
||||
activity.startActivityForResult(intent, REQUEST_CODE_SETTING);
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否有权限回调,只有在有权限时才会执行hasPermissions()方法。
|
||||
* 当没有权限时就直接向系统申请权限
|
||||
*/
|
||||
public interface OnPermissionsCallback {
|
||||
/**
|
||||
* 当前权限时回调此方法
|
||||
*/
|
||||
void hasPermissions();
|
||||
|
||||
/**
|
||||
* 当多次申请后还是没有拿到权限时回调此方法
|
||||
*/
|
||||
void noPermissions();
|
||||
}
|
||||
}
|
||||
29
baselibs/src/main/java/com/baselibs/utils/CloseUtils.java
Normal file
29
baselibs/src/main/java/com/baselibs/utils/CloseUtils.java
Normal file
@@ -0,0 +1,29 @@
|
||||
package com.baselibs.utils;
|
||||
|
||||
import java.io.Closeable;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* 日期:2017.01.03
|
||||
* <p>
|
||||
* 作者:xudiwei
|
||||
* <p>
|
||||
* 描述:流关闭工具类
|
||||
*/
|
||||
|
||||
public class CloseUtils {
|
||||
|
||||
/**
|
||||
* 关闭流
|
||||
* @param closeable
|
||||
*/
|
||||
public static void close(Closeable closeable){
|
||||
if(null != closeable){
|
||||
try {
|
||||
closeable.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
37
baselibs/src/main/java/com/baselibs/utils/DensityUtils.java
Normal file
37
baselibs/src/main/java/com/baselibs/utils/DensityUtils.java
Normal file
@@ -0,0 +1,37 @@
|
||||
package com.baselibs.utils;
|
||||
|
||||
import android.content.Context;
|
||||
import android.util.TypedValue;
|
||||
|
||||
/**
|
||||
* 日期:2016/2/1.19:25
|
||||
* <p/>
|
||||
* 作者:XuDiWei
|
||||
* <p/>
|
||||
* 描述:密度与单位转换工具
|
||||
*/
|
||||
public class DensityUtils {
|
||||
|
||||
/**
|
||||
* dip转Px
|
||||
*
|
||||
* @param dip
|
||||
* @return
|
||||
*/
|
||||
public static float dipToPx(Context context, int dip) {
|
||||
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dip, context.getResources().getDisplayMetrics());
|
||||
// float density = context.getResources().getDisplayMetrics().density;
|
||||
// return (int) (density * dip + 0.5f);
|
||||
}
|
||||
|
||||
/**
|
||||
* sp 转px
|
||||
*
|
||||
* @param context
|
||||
* @param sp
|
||||
* @return
|
||||
*/
|
||||
public static float spToDx(Context context, int sp) {
|
||||
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, sp, context.getResources().getDisplayMetrics());
|
||||
}
|
||||
}
|
||||
295
baselibs/src/main/java/com/baselibs/utils/FileUtils.java
Normal file
295
baselibs/src/main/java/com/baselibs/utils/FileUtils.java
Normal file
@@ -0,0 +1,295 @@
|
||||
package com.baselibs.utils;
|
||||
|
||||
import android.graphics.Bitmap;
|
||||
import android.os.Environment;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Base64;
|
||||
import android.util.Log;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
|
||||
/**
|
||||
* 日期:2017.01.12
|
||||
* <p>
|
||||
* 作者:xudiwei
|
||||
* <p>
|
||||
* 描述:文件工具类
|
||||
*/
|
||||
public class FileUtils {
|
||||
private static final String TAG = "FileUtils";
|
||||
|
||||
/**
|
||||
* 根据路径创建一个在sd卡目录下的一个文件夹
|
||||
*
|
||||
* @param dirPath 文件夹名
|
||||
* @return 返回创建成功的File对象或是null.
|
||||
*/
|
||||
public static File createDirToSDCard(String dirPath) {
|
||||
if (TextUtils.isEmpty(dirPath)) {
|
||||
throw new IllegalArgumentException("dir path can not empty");
|
||||
}
|
||||
|
||||
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
|
||||
File dir; /*= Environment.getExternalStoragePublicDirectory(dirPath);*/
|
||||
dir = new File(Environment.getExternalStorageDirectory(), dirPath);
|
||||
boolean mkdirs = dir.mkdirs() || dir.isDirectory();
|
||||
return dir;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建一个文件,如果已存在则册除后再创建
|
||||
*
|
||||
* @param dir
|
||||
* @param filename
|
||||
* @return
|
||||
*/
|
||||
public static File createFile(File dir, String filename) {
|
||||
File file = new File(dir, filename);
|
||||
try {
|
||||
if (file.exists()) {
|
||||
boolean deleteResult = file.delete();
|
||||
}
|
||||
boolean createResult = file.createNewFile();
|
||||
Log.d(TAG, "createFile: " + createResult);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return file;
|
||||
}
|
||||
|
||||
/**
|
||||
* 册除一个已存在的文件
|
||||
*
|
||||
* @param dir
|
||||
* @param name
|
||||
* @return
|
||||
*/
|
||||
public static boolean deleteFile(String dir, String name) {
|
||||
File file = new File(createDirToSDCard(dir), name);
|
||||
if (file.exists() && file.isFile()) {
|
||||
boolean deleteResult = file.delete();
|
||||
Log.d(TAG, "deleteFile: " + deleteResult);
|
||||
return deleteResult;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除文件夹里面的所以文件
|
||||
*/
|
||||
public static void deleteDir(File dir) {
|
||||
if (dir.exists()) {
|
||||
File[] files = dir.listFiles();
|
||||
for (int i = 0; i < files.length; i++) {
|
||||
if (files[i].isFile()) {
|
||||
files[i].delete();
|
||||
} else {
|
||||
deleteDir(files[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 统计文件夹文件的大小
|
||||
*/
|
||||
public static long getSize(File file) {
|
||||
// 判断文件是否存在
|
||||
if (file.exists()) {
|
||||
// 如果是目录则递归计算其内容的总大小,如果是文件则直接返回其大小
|
||||
if (!file.isFile()) {
|
||||
// 获取文件大小
|
||||
File[] fl = file.listFiles();
|
||||
long ss = 0;
|
||||
for (File f : fl)
|
||||
ss += getSize(f);
|
||||
return ss;
|
||||
} else {
|
||||
long ss = (long) file.length();
|
||||
return ss; // 单位制bytes
|
||||
}
|
||||
} else {
|
||||
// System.out.println("文件或者文件夹不存在,请检查路径是否正确!");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 编码Base64图片
|
||||
*
|
||||
* @param file
|
||||
* @return
|
||||
*/
|
||||
public static String encodeImage(File file) {
|
||||
FileInputStream fis = null;
|
||||
byte[] rs = new byte[0];
|
||||
|
||||
try {
|
||||
fis = new FileInputStream(file);
|
||||
rs = new byte[fis.available()];
|
||||
fis.read(rs);
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
if (fis != null) {
|
||||
try {
|
||||
fis.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
return Base64.encodeToString(rs, Base64.DEFAULT);
|
||||
}
|
||||
|
||||
/**
|
||||
* 把一个bitmap对象保存为文件
|
||||
*
|
||||
* @param fileName
|
||||
* @param bitmap
|
||||
*/
|
||||
public static void writeBitmapToFile(String path, String fileName, Bitmap bitmap) {
|
||||
if (null == bitmap) {
|
||||
return;
|
||||
}
|
||||
if (TextUtils.isEmpty(fileName)) {
|
||||
throw new RuntimeException(" fileName is null");
|
||||
}
|
||||
FileOutputStream fos = null;
|
||||
try {
|
||||
File file = new File(createDirToSDCard(path), fileName);
|
||||
fos = new FileOutputStream(file);
|
||||
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
CloseUtils.close(fos);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 把一个String对象保存为文件
|
||||
*
|
||||
* @param fileName
|
||||
* @param str
|
||||
*/
|
||||
public static void writeStringToFile(String path, String fileName, String str) {
|
||||
if (null == str) {
|
||||
return;
|
||||
}
|
||||
if (TextUtils.isEmpty(fileName)) {
|
||||
throw new RuntimeException(" fileName is null");
|
||||
}
|
||||
FileOutputStream fos = null;
|
||||
try {
|
||||
File file = new File(createDirToSDCard(path), fileName);
|
||||
fos = new FileOutputStream(file);
|
||||
fos.write(str.getBytes());
|
||||
fos.flush();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
CloseUtils.close(fos);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据文件路径与名称读取一个字符串值
|
||||
*
|
||||
* @param path
|
||||
* @param fileName
|
||||
* @return
|
||||
*/
|
||||
public static String readStringFromFile(String path, String fileName) {
|
||||
if (TextUtils.isEmpty(path) || TextUtils.isEmpty(fileName)) {
|
||||
throw new IllegalArgumentException(" path or filename is null");
|
||||
}
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
BufferedReader br = null;
|
||||
|
||||
try {
|
||||
File file = new File(createDirToSDCard(path), fileName);
|
||||
Log.d(TAG, file.getAbsolutePath());
|
||||
Log.d(TAG, "exists : " + file.exists());
|
||||
br = new BufferedReader(new FileReader(file));
|
||||
String buf;
|
||||
while (null != (buf = br.readLine())) {
|
||||
sb.append(buf);
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
CloseUtils.close(br);
|
||||
}
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 把一个对象写到文件里
|
||||
*
|
||||
* @param path
|
||||
* @param fileName
|
||||
* @param object
|
||||
*/
|
||||
public static void writeObjectToFile(String path, String fileName, Object object) {
|
||||
File file = new File(createDirToSDCard(path), fileName);
|
||||
FileOutputStream fos = null;
|
||||
ObjectOutputStream oos = null;
|
||||
try {
|
||||
fos = new FileOutputStream(file);
|
||||
oos = new ObjectOutputStream(fos);
|
||||
oos.writeObject(object);
|
||||
oos.flush();
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
CloseUtils.close(fos);
|
||||
CloseUtils.close(oos);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取一个对象
|
||||
*
|
||||
* @param path
|
||||
* @param fileName
|
||||
* @return
|
||||
*/
|
||||
public static Object readObjectFromFile(String path, String fileName) {
|
||||
File file = new File(createDirToSDCard(path), fileName);
|
||||
FileInputStream fis = null;
|
||||
ObjectInputStream ois = null;
|
||||
try {
|
||||
fis = new FileInputStream(file);
|
||||
ois = new ObjectInputStream(fis);
|
||||
Object object = ois.readObject();
|
||||
return object;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
CloseUtils.close(fis);
|
||||
CloseUtils.close(ois);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
68
baselibs/src/main/java/com/baselibs/utils/Md5Utils.java
Normal file
68
baselibs/src/main/java/com/baselibs/utils/Md5Utils.java
Normal file
@@ -0,0 +1,68 @@
|
||||
package com.baselibs.utils;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
|
||||
public class Md5Utils {
|
||||
/**
|
||||
* 加密字符串采用md5算法
|
||||
*
|
||||
* @param text
|
||||
* @return
|
||||
*/
|
||||
public static String encode(String text) {
|
||||
try {
|
||||
MessageDigest digest = MessageDigest.getInstance("md5");
|
||||
byte[] result = digest.digest(text.getBytes());
|
||||
StringBuffer sb = new StringBuffer();
|
||||
for (byte b : result) {
|
||||
String hex = Integer.toHexString(b & 0xff);//加盐
|
||||
if (hex.length() == 1) {
|
||||
sb.append("0");
|
||||
}
|
||||
sb.append(hex);
|
||||
}
|
||||
return sb.toString();
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
e.printStackTrace();
|
||||
return "";//can't reach
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文件的md5值
|
||||
*
|
||||
* @param file
|
||||
* @return
|
||||
*/
|
||||
public static String encodeFile(File file) {
|
||||
FileInputStream in;
|
||||
try {
|
||||
in = new FileInputStream(file);
|
||||
MessageDigest digester = MessageDigest.getInstance("MD5");
|
||||
byte[] bytes = new byte[8192];
|
||||
int byteCount;
|
||||
while ((byteCount = in.read(bytes)) > 0) {
|
||||
digester.update(bytes, 0, byteCount);
|
||||
}
|
||||
byte[] digest = digester.digest();
|
||||
StringBuffer sb = new StringBuffer();
|
||||
for (byte b : digest) {
|
||||
String hex = Integer.toHexString(b & 0xff);//加盐
|
||||
if (hex.length() == 1) {
|
||||
sb.append("0");
|
||||
}
|
||||
sb.append(hex);
|
||||
}
|
||||
return sb.toString();
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return "";
|
||||
}
|
||||
}
|
||||
160
baselibs/src/main/java/com/baselibs/widget/LetterBar.java
Normal file
160
baselibs/src/main/java/com/baselibs/widget/LetterBar.java
Normal file
@@ -0,0 +1,160 @@
|
||||
package com.baselibs.widget;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.Color;
|
||||
import android.graphics.Paint;
|
||||
import android.graphics.Typeface;
|
||||
import android.util.AttributeSet;
|
||||
import android.view.MotionEvent;
|
||||
import android.view.View;
|
||||
import android.widget.TextView;
|
||||
|
||||
import com.baselibs.utils.DensityUtils;
|
||||
|
||||
/**
|
||||
* 作者:xudiwei
|
||||
* <p/>
|
||||
* 日期: Administrator on 2015/8/25.
|
||||
* <p/>
|
||||
* 文本描述:字母导航View
|
||||
*/
|
||||
public class LetterBar extends View {
|
||||
|
||||
private String[] lets = {"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 Paint mPaint;
|
||||
|
||||
/**
|
||||
* 当前控件的宽度
|
||||
*/
|
||||
private int width;
|
||||
/**
|
||||
* 当前控件的高度
|
||||
*/
|
||||
private int height;
|
||||
/**
|
||||
* 当前选中字母的索引
|
||||
*/
|
||||
private int choosePoint;
|
||||
|
||||
/**
|
||||
* 文本的高度
|
||||
*/
|
||||
private int textHeight;
|
||||
|
||||
/**
|
||||
* 默认的背影色
|
||||
*/
|
||||
private final static String BACKCOLOR_DEFAULT = "#00000000";
|
||||
|
||||
/**
|
||||
* 触摸时的背影色
|
||||
*/
|
||||
private final static String BACKCOLOR_TOUCH = "#33000000";
|
||||
|
||||
/**
|
||||
* 大文本显示
|
||||
*/
|
||||
private TextView textView;
|
||||
|
||||
private onLetterSelectedListener listener;
|
||||
|
||||
public LetterBar(Context context) {
|
||||
super(context);
|
||||
}
|
||||
|
||||
public LetterBar(Context context, AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
init();
|
||||
}
|
||||
|
||||
private void init() {
|
||||
setBackgroundColor(Color.parseColor(BACKCOLOR_DEFAULT));
|
||||
mPaint = new Paint();
|
||||
mPaint.setAntiAlias(true);
|
||||
mPaint.setColor(Color.parseColor("#ff67c729"));
|
||||
mPaint.setTypeface(Typeface.DEFAULT);
|
||||
mPaint.setTextSize(DensityUtils.dipToPx(getContext(),15));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDraw(Canvas canvas) {
|
||||
width = this.getMeasuredWidth();
|
||||
height = this.getMeasuredHeight();
|
||||
//文本的宽,可以通过Paint的方法进行测量获得
|
||||
float textWidth = mPaint.measureText(lets[0]);
|
||||
//文本的高,是当前控件的高度除以字母数量的长度
|
||||
textHeight = height / lets.length;
|
||||
//文本的X轴坐标,是当前控件的宽度减去文本的宽度再除以2
|
||||
int poX = (int) ((width - textWidth) / 2);
|
||||
for (int i = 0; i < lets.length; i++) {
|
||||
if (i == choosePoint) {
|
||||
mPaint.setColor(Color.RED);
|
||||
} else {
|
||||
mPaint.setColor(Color.parseColor("#ff67c729"));
|
||||
}
|
||||
canvas.drawText(lets[i], poX, textHeight * (i + 1), mPaint);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean dispatchTouchEvent(MotionEvent event) {
|
||||
int y = (int) event.getY();
|
||||
switch (event.getAction()) {
|
||||
case MotionEvent.ACTION_UP:
|
||||
setBackgroundColor(Color.parseColor(BACKCOLOR_DEFAULT));
|
||||
//当手指松开时应该把选中字母索引的变量设置-1表示不选中任何一个
|
||||
choosePoint = -1;
|
||||
if (null != textView) {
|
||||
textView.setVisibility(View.INVISIBLE);
|
||||
}
|
||||
invalidate();
|
||||
break;
|
||||
default:
|
||||
choosePoint = y / textHeight;
|
||||
setBackgroundColor(Color.parseColor(BACKCOLOR_TOUCH));
|
||||
if (null != textView) {
|
||||
if (choosePoint >= 0 && choosePoint < lets.length) {
|
||||
//根据计算出来的索引获取字字母
|
||||
String let = lets[choosePoint];
|
||||
textView.setText(let);
|
||||
textView.setVisibility(View.VISIBLE);
|
||||
//回调
|
||||
if (null != listener){
|
||||
listener.onLetterSelected(let);
|
||||
System.out.println(listener);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
invalidate();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 大文本显示
|
||||
* @param textView
|
||||
*/
|
||||
public void setTextView(TextView textView) {
|
||||
this.textView = textView;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置字母选中回调方法
|
||||
* @param listener
|
||||
*/
|
||||
public void setOnLetterSelectedListener(onLetterSelectedListener listener){
|
||||
this.listener = listener;
|
||||
}
|
||||
|
||||
/**
|
||||
* 回调接口
|
||||
*/
|
||||
public interface onLetterSelectedListener{
|
||||
void onLetterSelected(String letter);
|
||||
}
|
||||
}
|
||||
108
baselibs/src/main/java/com/baselibs/widget/RoundImageView.java
Normal file
108
baselibs/src/main/java/com/baselibs/widget/RoundImageView.java
Normal file
@@ -0,0 +1,108 @@
|
||||
package com.baselibs.widget;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.res.TypedArray;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.Matrix;
|
||||
import android.graphics.Paint;
|
||||
import android.graphics.PorterDuff;
|
||||
import android.graphics.PorterDuffXfermode;
|
||||
import android.graphics.RectF;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.util.AttributeSet;
|
||||
import android.widget.ImageView;
|
||||
|
||||
import com.antiphon.baselibs.R;
|
||||
|
||||
|
||||
/**
|
||||
* 作者: XuDiWei
|
||||
* <p/>
|
||||
* 日期:2014/7/20 17:06.
|
||||
* <p/>
|
||||
* 文件描述:圆角ImageView
|
||||
*
|
||||
* @attr ref R.styleable.RoundImageView_degree 图片的角度(int值,不用给单位)
|
||||
*
|
||||
*/
|
||||
public class RoundImageView extends ImageView {
|
||||
|
||||
/**
|
||||
* 圆角角度
|
||||
*/
|
||||
private int degree;
|
||||
private Matrix mMatrix = new Matrix();
|
||||
private Paint mPaint = new Paint();
|
||||
|
||||
public RoundImageView(Context context) {
|
||||
super(context);
|
||||
}
|
||||
|
||||
public RoundImageView(Context context, AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.RoundImageView);
|
||||
degree = typedArray.getInt(R.styleable.RoundImageView_degree, 0);
|
||||
typedArray.recycle();
|
||||
}
|
||||
|
||||
public RoundImageView(Context context, AttributeSet attrs, int defStyleAttr) {
|
||||
super(context, attrs, defStyleAttr);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDraw(Canvas canvas) {
|
||||
Bitmap roundBitmap = createRoundBitmap();
|
||||
if (null != roundBitmap) {
|
||||
canvas.drawBitmap(roundBitmap, mMatrix, mPaint);
|
||||
roundBitmap.recycle();
|
||||
}
|
||||
|
||||
//这种方法好像有锯齿
|
||||
// setImageBitmap(roundBitmap);
|
||||
// super.onDraw(canvas);
|
||||
|
||||
}
|
||||
|
||||
private Bitmap createRoundBitmap() {
|
||||
//获取drawable然后转为bitmap
|
||||
Drawable drawable = getDrawable();
|
||||
if (null == drawable) {
|
||||
return null;
|
||||
}
|
||||
int intrinsicWidth = drawable.getIntrinsicWidth();
|
||||
int intrinsicHeight = drawable.getIntrinsicHeight();
|
||||
//创建空画板,用于把drawable画成bitmap
|
||||
Bitmap bitmap = Bitmap.createBitmap(intrinsicWidth, intrinsicHeight, Bitmap.Config.ARGB_8888);
|
||||
Canvas canvas = new Canvas(bitmap);
|
||||
drawable.setBounds(0, 0, intrinsicWidth, intrinsicHeight);
|
||||
drawable.draw(canvas);
|
||||
|
||||
//转成圆角图片
|
||||
// 创建空画板,用于画一个圆角矩形和bitmap然后取交集 这里的宽高要用当前控件的宽高,不要用bitmap的宽高
|
||||
Bitmap roundBitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
|
||||
Canvas canvasRound = new Canvas(roundBitmap);
|
||||
Paint paint = new Paint();
|
||||
paint.setAntiAlias(true);
|
||||
//这里的宽高要用当前控件的宽高,不要用bitmap的宽高
|
||||
RectF rectF = new RectF(0, 0, getWidth(), getHeight());
|
||||
canvasRound.drawRoundRect(rectF, degree, degree, paint);
|
||||
//设置这个属性就可以获取后画的图与先画的矩形重叠部分显示后画的图形
|
||||
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
|
||||
canvasRound.drawBitmap(bitmap, null, rectF, paint);
|
||||
bitmap.recycle();
|
||||
return roundBitmap;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置角度
|
||||
*
|
||||
* @param degree
|
||||
*/
|
||||
public void setDegree(int degree) {
|
||||
this.degree = degree;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
8
baselibs/src/main/res/values/attrs.xml
Normal file
8
baselibs/src/main/res/values/attrs.xml
Normal file
@@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
<!--RoundImageView 自定义属性-->
|
||||
<declare-styleable name="RoundImageView">
|
||||
<!--图片的角度-->
|
||||
<attr name="degree" format="integer"/>
|
||||
</declare-styleable>
|
||||
</resources>
|
||||
3
baselibs/src/main/res/values/strings.xml
Normal file
3
baselibs/src/main/res/values/strings.xml
Normal file
@@ -0,0 +1,3 @@
|
||||
<resources>
|
||||
<string name="app_name">BaseLibs</string>
|
||||
</resources>
|
||||
Reference in New Issue
Block a user