# ViewAndUtilsApp **Repository Path**: osard/view-and-utils ## Basic Information - **Project Name**: ViewAndUtilsApp - **Description**: 一些视图和常用工具集合,需要等待构建依赖 - **Primary Language**: Unknown - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 2 - **Forks**: 0 - **Created**: 2021-08-31 - **Last Updated**: 2022-05-24 ## Categories & Tags **Categories**: Uncategorized **Tags**: Android, Java, 工具, View ## README # ViewAndUtilsApp [![License](https://img.shields.io/badge/License%20-Apache%202-337ab7.svg)](https://www.apache.org/licenses/LICENSE-2.0) [![API](https://img.shields.io/badge/API-21%2B-brightgreen.svg?style=flat)](https://android-arsenal.com/api?level=21) [![](https://jitpack.io/v/com.gitee.osard/view-and-utils.svg)](https://jitpack.io/#com.gitee.osard/view-and-utils) ### 介绍 一些视图和常用工具集合 **_文件夹及文件操作均不支持分区存储的外置文件夹,请使用应用专属文件夹,项目需要启用AndroidX支持_** ### 依赖引入 **工程的build.gradle文件添加** ``` allprojects { repositories { google() mavenCentral() //jitpack 仓库 maven { url 'https://jitpack.io' } } } ``` **APP的build.gradle文件添加** ``` dependencies { ... implementation 'com.gitee.osard:view-and-utils:1.0.0' implementation 'androidx.appcompat:appcompat:1.2.0' } ``` ### View 1. **EditTextPrihibitEmoji** 不可输入手机输入法表情的AppCompatEditText 2. **DecimalEditText** 仅能输入一个"."的AppCompatEditText,适用于金额输入 ```java //扩展属性 /** * 设置小数点后的位数 * * @param decimals 默认:2,必须≥1 */ public void setDecimals(int decimals); ``` 3. **PhotoView** 支持缩放拖拽查看的AppCompatImageView ```java //扩展属性 /** * 适用于指定view显示在界面上时 */ public void setImageBitmap(View v) /** * 适用于指定view没有显示在界面上时,如:通过inflate 转化的view * * @param width 指定宽度,像素 * @param height 指定高度,像素 */ public void setImageBitmap(View v, int width, int height); ``` ### Utils 1. **AppMetaDataUtils** meta-data 资源参数读取 ```java public class AppMetaDataUtils { /** * 从Application获取 */ public static String fromApplication(Context context, String key); /** * 从Application获取 */ public static String fromApplication(Context context, String key, String defaultValue); /** * 从activity获取 */ public static String fromActivity(Context context, Class activity, String key); /** * 从activity获取 */ public static String fromActivity(Context context, Class activity, String key, String defaultValue); /** * 从服务获取 */ public static String fromService(Context context, Class service, String key); /** * 从服务获取 */ public static String fromService(Context context, Class service, String key, String defaultValue); /** * 从广播获取 */ public static String fromReceiver(Context context, Class receiver, String key); /** * 从广播获取 */ public static String fromReceiver(Context context, Class receiver, String key, String defaultValue); ``` 2. **AppPackageInfoUtils** 获取应用的内部{versionCode}和{versionName} ```java public class AppPackageInfoUtils { /** * 获取版本号(内部识别号) */ public static int getVersionCode(Context context); /** * 获取版本号 */ public static String getVersion(Context context); /** * 获取版本号 */ public static String getVersion(Context context, @StringRes int defaultText); /** * 获取版本号 */ public static String getVersion(Context context, String defaultText); } ``` 3. **AssetsUtils** 将APP内assets文件资源释放到手机存储中 ```java public class AssetsUtils { /** * 复制Assets下指定的目录下文件和文件夹到指定的目录 *

* 注意:不支持分区存储的外置文件夹,请放置到应用专属文件夹下 * * @param context 上下文 * @param assetsPath Assets下的文件,不能在最前面添加 “/” * @param desPath 目标位置文件夹,若导出单独文件夹且需要保持目录一致则参数需要传递到 assetsFile 参数最后一级目录 * @throws IOException io错误 */ public static void doCopyFilesAndFolders(Context context, String assetsPath, String desPath) throws IOException; /** * 复制Assets下指定的目录下的单一文件到指定的目录下 *

* 注意:不支持分区存储的外置文件夹,请放置到应用专属文件夹下 * * @param context 上下文 * @param assetsFile Assets下的文件,不能在最前面添加 “/” * @param desPath 目标位置文件夹,若需要保持目录一致则参数需要传递到 assetsFile 参数最后一级目录 * @throws IOException io错误 */ public static void doCopyFile(Context context, String assetsFile, String desPath) throws IOException; } ``` 4. **Base64** Base64编解码 ```java public class Base64 { /** * 获得String的Base64编码字符串,使用utf-8作为内码进行编码 */ public static String encode(String dataStr); /** * 获得指定内码String的Base64编码 */ public static String encode(String dataStr, Charset codec); /** * 编码Base64 */ public static char[] encode(byte[] data); /** * 获得Base64编码字符串的原文,使用utf-8作为内码进行解码 */ public static String decode(String base64Str); /** * 获得Base64编码字符串的原文,使用指定codec作为内码进行解码 */ public static String decode(String base64Str, Charset codec); /** * 解码Base64 */ public static byte[] decode(char[] data); } ``` 5. **ClickLookUtils** 阻止短时间内多次点击而触发多次点击事件 ```java public class ClickLookUtils { /** * 创建工厂 * * @param view 点击的View */ public static ClickLookUtils createFactory(View view); /** * 创建工厂 * * @param viewId 点击View的ID */ public static ClickLookUtils createFactory(@IdRes int viewId); /** * 设置间隔,单位毫秒 *

* 默认200毫秒 */ public ClickLookUtils setInterval(int interval); /** * 检查是否可以再次触发点击 * * @return true: 不允许触发点击,false: 可以触发点击 */ public boolean checkLook(); } //使用 @Override public void onClick(View v) { //300毫秒内针对同一个View触发者不可响应多次 if(ClickLookUtils.createFactory(v).setInterval(300).checkLook()){ return; } } ``` 6. **DensityUtil** dp和px互相转换,以及获取屏幕宽高 ```java public class DensityUtil { /** * 根据手机的分辨率从 dp 的单位 转成为 px(像素) */ public static int dpToPx(Context context, float dpValue); /** * 根据手机的分辨率从 px(像素) 的单位 转成为 dp */ public static int pxToDp(Context context, float pxValue); /** * 屏幕宽高,像素 * * @return int[0]: 宽度; int[1]: 高度 */ public static int[] screenWidthAndHeight(Application app); /** * 屏幕宽度,像素 */ public static int screenWidth(Application app); /** * 屏幕高度,像素 */ public static int screenHeight(Application app); } ``` 7. **FileSizeUtil** 文件及文件夹大小统计 ```java public class FileSizeUtil { //获取文件大小单位为Byte的double值 public static final int SIZE_TYPE_BYTE = 1; //获取文件大小单位为KB的double值 public static final int SIZE_TYPE_KB = 2; //获取文件大小单位为MB的double值 public static final int SIZE_TYPE_MB = 3; //获取文件大小单位为GB的double值 public static final int SIZE_TYPE_GB = 4; /** * 获取指定文件或指定文件夹的指定单位的大小 * * @param filePath 文件或指定文件夹的路径 * @param sizeType 获取大小的类型1为B、2为KB、3为MB、4为GB * @return double值的大小 */ public static double getFolderOrFileSize(String filePath, int sizeType); /** * 获取指定文件或指定文件夹的指定单位的大小 * * @param file 文件或指定文件夹的File对象 * @param sizeType 获取大小的类型1为B、2为KB、3为MB、4为GB * @return double值的大小 */ public static double getFolderOrFileSize(File file, int sizeType); /** * 调用此方法自动计算指定文件或指定文件夹的大小 * * @param filePath 文件路径 * @return 计算好的带B、KB、MB、GB的字符串 */ public static String getAutoFolderOrFileSize(String filePath); } ``` 8. **ImageToPdfUtils** 图片直接生成PDF文件 ```java public class ImageToPdfUtils { public static ImageToPdfUtils createInstance(); /** * 设置预期PDF文档的宽高,像素 *

* 若图像大小超出此值,则打印图像页面以图像超出的宽或高为准 */ public ImageToPdfUtils setPdfDocumentSize(int width, int height); /** * 设置打印的图像 */ public ImageToPdfUtils setBitmaps(Bitmap... bitmaps); /** * 设置打印的图像 */ public ImageToPdfUtils setBitmaps(List bitmaps); /** * 使用图片的自身的宽高决定每页文档的宽高 *

* 设置后 {@link #setPdfDocumentSize(int, int)} 属性设置无效 */ public ImageToPdfUtils setUseBitmapWidthAndHeight(); /** * 保存图像到指定的文件 * * @param fileName 全路径,且不能是分区储存的路径 * @return 是否保存成功 */ public boolean save(String fileName); } ``` 9. **NumberFormat** 数字文本格式化 ```java public class NumberFormat { /** * 转化字符串为int类型(十进制) * * @param text 数字文本 * @param defaultValue 转换失败默认值 */ public static int intFormat(String text, int defaultValue); /** * 转化字符串为int类型 * * @param text 数字文本 * @param radix 数字文本的进制,(2、8、10、16)进制 * @param defaultValue 转换失败默认值 */ public static int intFormat(String text, int radix, int defaultValue); /** * 转化字符串为short类型(十进制) * * @param text 数字文本 * @param defaultValue 转换失败默认值 */ public static short shortFormat(String text, short defaultValue); /** * 转化字符串为short类型 * * @param text 数字文本 * @param radix 数字文本的进制,(2、8、10、16)进制 * @param defaultValue 转换失败默认值 */ public static short shortFormat(String text, int radix, short defaultValue); /** * 转化字符串为long类型(十进制) * * @param text 数字文本 * @param defaultValue 转换失败默认值 */ public static long longFormat(String text, long defaultValue); /** * 转化字符串为long类型 * * @param text 数字文本 * @param radix 数字文本的进制,(2、8、10、16)进制 * @param defaultValue 转换失败默认值 */ public static long longFormat(String text, int radix, long defaultValue); /** * 转化字符串为double类型 * * @param text 数字文本 * @param defaultValue 转换失败默认值 */ public static double doubleFormat(String text, double defaultValue); /** * 转化字符串为float类型 * * @param text 数字文本 * @param defaultValue 转换失败默认值 */ public static float floatFormat(String text, float defaultValue); /** * 转化字符串为boolean类型 * * @param text 数字文本 * @param defaultValue 转换失败默认值 */ public static boolean booleanFormat(String text, boolean defaultValue); } ``` 10. **PlayVoiceUtil** Media播放本地MP3及raw下MP3文件 ```java public class PlayVoiceUtil { public static synchronized PlayVoiceUtil getInstance(); /** * 播放声音 * * @param url 本地文件的绝对路径 */ public void playSound(String url); /** * 播放声音 * * @param url 本地文件的绝对路径 * @param completionListener 播放完成,发生错误时也会触发此回调 */ public void playSound(String url, SoundCompletion completionListener); /** * 播放声音 * * @param url 本地文件的绝对路径 * @param completionListener 播放完成 * @param errorListener 为null时,发生错误将会触发 SoundCompletion 事件,均为空时什么都不会触发 */ public void playSound(String url, SoundCompletion completionListener, SoundFail errorListener); /** * 播放声音 * * @param context 上下文 * @param rawId raw资源 (R.raw.test) */ public void playSound(Context context, @RawRes int rawId); /** * 播放声音 * * @param context 上下文 * @param rawId raw资源 (R.raw.test) * @param completionListener 播放完成,发生错误时也会触发此回调 */ public void playSound(Context context, @RawRes int rawId, SoundCompletion completionListener); /** * 播放声音 * * @param context 上下文 * @param rawId raw资源 (R.raw.test) * @param completionListener 播放完成 * @param errorListener 为null时,发生错误将会触发 SoundCompletion 事件,均为空时什么都不会触发 */ public void playSound(Context context, @RawRes int rawId, SoundCompletion completionListener, SoundFail errorListener); /** * 停止播放语音 */ public void stopSound(); } ``` 11. **PropertiesUtil** Properties配置文件的读取与保存 ```java public class PropertiesUtil { /** * Properties文件读取 * * @param path 文件路径 * @return Properties 的对象 */ public static Properties loadPropertiesFile(String path); /** * Properties文件保存 * * @param properties Properties 对象 * @param path 文件路径 * @param comments 属性列表的描述。一般传"" * @return 是否保存成功 */ public static boolean savePropertiesFile(Properties properties, String path, String comments); } ``` 12. **UUIDUtils** 数据库ID生成器(长度20及以内) ```java public class UUIDUtils { /** * id生成规则,有正负数 */ public static long longUUID(); /** * id生成规则,全正数 */ public static long absLongUUID(); } ``` 13. **ListUtils** list遍历筛选工具 ```java public class ListUtils { /** * 获取列表下全部数据,某字符串属性使用特定的拼接字符的拼接结果 * * @param list 列表数据 * @param interpolation 拼接字符 * @param iJoin 指定拼接字符串时使用的属性 * @param 对泛型列表使用 * @return 最终的拼接结果,如果属性都为空,则返回空字符串 */ public static String join(List list, String interpolation, IJoin iJoin); /** * 按指定字符拆分字符串 * * @param content 原始数据 * @param regex 拆分字符 * @return 最终的拆分结果,如果原始数据为空,则返回null */ public static List split(String content, String regex); /** * 获取列表下全部数据,某整型属性的和 * * @param iSum 指定求和使用的属性 * @return 最终和 */ public static int getDataIntSum(List list, IIntSum iSum); /** * 获取列表下全部数据,某浮点类型属性的和 * * @param iSum 指定求和使用的属性 * @return 最终和 */ public static int getDataDoubleSum(List list, IDoubleSum iSum); /** * 根据指定条件过滤符合条件的数据 * * @param iWhere 指定的条件表达式 * @return 符合条件的数据, 不会为null */ public static List where(List list, IWhere iWhere); /** * 根据原始数据列表转换成目标数据列表 */ public static List convert(List list, IConvert iConvert); } ``` License ------- Copyright 2021 mjsoftking Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.