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);
}
}
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;
}
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);
}
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);
}
}
}
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);
}
}
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);
}
}