一些自己常用的代码块(更新中...)

SharedPreferences工具类

Application中初始化

public class SpUtil {    
    private static SharedPreferences mSp;   

    public static void init(SharedPreferences sharedPreferences){        
          mSp = sharedPreferences;
    }   
    public static void updateOrSave(String key,String json){           
       mSp.edit().putString(key,json).apply();    
    }  
    public static String find(String key){
        return mSp.getString(key,null);
    }
    public static void delete(String key){
        mSp.edit().remove(key).apply();
    }     
    public static void clearAll(){
        mSp.edit().clear().apply();
    }
}

Toast工具类

Application中初始化

public class ToastUtils {
    public static Context sContext;
    private ToastUtils() {    }
    public static void register(Context context) {
        sContext = context.getApplicationContext();
    }
    private static void check() {
        if (sContext == null) {
            throw new NullPointerException(
                    "Must initial call ToastUtils.register(Context context) in your " +                            "");
        }
    }
    public static void showShort(int resId) {
        check();
        Toast.makeText(sContext, resId, Toast.LENGTH_SHORT).show();
    }
    public static void showShort(String message) {
        check();
        Toast.makeText(sContext, message,Toast.LENGTH_SHORT).show();
    }
    public static void showLong(int resId) {
        check();
        Toast.makeText(sContext, resId, Toast.LENGTH_LONG).show();
    }
    public static void showLong(String message) {
        check();
        Toast.makeText(sContext, message, Toast.LENGTH_LONG).show();
    }
    public static void showLongX2(String message) {
        showLong(message);
        showLong(message);
    }
    public static void showLongX2(int resId) {
        showLong(resId);
        showLong(resId);
    }
    public static void showLongX3(int resId) {
        showLong(resId);
        showLong(resId);
        showLong(resId);
    }
    public static void showLongX3(String message) {
        showLong(message);
        showLong(message);
        showLong(message);
    }}

activity变暗

    /**
     * 调整窗口的透明度
     * @param from>=0&&from<=1.0f
     * @param to>=0&&to<=1.0f
     * 
     * */
    private void dimBackground(final float from, final float to) {
        final Window window = getWindow();
        ValueAnimator valueAnimator = ValueAnimator.ofFloat(from, to);
        valueAnimator.setDuration(500);
        valueAnimator.addUpdateListener(new AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                WindowManager.LayoutParams params = window.getAttributes();
                params.alpha = (Float) animation.getAnimatedValue();
                window.setAttributes(params);
            }
        });
        valueAnimator.start();
    }
然后这样调用:

    /** 窗口背景变暗*/
    dimBackground(1.0f,0.5f);


    /** 窗口背景变亮*/
    dimBackground(0.5f,1.0f);

你可能感兴趣的:(一些自己常用的代码块(更新中...))