Android 开发 常用工具类收藏(随时更新)

说在前面,以下工具类可以大大的提高安卓开发者的开发效率

1、偏好设置(ShareUtils)

import android.content.Context;
import android.content.SharedPreferences;

/**
 * Content:偏好设置工具类
 * Actor:韩小呆 ヾ(゚▽゚)ノ
 * Time:  15:44
 */
public class ShareUtils {
    public static final String NAME = "config.hxd";

    /**
     * 存入数据(String)
     */
    public static void putString(Context context, String key, String value) {
        //获得偏好设置
        SharedPreferences spf = context.getSharedPreferences(NAME, Context.MODE_PRIVATE);
        //向偏好设置内部添加数据并且提交
        spf.edit().putString(key, value).apply();
    }

    /**
     * 取数据(上下文,钥匙,未取得数据返回的默认值)(String)
     */
    public static String getString(Context context, String key, String value) {
        //获得偏好设置
        SharedPreferences spf = context.getSharedPreferences(NAME, Context.MODE_PRIVATE);
        //返回需要取得的数据
        return spf.getString(key, value);
    }

    /**
     * 存入数据(Int)
     */
    public static void putInt(Context context, String key, int value) {
        //获得偏好设置
        SharedPreferences spf = context.getSharedPreferences(NAME, Context.MODE_PRIVATE);
        //向偏好设置内部添加数据并且提交
        spf.edit().putInt(key, value).apply();
    }

    /**
     * 取数据(上下文,钥匙,未取得数据返回的默认值)(Int)
     */
    public static int getInt(Context context, String key, int value) {
        //获得偏好设置
        SharedPreferences spf = context.getSharedPreferences(NAME, Context.MODE_PRIVATE);
        //返回需要取得的数据
        return spf.getInt(key, value);
    }

    /**
     * 存入数据(Int)
     */
    public static void putBoolean(Context context, String key, boolean value) {
        //获得偏好设置
        SharedPreferences spf = context.getSharedPreferences(NAME, Context.MODE_PRIVATE);
        //向偏好设置内部添加数据并且提交
        spf.edit().putBoolean(key, value).apply();
    }

    /**
     * 取数据(上下文,钥匙,未取得数据返回的默认值)(Int)
     */
    public static boolean getBoolean(Context context, String key, boolean value) {
        //获得偏好设置
        SharedPreferences spf = context.getSharedPreferences(NAME, Context.MODE_PRIVATE);
        //返回需要取得的数据
        return spf.getBoolean(key, value);
    }

    /**
     * 删除单个偏好设置
     */
    public static void deleteShare(Context context, String key) {
        //获得偏好设置
        SharedPreferences spf = context.getSharedPreferences(NAME, Context.MODE_PRIVATE);
        spf.edit().remove(key).apply();
    }

    /**
     * 删除所有偏好设置
     */
    public static void deleteShareAll(Context context, String key) {
        //获得偏好设置
        SharedPreferences spf = context.getSharedPreferences(NAME, Context.MODE_PRIVATE);
        spf.edit().clear().apply();
    }
}

鸿神的偏好设置工具类

具体地址为: https://blog.csdn.net/lmj623565791/article/details/38965311

import java.lang.reflect.InvocationTargetException;  
import java.lang.reflect.Method;  
import java.util.Map;  
  
import android.content.Context;  
import android.content.SharedPreferences;  
  
public class SPUtils  
{  
    /** 
     * 保存在手机里面的文件名 
     */  
    public static final String FILE_NAME = "share_data";  
  
    /** 
     * 保存数据的方法,我们需要拿到保存数据的具体类型,然后根据类型调用不同的保存方法 
     *  
     * @param context 
     * @param key 
     * @param object 
     */  
    public static void put(Context context, String key, Object object)  
    {  
  
        SharedPreferences sp = context.getSharedPreferences(FILE_NAME,  
                Context.MODE_PRIVATE);  
        SharedPreferences.Editor editor = sp.edit();  
  
        if (object instanceof String)  
        {  
            editor.putString(key, (String) object);  
        } else if (object instanceof Integer)  
        {  
            editor.putInt(key, (Integer) object);  
        } else if (object instanceof Boolean)  
        {  
            editor.putBoolean(key, (Boolean) object);  
        } else if (object instanceof Float)  
        {  
            editor.putFloat(key, (Float) object);  
        } else if (object instanceof Long)  
        {  
            editor.putLong(key, (Long) object);  
        } else  
        {  
            editor.putString(key, object.toString());  
        }  
  
        SharedPreferencesCompat.apply(editor);  
    }  
  
    /** 
     * 得到保存数据的方法,我们根据默认值得到保存的数据的具体类型,然后调用相对于的方法获取值 
     *  
     * @param context 
     * @param key 
     * @param defaultObject 
     * @return 
     */  
    public static Object get(Context context, String key, Object defaultObject)  
    {  
        SharedPreferences sp = context.getSharedPreferences(FILE_NAME,  
                Context.MODE_PRIVATE);  
  
        if (defaultObject instanceof String)  
        {  
            return sp.getString(key, (String) defaultObject);  
        } else if (defaultObject instanceof Integer)  
        {  
            return sp.getInt(key, (Integer) defaultObject);  
        } else if (defaultObject instanceof Boolean)  
        {  
            return sp.getBoolean(key, (Boolean) defaultObject);  
        } else if (defaultObject instanceof Float)  
        {  
            return sp.getFloat(key, (Float) defaultObject);  
        } else if (defaultObject instanceof Long)  
        {  
            return sp.getLong(key, (Long) defaultObject);  
        }  
  
        return null;  
    }  
  
    /** 
     * 移除某个key值已经对应的值 
     * @param context 
     * @param key 
     */  
    public static void remove(Context context, String key)  
    {  
        SharedPreferences sp = context.getSharedPreferences(FILE_NAME,  
                Context.MODE_PRIVATE);  
        SharedPreferences.Editor editor = sp.edit();  
        editor.remove(key);  
        SharedPreferencesCompat.apply(editor);  
    }  
  
    /** 
     * 清除所有数据 
     * @param context 
     */  
    public static void clear(Context context)  
    {  
        SharedPreferences sp = context.getSharedPreferences(FILE_NAME,  
                Context.MODE_PRIVATE);  
        SharedPreferences.Editor editor = sp.edit();  
        editor.clear();  
        SharedPreferencesCompat.apply(editor);  
    }  
  
    /** 
     * 查询某个key是否已经存在 
     * @param context 
     * @param key 
     * @return 
     */  
    public static boolean contains(Context context, String key)  
    {  
        SharedPreferences sp = context.getSharedPreferences(FILE_NAME,  
                Context.MODE_PRIVATE);  
        return sp.contains(key);  
    }  
  
    /** 
     * 返回所有的键值对 
     *  
     * @param context 
     * @return 
     */  
    public static Map<String, ?> getAll(Context context)  
    {  
        SharedPreferences sp = context.getSharedPreferences(FILE_NAME,  
                Context.MODE_PRIVATE);  
        return sp.getAll();  
    }  
  
    /** 
     * 创建一个解决SharedPreferencesCompat.apply方法的一个兼容类 
     *  
     * @author zhy 
     *  
     */  
    private static class SharedPreferencesCompat  
    {  
        private static final Method sApplyMethod = findApplyMethod();  
  
        /** 
         * 反射查找apply的方法 
         *  
         * @return 
         */  
        @SuppressWarnings({ "unchecked", "rawtypes" })  
        private static Method findApplyMethod()  
        {  
            try  
            {  
                Class clz = SharedPreferences.Editor.class;  
                return clz.getMethod("apply");  
            } catch (NoSuchMethodException e)  
            {  
            }  
  
            return null;  
        }  
  
        /** 
         * 如果找到则使用apply执行,否则使用commit 
         *  
         * @param editor 
         */  
        public static void apply(SharedPreferences.Editor editor)  
        {  
            try  
            {  
                if (sApplyMethod != null)  
                {  
                    sApplyMethod.invoke(editor);  
                    return;  
                }  
            } catch (IllegalArgumentException e)  
            {  
            } catch (IllegalAccessException e)  
            {  
            } catch (InvocationTargetException e)  
            {  
            }  
            editor.commit();  
        }  
    }  
  
}  

2、单位转换和屏幕尺寸获取工具类

import android.app.Activity;
import android.content.Context;
import android.util.DisplayMetrics;

/**
 * Content:单位转换和屏幕尺寸获取工具类
 * Actor:韩小呆 ヾ(゚▽゚)ノ
 * Time:  15:44
 */

public class DensityUtil {

    /**
     * 将px值转换为 dip或dp值,保证尺寸大小不变
     *
     * @param pxValue 像素值
     * @param context Context 对象
     * @return dp值
     */
    public static int px2dip(float pxValue, Context context) {
        float scale = getDensity(context);
        return (int) (pxValue / scale + 0.5f);
    }

    /**
     * 将dip或dp值转换为px值,保证尺寸大小不变
     *
     * @param dipValue dip数值
     * @param context  Context 对象
     * @return 像素值
     */
    public static int dip2px(float dipValue, Context context) {
        float scale = getDensity(context);
        return (int) (dipValue * scale + 0.5f);
    }

    /**
     * 将px值转换为sp值,保证文字大小不变
     *
     * @param pxValue 像素值
     * @param context Context 对象
     * @return 返回sp数值
     */
    public static int px2sp(float pxValue, Context context) {
        float scale = getDensity(context);

        return (int) (pxValue / scale + 0.5f);
    }

    /**
     * 将sp值转换为px值,保证文字大小不变
     *
     * @param spValue sp数值
     * @param context Context 对象
     * @return 返回像素值
     */
    public static int sp2px(float spValue, Context context) {
        float scale = getDensity(context);
        return (int) (spValue * scale + 0.5f);
    }

    /**
     * 取得手机屏幕的密度
     *
     * @param context 上下文
     * @return 手机屏幕的密度
     */
    public static float getDensity(Context context) {
        float scale = context.getResources().getDisplayMetrics().density;
        return scale;
    }

    /**
     * 获取屏幕高
     * @param activity
     * @return 手机屏幕的高度
     */
    public static int getScreenHeight(Activity activity) {
        DisplayMetrics dm = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
        return dm.heightPixels;
    }

    /**
     * 获取屏幕宽
     * @param activity
     * @return 手机屏幕的宽度
     */
    public static int getScreenWidth(Activity activity) {
        DisplayMetrics dm = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
        return dm.widthPixels;
    }
}

3、时间转换工具类

import android.annotation.SuppressLint;
import android.text.format.Time;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Content:时间转换工具类
 * Actor:韩小呆 ヾ(゚▽゚)ノ
 * Time:  15:44
 */
public class DateUtils {

    /**
     * 获取当前时间
     *
     * @return 年月日时分秒字符串
     */
    public static String getCurrentTime() {
        Time time = new Time("GMT+8");
        time.setToNow();
        String year = "" + time.year;
        int imonth = time.month + 1;
        String month = imonth > 9 ? "" + imonth : "0" + imonth;
        String day = time.monthDay > 9 ? "" + time.monthDay : "0"
                + time.monthDay;
        String hour = (time.hour + 8) > 9 ? "" + (time.hour + 8) : "0"
                + (time.hour + 8);
        String minute = time.minute > 9 ? "" + time.minute : "0" + time.minute;
        String sec = time.second > 9 ? "" + time.second : "0" + time.second;
        String currentTime = year + month + day + hour + minute + sec;
        return currentTime;
    }

    //获取 日期/时/分/秒
    @SuppressLint("SimpleDateFormat")
    public static String getDateTime(long time) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date = sdf.format(new Date(time));
        return date;
    }

    //获取 日期/时/分
    @SuppressLint("SimpleDateFormat")
    public static String getHourMinute(long time) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日HH时mm分");
        String date = sdf.format(new Date(time));
        return date;
    }

    //获取 日期年月日
    @SuppressLint("SimpleDateFormat")
    public static String getYearMonthDay(long time) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
        String date = sdf.format(new Date(time));
        return date;
    }

    //获取日期年月
    public static String getYearMonth(long time){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月");
        String date = sdf.format(new Date(time));
        return date;
    }

    //获取日期年月
    public static String getYearMonth2(long time){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        String date = sdf.format(new Date(time));
        return date;
    }

    //获取日期年
    public static String getYear(long time){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年");
        String date = sdf.format(new Date(time));
        return date;
    }

    //获取  时/分
    public static String getTime(long time) {
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
        String date = sdf.format(new Date(time));
        return date;
    }

}

4、验证码倒计时及重新发送验证码的工具类

import android.os.CountDownTimer;
import android.widget.Button;

/**
 * Content:验证码倒计时及重新发送验证码的工具类
 * Actor:韩小呆 ヾ(゚▽゚)ノ
 * Time:  15:44
 */

public class MyCountDownTimer extends CountDownTimer {
    private Button mBtn;
    private int mEnable;
    private int mDisable;

    /**
     * @param millisInFuture    The number of millis in the future from the call
     *                          to {@link #start()} until the countdown is done and {@link #onFinish()}
     *                          is called.
     * @param countDownInterval The interval along the way to receive
     *                          {@link #onTick(long)} callbacks.
     */
    public MyCountDownTimer(long millisInFuture, long countDownInterval, Button btn, int enable, int disable) {
        super(millisInFuture, countDownInterval);
        mBtn = btn;
        mEnable = enable;
        mDisable = disable;
    }

    //计时过程
    @Override
    public void onTick(long l) {
        //防止计时过程中重复点击
        mBtn.setClickable(false);
        mBtn.setText(l / 1000 + "s");
        //设置按钮背景色
        mBtn.setBackgroundResource(mDisable);
    }

    //计时完毕的方法
    @Override
    public void onFinish() {
        //重新给Button设置文字
        mBtn.setText("重新获取验证码");
        //设置可点击
        mBtn.setClickable(true);
        //设置按钮背景色
        mBtn.setBackgroundResource(mEnable);
    }
}

6、进度Dialog工具类

import android.app.ProgressDialog;
import android.content.Context;

/**
 * Content:进度Dialog工具类
 * Actor:韩小呆 ヾ(゚▽゚)ノ
 * Time:  15:44
 */

public class ProgressDlgUtil {
    private static ProgressDialog progressDlg = null;

    /**
     * 启动进度条
     * @param ctx 当前的activity
     * @param content 进度条显示的信息
     */
    public static void show(Context ctx, String content) {
        if (null == progressDlg) {
            progressDlg = new ProgressDialog(ctx);
            progressDlg.setProgressStyle(ProgressDialog.STYLE_SPINNER);// 设置进度条的形式为圆形转动的进度条
            //progressDlg.setTitle("提示");//设置进度条标题
            progressDlg.setMessage(content);//提示的消息
            progressDlg.setIndeterminate(false);
            progressDlg.setCancelable(false);
            //progressDlg.setIcon(R.drawable.ic_launcher_scale);
            progressDlg.show();
        }
    }

    /**
     * 结束进度条
     */
    public static void dismiss() {
        if (null != progressDlg) {
            progressDlg.dismiss();
            progressDlg = null;
        }
    }
}

7、吐司工具类

import android.annotation.SuppressLint;
import android.content.Context;
import android.widget.Toast;

/**
 * Content:ToastUtils
 * Actor:韩小呆 ヾ(゚▽゚)ノ
 * Time: 2018/5/17 17:37
 */
public class ToastUtils {

    private ToastUtils() {
        throw new UnsupportedOperationException("禁止实例化");
    }

    private static boolean isShow = true;
    private static Toast toast;

    /**
     * 短时间显示Toast
     *
     * @param context 上下文
     * @param msg     显示内容
     */
    @SuppressLint("ShowToast")
    public static void showToast(Context context, String msg) {
        if (isShow && toast == null) {
            //第一次创建
            toast = Toast.makeText(context, msg, Toast.LENGTH_SHORT);
        } else {
            //如果toast存在,只修改文字,防止点的次数多一直弹出
            toast.setText(msg);
        }
        toast.show();
    }

    /**
     * 短时间显示Toast
     *
     * @param context  上下文
     * @param msgResID 显示内容
     */
    @SuppressLint("ShowToast")
    public static void showToast(Context context, int msgResID) {

        if (isShow && toast == null) {
            toast = Toast.makeText(context, msgResID, Toast.LENGTH_SHORT);
        } else {
            toast.setText(msgResID);
        }
        toast.show();
    }


    /**
     * 长时间显示Toast
     *
     * @param context 上下文
     * @param message 显示内容
     */
    @SuppressLint("ShowToast")
    public static void showLong(Context context, CharSequence message) {
        if (isShow && toast == null) {
            toast = Toast.makeText(context, message, Toast.LENGTH_LONG);
        } else {
            toast.setText(message);
        }
        toast.show();
    }

    /**
     * 长时间显示Toast
     *
     * @param context 上下文
     * @param message 显示内容
     */
    @SuppressLint("ShowToast")
    public static void showLong(Context context, int message) {
        if (isShow && toast == null) {
            toast = Toast.makeText(context, message, Toast.LENGTH_LONG);
        } else {
            toast.setText(message);
        }
        toast.show();
    }

    /**
     * 自定义显示Toast时间
     *
     * @param context  上下文
     * @param message  显示内容
     * @param duration 显示时间
     */
    public static void show(Context context, CharSequence message, int duration) {
        if (isShow) Toast.makeText(context, message, duration).show();
    }

    /**
     * 自定义显示Toast时间
     *
     * @param context  上下文
     * @param message  显示内容
     * @param duration 显示时间
     */
    public static void show(Context context, int message, int duration) {
        if (isShow) Toast.makeText(context, message, duration).show();
    }

8、软键盘操作工具类

mport android.content.Context;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

/**
 * Content:打开关闭软键盘
 * Actor:韩小呆 ヾ(゚▽゚)ノ
 * Time: 2018/5/17 17:37
 */
public class KeyBoardUtils {
    /**
     * 打卡软键盘
     *
     * @param mEditText 输入框
     * @param mContext  上下文
     */
    public static void openKeyBord(EditText mEditText, Context mContext) {
        InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
        assert imm != null;
        imm.showSoftInput(mEditText, InputMethodManager.RESULT_SHOWN);
        imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
    }

    /**
     * 关闭软键盘
     *
     * @param mEditText 输入框
     * @param mContext  上下文
     */
    public static void closeKeyBord(Context mContext ,  EditText mEditText ) {
        InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
        assert imm != null;
        imm.hideSoftInputFromWindow(mEditText.getWindowToken(), 0);
    }

    /**
     * 判断软键盘是否弹出
     *
     * @param context 上下文
     * @param v       控件
     * @return 是否关闭
     */
    public static boolean isSHowKeyboard(Context context, View v) {
        InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        assert imm != null;
        if (imm.hideSoftInputFromWindow(v.getWindowToken(), 0)) {
            imm.showSoftInput(v, 0);
            return true;
            //软键盘已弹出
        } else {
            return false;
            //软键盘未弹出
        }
    }

}

9、网络相关工具类

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

/**
 * Content:网络相关判断操作
 * Actor:鸿神
 * Time:2018/3/30
 */

public class NetUtils {

    private NetUtils() {
        throw new UnsupportedOperationException("cannot be instantiated");
    }

    /**
     * 判断网络是否连接
     *
     * @param context
     * @return
     */
    public static boolean isConnected(Context context) {
        ConnectivityManager connectivity = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);

        if (null != connectivity) {
            NetworkInfo info = connectivity.getActiveNetworkInfo();
            if (null != info && info.isConnected()) {
                if (info.getState() == NetworkInfo.State.CONNECTED) {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * 判断是否是wifi连接
     */
    public static boolean isWifi(Context context) {

        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context
                .CONNECTIVITY_SERVICE);
        if (cm == null) {
            return cm.getActiveNetworkInfo().getType() == ConnectivityManager.TYPE_WIFI;
        }
        return false;

    }

    /**
     * 打开网络设置界面
     */
    public static void openSetting(Activity activity) {
        Intent intent = new Intent("/");
        ComponentName cm = new ComponentName("com.android.settings",
                "com.android.settings.WirelessSettings");
        intent.setComponent(cm);
        intent.setAction("android.intent.action.VIEW");
        activity.startActivityForResult(intent, 0);
    }
}

10、dp、px、sp之间的转换工具类

import android.content.Context;
import android.util.DisplayMetrics;

/**
 * Used 尺寸转换工具类(全)
 */
public class DensityUtil {
    public static float RATIO = 0.95F;//缩放比例值

    /**
     * px 转 dp【按照一定的比例】*/
    public static int px2dipRatio(Context context, float pxValue) {
        float scale = getScreenDendity(context) * RATIO;
        return (int)((pxValue / scale) + 0.5f);
    }

    /**
     * dp转px【按照一定的比例】*/
    public static int dip2pxRatio(Context context, float dpValue) {
        float scale = getScreenDendity(context) * RATIO;
        return (int)((dpValue * scale) + 0.5f);
    }

    /**
     * px 转 dp
     * 48px - 16dp
     * 50px - 17dp*/
    public static int px2dip(Context context, float pxValue) {
        float scale = getScreenDendity(context);
        return (int)((pxValue / scale) + 0.5f);
    }

    /**
     * dp转px
     * 16dp - 48px
     * 17dp - 51px*/
    public static int dip2px(Context context, float dpValue) {
        float scale = getScreenDendity(context);
        return (int)((dpValue * scale) + 0.5f);
    }

    /**获取屏幕的宽度(像素)*/
    public static int getScreenWidth(Context context) {
        return context.getResources().getDisplayMetrics().widthPixels;//1080
    }

    /**获取屏幕的宽度(dp)*/
    public static int getScreenWidthDp(Context context) {
        float scale = getScreenDendity(context);
        return (int)(context.getResources().getDisplayMetrics().widthPixels / scale);//360
    }

    /**获取屏幕的高度(像素)*/
    public static int getScreenHeight(Context context) {
        return context.getResources().getDisplayMetrics().heightPixels;//1776
    }

    /**获取屏幕的高度(像素)*/
    public static int getScreenHeightDp(Context context) {
        float scale = getScreenDendity(context);
        return (int)(context.getResources().getDisplayMetrics().heightPixels / scale);//592
    }
    /**屏幕密度比例*/
    public static float getScreenDendity(Context context){
        return context.getResources().getDisplayMetrics().density;//3
    }

    /**获取状态栏的高度 72px
     * http://www.2cto.com/kf/201501/374049.html*/
    public static int getStatusBarHeight(Context context) {
        int statusHeight = -1;
        try {
            Class<?> aClass = Class.forName("com.android.internal.R$dimen");
            Object object = aClass.newInstance();
            int height = Integer.parseInt(aClass.getField("status_bar_height").get(object).toString());
            statusHeight = context.getResources().getDimensionPixelSize(height);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return statusHeight;

        //依赖于WMS(窗口管理服务的回调)【不建议使用】
        /*Rect outRect = new Rect();
        ((Activity)context).getWindow().getDecorView().getWindowVisibleDisplayFrame(outRect);
        return outRect.top;*/
    }

    /**
     * 指定机型(displayMetrics.xdpi)下dp转px
     * 18dp - 50px*/
    public static int dpToPx(Context context, int dp) {
        return Math.round(((float)dp * getPixelScaleFactor(context)));
    }

    /**
     * 指定机型(displayMetrics.xdpi)下px 转 dp
     * 50px - 18dp*/
    public static int pxToDp(Context context, int px) {
        return Math.round(((float)px / getPixelScaleFactor(context)));
    }

    /**获取水平方向的dpi的密度比例值
     * 2.7653186*/
    public static float getPixelScaleFactor(Context context) {
        DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
        return (displayMetrics.xdpi / 160.0f);
    }
}

11、自用Log工具类

import android.util.Log;

/**
 * Content:
 * Actor:韩小呆 ヾ(゚▽゚)ノ
 * Time:  2018/6/29 9:31
 * Update:
 * Time: 2018/6/29 9:31
 */
public class LogUtils {
    private static final String TAG = "HXD";

    private static final int LOG_LEVEL_VERBOSE = 0;
    private static final int LOG_LEVEL_DEBUG = 1;
    private static final int LOG_LEVEL_INFO = 2;
    private static final int LOG_LEVEL_WARN = 3;
    private static final int LOG_LEVEL_ERROR = 4;

    private static int outputLevel = LOG_LEVEL_VERBOSE;

    private LogUtils() {
        super();
    }

    /**
     * 控制原生Log是否打印
     * level>4 则不打印原生     日志,反之则打印原生日志
     *
     * @param level 控制标志位
     */
    public static void setOutputLevel(int level) {
        outputLevel = level;
    }

    public static void v(String tag, String msg) {
        if (outputLevel <= LOG_LEVEL_VERBOSE) {
            Log.v(tag, msg);
        }
    }

    public static void v(String msg) {
        v(TAG, msg);
    }

    public static void d(String tag, String msg) {
        if (outputLevel <= LOG_LEVEL_DEBUG) {
            Log.d(tag, msg);
        }
    }

    public static void d(String msg) {
        d(TAG, msg);
    }

    public static void i(String tag, String msg) {
        if (outputLevel <= LOG_LEVEL_INFO) {
            Log.i(tag, msg);
        }
    }

    public static void i(String msg) {
        i(TAG, msg);
    }

    public static void w(String tag, String msg) {
        if (outputLevel <= LOG_LEVEL_WARN) {
            Log.w(tag, msg);
        }
    }

    public static void w(String msg) {
        w(TAG, msg);
    }

    public static void e(String tag, String msg) {
        if (outputLevel <= LOG_LEVEL_ERROR) {
            Log.e(tag, msg);
        }
    }

    public static void e(String msg) {
        e(TAG, msg);
    }

}

12、文字变色工具类

import android.content.Context;
import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.widget.TextView;

/**
 * Content: 一段文字多种颜色
 * Actor:韩小呆 ヾ(゚▽゚)ノ
 * Time:  2018/7/23 17:34
 * Update:
 * Time:
 */
public class TextColorChangeUtils {
    /***
     *通过Resources内的颜色实现文字变色龙
     *
     * @param context 上下文
     * @param textView 文字变色控件
     * @param textColor  文字固有颜色
     * @param startColor 开始半段文字color
     * @param endColor 结束半段 文字color
     * @param startStart 前半段开始变色文字下标
     * @param startEnd 前半段结束变色文字下标
     * @param endStart 后半段开始变色文字下标
     * @param endEnd 后半段结束变色文字下标
     * @param text 变色的文字内容
     * @return 返回变色结果
     */

    public static TextView interTextColorForResources(Context context, TextView textView, int textColor, int startColor,
                                                      int endColor, int startStart, int startEnd, int endStart, int endEnd, String text) {
        textView.setTextColor(context.getResources().getColor(textColor));
        SpannableStringBuilder style = new SpannableStringBuilder(text);
        style.setSpan(new ForegroundColorSpan(context.getResources().getColor(startColor)), startStart, startEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        style.setSpan(new ForegroundColorSpan(context.getResources().getColor(endColor)), endStart, endEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        textView.setText(style);
        return textView;
    }

    /**
     * 通过 ParseColor 的形式实现变色
     *
     * @param textView   文字变色控件
     * @param textColor  文字固有颜色
     * @param startColor 开始半段文字color
     * @param endColor   结束半段文字color
     * @param startStart 前半段开始变色文字下标
     * @param startEnd   后半段开始变色文字下标
     * @param endStart   后半段结束变色文字下标
     * @param endEnd     后半段结束变色文字下标
     * @param text       变色的文字内容
     * @return 返回变色结果
     */
    public static TextView interTextColorForParseColor(TextView textView, String textColor, String startColor, String endColor, int startStart, int startEnd, int endStart, int endEnd, String text) {
        textView.setTextColor(Color.parseColor(textColor));
        SpannableStringBuilder style = new SpannableStringBuilder(text);
        style.setSpan(new ForegroundColorSpan(Color.parseColor(startColor)), startStart, startEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        style.setSpan(new ForegroundColorSpan(Color.parseColor(endColor)), endStart, endEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        return textView;
    }

}

使用例子

 tvHiddenNum = TextColorChangeUtils.interTextColorForResources(this, tvHiddenNum, R.color.colorRed,
                R.color.colorBlue, R.color.colorYellow, 0, 2, num.length() - 1, num.length(), num);

使用结果:


使用结果
13、调用相册获取图片路径
import android.annotation.SuppressLint;
import android.content.ContentUris;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.provider.DocumentsContract;
import android.provider.MediaStore;

/**
 * Content:从相册内获取照片转化工具类
 * Actor:韩小呆 ヾ(゚▽゚)ノ
 * Time:  2018/08/18 18:54
 * Update:
 * Time:
 */
public class getPhotoFromPhotoAlbum {
    /**
     * 根据Uri获取图片的绝对路径
     *
     * @param context 上下文对象
     * @param uri     图片的Uri
     * @return 如果Uri对应的图片存在, 那么返回该图片的绝对路径, 否则返回null
     */
    public static String getRealPathFromUri(Context context, Uri uri) {
        int sdkVersion = Build.VERSION.SDK_INT;
        if (sdkVersion >= 19) { 
            return getRealPathFromUriAboveApi19(context, uri);
        } else {
            return getRealPathFromUriBelowAPI19(context, uri);
        }
    }

    /**
     * 适配api19以下(不包括api19),根据uri获取图片的绝对路径
     *
     * @param context 上下文对象
     * @param uri     图片的Uri
     * @return 如果Uri对应的图片存在, 那么返回该图片的绝对路径, 否则返回null
     */
    private static String getRealPathFromUriBelowAPI19(Context context, Uri uri) {
        return getDataColumn(context, uri, null, null);
    }

    /**
     * 适配api19及以上,根据uri获取图片的绝对路径
     *
     * @param context 上下文对象
     * @param uri     图片的Uri
     * @return 如果Uri对应的图片存在, 那么返回该图片的绝对路径, 否则返回null
     */
    @SuppressLint("NewApi")
    private static String getRealPathFromUriAboveApi19(Context context, Uri uri) {
        String filePath = null;
        if (DocumentsContract.isDocumentUri(context, uri)) {
            // 如果是document类型的 uri, 则通过document id来进行处理
            String documentId = DocumentsContract.getDocumentId(uri);
            if (isMediaDocument(uri)) {
                // 使用':'分割
                String id = documentId.split(":")[1];

                String selection = MediaStore.Images.Media._ID + "=?";
                String[] selectionArgs = {id};
                filePath = getDataColumn(context, MediaStore.Images.Media.EXTERNAL_CONTENT_URI, selection, selectionArgs);
            } else if (isDownloadsDocument(uri)) { 
                Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(documentId));
                filePath = getDataColumn(context, contentUri, null, null);
            }
        } else if ("content".equalsIgnoreCase(uri.getScheme())) {
            // 如果是 content 类型的 Uri
            filePath = getDataColumn(context, uri, null, null);
        } else if ("file".equals(uri.getScheme())) {
            // 如果是 file 类型的 Uri,直接获取图片对应的路径
            filePath = uri.getPath();
        }
        return filePath;
    }

    /**
     * 获取数据库表中的 _data 列,即返回Uri对应的文件路径
     *
     */
    private static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) {
        String path = null;

        String[] projection = new String[]{MediaStore.Images.Media.DATA};
        Cursor cursor = null;
        try {
            cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null);
            if (cursor != null && cursor.moveToFirst()) {
                int columnIndex = cursor.getColumnIndexOrThrow(projection[0]);
                path = cursor.getString(columnIndex);
            }
        } catch (Exception e) {
            if (cursor != null) {
                cursor.close();
            }
        }
        return path;
    }

    /**
     * @param uri the Uri to check
     * @return Whether the Uri authority is MediaProvider
     */
    private static boolean isMediaDocument(Uri uri) {
        return "com.android.providers.media.documents".equals(uri.getAuthority());
    }

    /**
     * @param uri the Uri to check
     * @return Whether the Uri authority is DownloadsProvider
     */
    private static boolean isDownloadsDocument(Uri uri) {
        return "com.android.providers.downloads.documents".equals(uri.getAuthority());
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值