Android -- 工具類(七):[ScreenUtil] (截圖,獲取螢幕寬高,顯示、隱藏虛擬鍵盤,調節螢幕亮度)

花追雨發表於2020-09-27

Android – 工具類(七):[ScreenUtil] (截圖,獲取螢幕寬高,顯示、隱藏虛擬鍵盤,調節螢幕亮度)

public class ScreenUtil {
    private static final String TAG = "ScreenUtil";

    //==============================截圖===================================
    public static void screenCap(Activity activity, String fileName){
        View dView = activity.getWindow().getDecorView();
        dView.setDrawingCacheEnabled(true);
        dView.buildDrawingCache();
        Bitmap bmp = dView.getDrawingCache();
        if (bmp != null)
        {
            String sdCardPath = FileUtil.getAppRootPth(activity);
            BitmapUtil.bitmap2Local(bmp, sdCardPath, fileName);
        }
    }

    /**
     * 獲取螢幕顯示指標物件
     * @return DisplayMetrics //dm.widthPixels;  //dm.heightPixels;
     */
    public static DisplayMetrics getScreenDM(Context context) {
        WindowManager manager = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        manager.getDefaultDisplay().getMetrics(dm);

        return dm;
    }

    public static int getScreenWidth(Context context){
        return getScreenDM(context).widthPixels;
    }

    public static int getScreenHeight(Context context){
        return getScreenDM(context).heightPixels;
    }

    //=========================keyboard control===============================
    //啟調鍵盤   (兩種啟調方式, 在不同機型存在啟調差異)
    public static void showKeyBoard(EditText et){
        et.setFocusable(true);
        et.requestFocus();
        InputMethodManager inputMethodManager = (InputMethodManager) et.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        inputMethodManager.showSoftInput(et,InputMethodManager.HIDE_IMPLICIT_ONLY);//InputMethodManager.RESULT_UNCHANGED_SHOWN
    }

    //啟調鍵盤
    public static void showKeyBoard(Context mContext) {
        if (mContext == null) return;
        InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm != null) {
            imm.toggleSoftInput(InputMethodManager.RESULT_UNCHANGED_SHOWN, InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }

    // 隱藏鍵盤
    public static void hideKeyBoard(EditText et){
        InputMethodManager inputMethodManager = (InputMethodManager) et.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        if(inputMethodManager!=null){
            inputMethodManager.hideSoftInputFromWindow(et.getWindowToken(), InputMethodManager.RESULT_UNCHANGED_SHOWN);
        }
    }

    // 隱藏鍵盤
    public static void hintSoftInput(Activity activity) {
        if (activity == null || activity.isFinishing()) return;
        View parent = ((ViewGroup) activity.findViewById(android.R.id.content)).getChildAt(0);
        hideKeyboard(activity, parent);
    }

    // 隱藏鍵盤
    public static void hideKeyboard(Activity act) {
        if (act == null || act.isFinishing()) return;
        final View v = act.getWindow().peekDecorView();
        hideKeyboard(act, v);
    }

    //隱藏鍵盤
    public static void hideKeyboard(Context context, View view) {
        if (context == null || view == null) return;
        IBinder mWindowToken = view.getWindowToken();
        if (mWindowToken != null) {
            InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
            if (imm != null) {
                imm.hideSoftInputFromWindow(mWindowToken, InputMethodManager.RESULT_UNCHANGED_SHOWN);
//                imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);//解決三星不能隱藏鍵盤的問題, 等遇到了再處理,
//                  (注: 現在如果使用, 則存在未被開啟鍵盤的時候, 呼叫該方法, 會啟動鍵盤)
            }
        }
    }

    /**
     * 禁止Edittext彈出軟體盤,游標依然正常顯示。
     */
    public static void prohibitShowSoftInput(EditText editText) {
        Class<EditText> cls = EditText.class;
        Method method;
        try {
            method = cls.getMethod("setShowSoftInputOnFocus", boolean.class);
            method.setAccessible(true);
            method.invoke(editText, false);
        } catch (Exception e) {
            Log.e(TAG, "阻止軟體盤彈窗異常"+e);
        }
    }

    //=============================控制螢幕亮度==================================
    //設定當前螢幕亮度值 0--255,並使之生效
    public static void setScreenBrightness(Activity act, float value) {
        value = value>255.0f? 255.0f: value< 0.0f? 0.0f: value;
        WindowManager.LayoutParams lp = act.getWindow().getAttributes();
        lp.screenBrightness = lp.screenBrightness + (value) / 255.0f;
        if (lp.screenBrightness > 1) {
            lp.screenBrightness = 1;
        } else if (lp.screenBrightness < 0.2) {
            lp.screenBrightness = (float) 0.2;
        }
        act.getWindow().setAttributes(lp);

        // 儲存設定的螢幕亮度值
        Settings.System.putInt(act.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, (int) value);
    }

    public static int getSystemScreenBrightness(Activity activity) {
        try {
            return Settings.System.getInt(activity.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS);
        } catch (Settings.SettingNotFoundException e) {
            e.printStackTrace();
        }
        return 0;
    }

    //自動調節亮度
    public static boolean isAutoBrightness(Activity activity) {
        try {
            int autoBrightness = Settings.System.getInt(
                    activity.getContentResolver(),
                    Settings.System.SCREEN_BRIGHTNESS_MODE);
            if (autoBrightness == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC) {
                return true;
            }
        } catch (Settings.SettingNotFoundException e) {
            e.printStackTrace();
        }
        return false;
    }

    public static void closeAutoBrightness(Activity activity) {
        Settings.System.putInt(activity.getContentResolver(),
                Settings.System.SCREEN_BRIGHTNESS_MODE,
                Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
    }

    public static void openAutoBrightness(Activity activity) {
        setScreenBrightness(activity, -1);
        Settings.System.putInt(activity.getContentResolver(),
                Settings.System.SCREEN_BRIGHTNESS_MODE,
                Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
    }
}

相關文章