天天看點

Android常用工具類ToastUtilsSizeUtilsLogUtilsRetrofitManagerAnimUtil

4條常用工具類

  • ToastUtils
  • SizeUtils
  • LogUtils
  • RetrofitManager
  • AnimUtil

ToastUtils

public class ToastUtils {

    private static Toast mToast;

    public static void showToast(String tips){
        if (mToast == null) {
            mToast = Toast.makeText(BaseApplication.getContext(), tips, Toast.LENGTH_SHORT);
        }else {
            mToast.show();
        }

    }

//    public static void showToast(Context context,String tips){
//        if (mToast == null) {
//            mToast = Toast.makeText(context, tips, Toast.LENGTH_SHORT);
//        }else {
//            mToast.show();
//        }
//
//    }
}
           

SizeUtils

/**
 * 尺寸工具類
 */
public class SizeUtil {

    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    public static int px2dip(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }

    public static int px2sp(Context context, float pxValue) {
        final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (pxValue / fontScale + 0.5f);
    }

    public static int sp2px(Context context, float spValue) {
        final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (spValue * fontScale + 0.5f);
    }

    /**
     * 擷取目前裝置寬度,機關px
     */
    public static int getDeviceWidth(Context context) {
        WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        return manager.getDefaultDisplay().getWidth();
    }

    /**
     * 擷取目前裝置高度,機關px
     */
    public static int getDeviceHeight(Context context) {
        WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        return manager.getDefaultDisplay().getHeight();
    }

    /**
     * 擷取狀态欄高度,要在onWindowFocusChanged中調用,在onCreate中擷取高度為0
     */
    public static int getStatusBarHeight(Activity activity) {
        Rect frame = new Rect();
        activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
        return frame.top;
    }
}
           

LogUtils

public class LogUtils {

        private static int currentLev = 5;
        private static final int DEBUG_LEV = 4;
        private static final int INFO_LEV = 3;
        private static final int WARNING_LEV = 2;
        private static final int ERROR_LEV = 1;


        public static void d(Object clazz,String log) {
            if(currentLev >= DEBUG_LEV) {
                Log.d(clazz.getClass().getSimpleName(),log);
            }
        }

        public static void i(Object clazz,String log) {
            if(currentLev >= INFO_LEV) {
                Log.i(clazz.getClass().getSimpleName(),log);
            }
        }

        public static void w(Object clazz,String log) {
            if(currentLev >= WARNING_LEV) {
                Log.w(clazz.getClass().getSimpleName(),log);
            }
        }

        public static void e(Object clazz,String log) {
            if(currentLev >= ERROR_LEV) {
                Log.e(clazz.getClass().getSimpleName(),log);
            }
        }

    }
           

RetrofitManager

public class RetrofitManager {

    public static final RetrofitManager mRetrofitManager = new RetrofitManager();
    private final Retrofit mRetrofit;

    public static RetrofitManager getInstance() {
        return mRetrofitManager;
    }

    public RetrofitManager() {
        //建立Retrofit
        this.mRetrofit = new Retrofit.Builder()
                .baseUrl(Constant.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    public Retrofit getRetrofit(){
        return mRetrofit;
    }
}
           

AnimUtil

private static final String TAG = AnimationUtil.class.getSimpleName();
 
    /**
     * 從控件所在位置移動到控件的底部
     *
     * @return
     */
    public static TranslateAnimation moveToViewBottom() {
        TranslateAnimation mHiddenAction = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,
                Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
                0.0f, Animation.RELATIVE_TO_SELF, 1.0f);
        mHiddenAction.setDuration(500);
        return mHiddenAction;
    }
 
    /**
     * 從控件的底部移動到控件所在位置
     *
     * @return
     */
    public static TranslateAnimation moveToViewLocation() {
        TranslateAnimation mHiddenAction = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,
                Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
                1.0f, Animation.RELATIVE_TO_SELF, 0.0f);
        mHiddenAction.setDuration(500);
        return mHiddenAction;
    }