diff --git a/README-CN.md b/README-CN.md index b7830c5f94074eac450fc27fc4bfb245277ae3da..21c28f5dee2ef2c55f5527f30430ee8d6173deb6 100644 --- a/README-CN.md +++ b/README-CN.md @@ -41,7 +41,7 @@ [logo]: https://raw.githubusercontent.com/Blankj/AndroidUtilCode/master/art/logo.png -[aucsvg]: https://img.shields.io/badge/AndroidUtilCode-v1.13.5-brightgreen.svg +[aucsvg]: https://img.shields.io/badge/AndroidUtilCode-v1.13.6-brightgreen.svg [auc]: https://github.com/Blankj/AndroidUtilCode [apisvg]: https://img.shields.io/badge/API-14+-brightgreen.svg diff --git a/README.md b/README.md index 16fcc7187814db5fbffb6802fcfcc8252ab45b54..407740dd89d2f70753108124239c4423090559f4 100644 --- a/README.md +++ b/README.md @@ -41,7 +41,7 @@ If this ptoject helps you a lot, and you would like to support this ptoject's fu [logo]: https://raw.githubusercontent.com/Blankj/AndroidUtilCode/master/art/logo.png -[aucsvg]: https://img.shields.io/badge/AndroidUtilCode-v1.13.5-brightgreen.svg +[aucsvg]: https://img.shields.io/badge/AndroidUtilCode-v1.13.6-brightgreen.svg [auc]: https://github.com/Blankj/AndroidUtilCode [apisvg]: https://img.shields.io/badge/API-14+-brightgreen.svg diff --git a/app/build.gradle b/app/build.gradle index c0176560e871b77f7a476d6b4b447380fdc71ee9..61984a4a5c25dadc9c07210c4f7d8441534b4265 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -48,7 +48,7 @@ android { dependencies { implementation fileTree(include: ['*.jar'], dir: 'libs') - implementation project(':utilcode') +// implementation project(':utilcode') implementation project(':subutil') implementation "com.android.support:appcompat-v7:$support_version" implementation "com.android.support:design:$support_version" @@ -56,7 +56,7 @@ dependencies { // LeakCanary debugImplementation "com.squareup.leakcanary:leakcanary-android:$leakcanary_version" releaseImplementation "com.squareup.leakcanary:leakcanary-android-no-op:$leakcanary_version" -// implementation 'com.blankj:utilcode:1.13.5' + implementation 'com.blankj:utilcode:1.13.6' } diff --git a/app/src/main/java/com/blankj/androidutilcode/feature/core/bar/BarStatusFragmentActivity.java b/app/src/main/java/com/blankj/androidutilcode/feature/core/bar/BarStatusFragmentActivity.java index 3d7caa95454a8cc807cf9b08bba215cbe1da06f2..537f41809ad293921e590c56b4c360c843fca35d 100644 --- a/app/src/main/java/com/blankj/androidutilcode/feature/core/bar/BarStatusFragmentActivity.java +++ b/app/src/main/java/com/blankj/androidutilcode/feature/core/bar/BarStatusFragmentActivity.java @@ -65,7 +65,7 @@ public class BarStatusFragmentActivity extends BaseActivity { mFragmentList.add(BarStatusAlphaFragment.newInstance()); mFragmentList.add(BarStatusImageViewFragment.newInstance()); - + mVpStatusBar.setOffscreenPageLimit(2); mVpStatusBar.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) { @Override public Fragment getItem(int position) { @@ -96,7 +96,6 @@ public class BarStatusFragmentActivity extends BaseActivity { }); navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener); - } @Override diff --git a/build.gradle b/build.gradle index 60e8df890429f625043519602c210330f83405eb..22a3508a9d9dad4b3b7b41bbd8460d6d839f0823 100644 --- a/build.gradle +++ b/build.gradle @@ -33,8 +33,8 @@ ext { min_sdk_version = 14 target_sdk_version = 27 - version_code = 1_013_005 - version_name = '1.13.5'// E.g 1.9.72 => 1,009,072 + version_code = 1_013_006 + version_name = '1.13.6'// E.g 1.9.72 => 1,009,072 // App dependencies support_version = '27.0.2' diff --git a/subutil/src/main/java/com/blankj/subutil/util/BitUtils.java b/subutil/src/main/java/com/blankj/subutil/util/BitUtils.java new file mode 100644 index 0000000000000000000000000000000000000000..75f7e223baa752f575efad47eb6470f6b5d086f5 --- /dev/null +++ b/subutil/src/main/java/com/blankj/subutil/util/BitUtils.java @@ -0,0 +1,115 @@ +package com.blankj.subutil.util; + +import android.util.Log; + +/** + *
+ *     author: Blankj
+ *     blog  : http://blankj.com
+ *     time  : 2018/03/21
+ *     desc  :
+ * 
+ */ +public class BitUtils { + + private BitUtils() { + throw new UnsupportedOperationException("u can't instantiate me..."); + } + + /** + * 获取运算数指定位置的值
+ * 例如: 0000 1011 获取其第 0 位的值为 1, 第 2 位 的值为 0
+ * + * @param source 需要运算的数 + * @param pos 指定位置 (0...7) + * @return 指定位置的值(0 or 1) + */ + public static byte getBitValue(byte source, int pos) { + return (byte) ((source >> pos) & 1); + + } + + + /** + * 将运算数指定位置的值置为指定值
+ * 例: 0000 1011 需要更新为 0000 1111, 即第 2 位的值需要置为 1
+ * + * @param source 需要运算的数 + * @param pos 指定位置 (0<=pos<=7) + * @param value 只能取值为 0, 或 1, 所有大于0的值作为1处理, 所有小于0的值作为0处理 + * @return 运算后的结果数 + */ + public static byte setBitValue(byte source, int pos, byte value) { + + byte mask = (byte) (1 << pos); + if (value > 0) { + source |= mask; + + } else { + source &= (~mask); + + } + return source; + } + + + /** + * 将运算数指定位置取反值
+ * 例: 0000 1011 指定第 3 位取反, 结果为 0000 0011; 指定第2位取反, 结果为 0000 1111
+ * + * @param source + * @param pos 指定位置 (0<=pos<=7) + * @return 运算后的结果数 + */ + public static byte reverseBitValue(byte source, int pos) { + byte mask = (byte) (1 << pos); + return (byte) (source ^ mask); + + } + + + /** + * 检查运算数的指定位置是否为1
+ * + * @param source 需要运算的数 + * @param pos 指定位置 (0<=pos<=7) + * @return true 表示指定位置值为1, false 表示指定位置值为 0 + */ + public static boolean checkBitValue(byte source, int pos) { + + source = (byte) (source >>> pos); + + return (source & 1) == 1; + + } + + /** + * 入口函数做测试
+ * + * @param args + */ + public static void main(String[] args) { + // 取十进制 11 (二级制 0000 1011) 为例子 + byte source = 11; + // 取第2位值并输出, 结果应为 0000 1011 + for (byte i = 7; i >= 0; i--) { + Log.d("BitUtils", getBitValue(source, i) + ""); + + } + // 将第6位置为1并输出 , 结果为 75 (0100 1011) + Log.d("BitUtils", setBitValue(source, 6, (byte) 1) + ""); + + // 将第6位取反并输出, 结果应为75(0100 1011) + Log.d("BitUtils", reverseBitValue(source, 6) + ""); + + // 检查第6位是否为1,结果应为false + Log.d("BitUtils", checkBitValue(source, 6) + ""); + + // 输出为1的位, 结果应为 0 1 3 + for (byte i = 0; i < 8; i++) { + if (checkBitValue(source, i)) { + Log.d("BitUtils", i + ""); + } + } + } +} diff --git a/subutil/src/main/java/com/blankj/subutil/util/CoordinateConvertUtils.java b/subutil/src/main/java/com/blankj/subutil/util/CoordinateConvertUtils.java new file mode 100644 index 0000000000000000000000000000000000000000..a8817cb20ed3cd814b1ac59b77bdc9fc8beca49b --- /dev/null +++ b/subutil/src/main/java/com/blankj/subutil/util/CoordinateConvertUtils.java @@ -0,0 +1,150 @@ +package com.blankj.subutil.util; + +import static java.lang.Math.PI; + +/** + *
+ *     author: Blankj
+ *     blog  : http://blankj.com
+ *     time  : 2018/03/21
+ *     desc  :
+ * 
+ */ +public class CoordinateConvertUtils { + + private final static double X_PI = 3.14159265358979324 * 3000.0 / 180.0; + private final static double A = 6378245.0; + private final static double EE = 0.00669342162296594323; + + /** + * BD09 坐标转 GCJ02 坐标 + * + * @param lng BD09 坐标纬度 + * @param lat BD09 坐标经度 + * @return GCJ02 坐标:[经度,纬度] + */ + public static double[] bd09ToGcj02(double lng, double lat) { + double x = lng - 0.0065; + double y = lat - 0.006; + double z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * X_PI); + double theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * X_PI); + double gg_lng = z * Math.cos(theta); + double gg_lat = z * Math.sin(theta); + return new double[]{gg_lng, gg_lat}; + } + + /** + * GCJ02 坐标转 BD09 坐标 + * + * @param lng GCJ02 坐标经度 + * @param lat GCJ02 坐标纬度 + * @return BD09 坐标:[经度,纬度] + */ + public static double[] gcj02ToBd09(double lng, double lat) { + double z = Math.sqrt(lng * lng + lat * lat) + 0.00002 * Math.sin(lat * X_PI); + double theta = Math.atan2(lat, lng) + 0.000003 * Math.cos(lng * X_PI); + double bd_lng = z * Math.cos(theta) + 0.0065; + double bd_lat = z * Math.sin(theta) + 0.006; + return new double[]{bd_lng, bd_lat}; + } + + /** + * GCJ02 坐标转 WGS84 坐标 + * + * @param lng GCJ02 坐标经度 + * @param lat GCJ02 坐标纬度 + * @return WGS84 坐标:[经度,纬度] + */ + public static double[] gcj02ToWGS84(double lng, double lat) { + if (outOfChina(lng, lat)) { + return new double[]{lng, lat}; + } + double dlat = transformLat(lng - 105.0, lat - 35.0); + double dlng = transformLng(lng - 105.0, lat - 35.0); + double radlat = lat / 180.0 * PI; + double magic = Math.sin(radlat); + magic = 1 - EE * magic * magic; + double sqrtmagic = Math.sqrt(magic); + dlat = (dlat * 180.0) / ((A * (1 - EE)) / (magic * sqrtmagic) * PI); + dlng = (dlng * 180.0) / (A / sqrtmagic * Math.cos(radlat) * PI); + double mglat = lat + dlat; + double mglng = lng + dlng; + return new double[]{lng * 2 - mglng, lat * 2 - mglat}; + } + + /** + * WGS84 坐标 转 GCJ02 坐标 + * + * @param lng WGS84 坐标经度 + * @param lat WGS84 坐标纬度 + * @return GCJ02 坐标:[经度,纬度] + */ + public static double[] wgs84ToGcj02(double lng, double lat) { + if (outOfChina(lng, lat)) { + return new double[]{lng, lat}; + } + double dlat = transformLat(lng - 105.0, lat - 35.0); + double dlng = transformLng(lng - 105.0, lat - 35.0); + double radlat = lat / 180.0 * PI; + double magic = Math.sin(radlat); + magic = 1 - EE * magic * magic; + double sqrtmagic = Math.sqrt(magic); + dlat = (dlat * 180.0) / ((A * (1 - EE)) / (magic * sqrtmagic) * PI); + dlng = (dlng * 180.0) / (A / sqrtmagic * Math.cos(radlat) * PI); + double mglat = lat + dlat; + double mglng = lng + dlng; + return new double[]{mglng, mglat}; + } + + /** + * BD09 坐标 转 WGS84 坐标 + * + * @param lng BD09 坐标经度 + * @param lat BD09 坐标纬度 + * @return WGS84 坐标:[经度,纬度] + */ + public static double[] bd09ToWGS84(double lng, double lat) { + double[] gcj = bd09ToGcj02(lng, lat); + return gcj02ToWGS84(gcj[0], gcj[1]); + } + + + /** + * WGS84 坐标转 BD09 坐标 + * + * @param lng WGS84 坐标经度 + * @param lat WGS84 坐标纬度 + * @return BD09 坐标:[经度,纬度] + */ + public static double[] wgs84ToBd09(double lng, double lat) { + double[] gcj = wgs84ToGcj02(lng, lat); + return gcj02ToBd09(gcj[0], gcj[1]); + } + + private static double transformLat(double lng, double lat) { + double ret = -100.0 + 2.0 * lng + 3.0 * lat + 0.2 * lat * lat + 0.1 * lng * lat + 0.2 * Math.sqrt(Math.abs(lng)); + ret += (20.0 * Math.sin(6.0 * lng * PI) + 20.0 * Math.sin(2.0 * lng * PI)) * 2.0 / 3.0; + ret += (20.0 * Math.sin(lat * PI) + 40.0 * Math.sin(lat / 3.0 * PI)) * 2.0 / 3.0; + ret += (160.0 * Math.sin(lat / 12.0 * PI) + 320 * Math.sin(lat * PI / 30.0)) * 2.0 / 3.0; + return ret; + } + + private static double transformLng(double lng, double lat) { + double ret = 300.0 + lng + 2.0 * lat + 0.1 * lng * lng + 0.1 * lng * lat + 0.1 * Math.sqrt(Math.abs(lng)); + ret += (20.0 * Math.sin(6.0 * lng * PI) + 20.0 * Math.sin(2.0 * lng * PI)) * 2.0 / 3.0; + ret += (20.0 * Math.sin(lng * PI) + 40.0 * Math.sin(lng / 3.0 * PI)) * 2.0 / 3.0; + ret += (150.0 * Math.sin(lng / 12.0 * PI) + 300.0 * Math.sin(lng / 30.0 * PI)) * 2.0 / 3.0; + return ret; + } + + /** + * 判断坐标是否不在国内 + * + * @param lng 经度 + * @param lat 纬度 + * @return 坐标是否在国内 + */ + public static boolean outOfChina(double lng, double lat) { + return lng < 72.004 || lng > 137.8347 || lat < 0.8293 || lat > 55.8271; + } +} diff --git a/subutil/src/test/java/com/blankj/subutil/util/CoordinateConvertUtilsTest.java b/subutil/src/test/java/com/blankj/subutil/util/CoordinateConvertUtilsTest.java new file mode 100644 index 0000000000000000000000000000000000000000..747d874f42747df454efb013b1aa861d5cebc9a4 --- /dev/null +++ b/subutil/src/test/java/com/blankj/subutil/util/CoordinateConvertUtilsTest.java @@ -0,0 +1,100 @@ +package com.blankj.subutil.util; + +import org.junit.Assert; +import org.junit.Test; + +import static java.lang.Math.PI; + +/** + *
+ *     author: Blankj
+ *     blog  : http://blankj.com
+ *     time  : 2018/03/22
+ *     desc  :
+ * 
+ */ +public class CoordinateConvertUtilsTest { + + // 以下为各个坐标系的 天安门坐标 + private static final double[] locationWGS84 = new double[]{116.3912022800, 39.9075017400}; + private static final double[] locationGCJ02 = new double[]{116.3973900000, 39.9088600000}; + private static final double[] locationBD09 = new double[]{116.4038206839, 39.9152478931}; + + // 以下为美国纽约坐标 + private static final double[] newyorkWGS84 = new double[]{-74.0059413000, 40.7127837000}; + + @Test + public void gcj2BD09() throws Exception { + double[] BD09 = CoordinateConvertUtils.gcj02ToBd09(locationGCJ02[0], locationGCJ02[1]); + double distance = distance(locationBD09[0], locationBD09[1], BD09[0], BD09[1]); + System.out.println("distance: " + distance); + Assert.assertTrue(distance < 10); + } + + @Test + public void bd092GCJ() { + double[] GCJ02 = CoordinateConvertUtils.bd09ToGcj02(locationBD09[0], locationBD09[1]); + double distance = distance(locationGCJ02[0], locationGCJ02[1], GCJ02[0], GCJ02[1]); + System.out.println("distance: " + distance); + Assert.assertTrue(distance < 10); + } + + @Test + public void bd092WGS() { + double[] WGS84 = CoordinateConvertUtils.bd09ToWGS84(locationBD09[0], locationBD09[1]); + double distance = distance(locationWGS84[0], locationWGS84[1], WGS84[0], WGS84[1]); + System.out.println("distance: " + distance); + Assert.assertTrue(distance < 10); + } + + @Test + public void wgs2BD09() { + double[] BD09 = CoordinateConvertUtils.wgs84ToBd09(locationWGS84[0], locationWGS84[1]); + double distance = distance(locationBD09[0], locationBD09[1], BD09[0], BD09[1]); + System.out.println("distance: " + distance); + Assert.assertTrue(distance < 10); + } + + @Test + public void wgs2GCJ() { + double[] GCJ02 = CoordinateConvertUtils.wgs84ToGcj02(locationWGS84[0], locationWGS84[1]); + double distance = distance(locationGCJ02[0], locationGCJ02[1], GCJ02[0], GCJ02[1]); + System.out.println("distance: " + distance); + Assert.assertTrue(distance < 10); + } + + @Test + public void gcj2WGS() { + double[] WGS84 = CoordinateConvertUtils.gcj02ToWGS84(locationGCJ02[0], locationGCJ02[1]); + double distance = distance(locationWGS84[0], locationWGS84[1], WGS84[0], WGS84[1]); + System.out.println("distance: " + distance); + Assert.assertTrue(distance < 10); + } + + @Test + public void gcj2WGSExactly() { + double[] WGS84 = CoordinateConvertUtils.gcj02ToWGS84(locationGCJ02[0], locationGCJ02[1]); + double distance = distance(locationWGS84[0], locationWGS84[1], WGS84[0], WGS84[1]); + System.out.println("distance: " + distance); + Assert.assertTrue(distance < 10); + } + + @Test + public void outOfChina() { + Assert.assertFalse(CoordinateConvertUtils.outOfChina(locationWGS84[0], locationWGS84[1])); + Assert.assertTrue(CoordinateConvertUtils.outOfChina(newyorkWGS84[0], newyorkWGS84[1])); + } + + public static double distance(double lngA, double latA, double lngB, double latB) { + int earthR = 6371000; + double x = Math.cos(latA * PI / 180) * Math.cos(latB * PI / 180) * Math.cos((lngA - lngB) * PI / 180); + double y = Math.sin(latA * PI / 180) * Math.sin(latB * PI / 180); + double s = x + y; + if (s > 1) + s = 1; + if (s < -1) + s = -1; + double alpha = Math.acos(s); + return alpha * earthR; + } +} \ No newline at end of file diff --git a/update_log.md b/update_log.md index 254f628ffad0d41e27c6c51dfbc5664a868398c1..8e3b82efb572bcb5d5fcc96ffb2a58f1ec97a0c2 100644 --- a/update_log.md +++ b/update_log.md @@ -1,3 +1,4 @@ +* 18/03/29 兼容 Utils 的初始化传入 application,发布 1.13.6 版本 * 18/03/20 修复 PermissionUtils 子进程的问题,发布 1.13.5 版本 * 18/03/16 新增 gradle 插件来格式化 README * 18/03/14 修复 KeyboardUtils#getContentViewInvisibleHeight,发布 1.13.4 版本 diff --git a/utilcode/README-CN.md b/utilcode/README-CN.md index 8987879b2728da058581b3c99e9842dad7df3240..b0a3897e5bd1c4b5b5da0d1d7308a030a5c0c9d0 100644 --- a/utilcode/README-CN.md +++ b/utilcode/README-CN.md @@ -2,7 +2,7 @@ Gradle: ```groovy -compile 'com.blankj:utilcode:1.13.5' +compile 'com.blankj:utilcode:1.13.6' ``` diff --git a/utilcode/README.md b/utilcode/README.md index ac07d8d4a360d0f569b87dfff1c3ca2b62fb17a9..708495ddfe90efac71246f71f45312754a4f4552 100644 --- a/utilcode/README.md +++ b/utilcode/README.md @@ -2,7 +2,7 @@ Gradle: ```groovy -compile 'com.blankj:utilcode:1.13.5' +compile 'com.blankj:utilcode:1.13.6' ``` diff --git a/utilcode/src/main/java/com/blankj/utilcode/util/Utils.java b/utilcode/src/main/java/com/blankj/utilcode/util/Utils.java index 219bac07c06c83f1f63fd8213077bce07c09b338..18f0bca0e2c60669fd325d6b977ccbd6bdfca74e 100644 --- a/utilcode/src/main/java/com/blankj/utilcode/util/Utils.java +++ b/utilcode/src/main/java/com/blankj/utilcode/util/Utils.java @@ -88,6 +88,17 @@ public final class Utils { Utils.sApplication.registerActivityLifecycleCallbacks(mCallbacks); } + /** + * Init utils. + *

Init it in the class of Application.

+ * + * @param application application + */ + public static void init(@NonNull final Application application) { + Utils.sApplication = application; + Utils.sApplication.registerActivityLifecycleCallbacks(mCallbacks); + } + /** * Return the context of Application object. *