转自:http://blog.youkuaiyun.com/zhou0614/article/details/53351857
一、全屏工具类(真对某些有虚拟按键的手机,如Google Nexus系列)
- import android.app.Activity;
- import android.content.Context;
- import android.graphics.Color;
- import android.os.Build;
- import android.util.Log;
- import android.view.View;
- import android.view.Window;
- import android.view.WindowManager;
- /**
- *
- * Created by xl on 2016/9/7.
- */
- public class FullScreenUtils {
- private static final String TAG = "screen util";
- public static void transActionAndNavigation(Window window){
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
- window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
- window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
- }
- // if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
- // window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
- // | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
- // window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
- // | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
- // | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
- // window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
- // window.setStatusBarColor(Color.TRANSPARENT);
- // window.setNavigationBarColor(Color.TRANSPARENT);
- // }
- }
- public static void normalActionAndNavigation(Window window){
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
- window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
- window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
- }
- if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
- window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
- window.clearFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
- }
- }
- public static void toggleHideyBar(Window window) {
- // The UI options currently enabled are represented by a bitfield.
- // getSystemUiVisibility() gives us that bitfield.
- int uiOptions = window.getDecorView().getSystemUiVisibility();
- int newUiOptions = uiOptions;
- boolean isImmersiveModeEnabled =
- ((uiOptions | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY) == uiOptions);
- if (isImmersiveModeEnabled) {
- Log.i(TAG, "Turning immersive mode mode off. ");
- } else {
- Log.i(TAG, "Turning immersive mode mode on.");
- }
- // Navigation bar hiding: Backwards compatible to ICS.
- if (Build.VERSION.SDK_INT >= 14) {
- newUiOptions ^= View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
- }
- // Status bar hiding: Backwards compatible to Jellybean
- if (Build.VERSION.SDK_INT >= 16) {
- newUiOptions ^= View.SYSTEM_UI_FLAG_FULLSCREEN;
- }
- // Immersive mode: Backward compatible to KitKat.
- // Note that this flag doesn't do anything by itself, it only augments the behavior
- // of HIDE_NAVIGATION and FLAG_FULLSCREEN. For the purposes of this sample
- // all three flags are being toggled together.
- // Note that there are two immersive mode UI flags, one of which is referred to as "sticky".
- // Sticky immersive mode differs in that it makes the navigation and status bars
- // semi-transparent, and the UI flag does not get cleared when the user interacts with
- // the screen.
- if (Build.VERSION.SDK_INT >= 18) {
- newUiOptions ^= View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
- }
- window.getDecorView().setSystemUiVisibility(newUiOptions);
- }
- }
二、SharePreference工具类
- import android.content.Context;
- import android.content.SharedPreferences;
- /**
- * 保存资源到本地
- * Created by xl on 2016/8/11.
- */
- public class SharedPreferencesUtils {
- /**
- * 保存在手机里面的文件名
- */
- private static final String FILE_NAME = "share_date";
- /**
- * 保存数据的方法,我们需要拿到保存数据的具体类型,然后根据类型调用不同的保存方法
- *
- * @param context
- * @param key
- * @param object
- */
- public static void setParam(Context context, String key, Object object) {
- String type = object.getClass().getSimpleName();
- SharedPreferences sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
- SharedPreferences.Editor editor = sp.edit();
- if ("String".equals(type)) {
- editor.putString(key, (String) object);
- } else if ("Integer".equals(type)) {
- editor.putInt(key, (Integer) object);
- } else if ("Boolean".equals(type)) {
- editor.putBoolean(key, (Boolean) object);
- } else if ("Float".equals(type)) {
- editor.putFloat(key, (Float) object);
- } else if ("Long".equals(type)) {
- editor.putLong(key, (Long) object);
- }
- editor.commit();
- }
- /**
- * 得到保存数据的方法,我们根据默认值得到保存的数据的具体类型,然后调用相对于的方法获取值
- *
- * @param context
- * @param key
- * @param defaultObject
- * @return
- */
- public static Object getParam(Context context, String key, Object defaultObject) {
- String type = defaultObject.getClass().getSimpleName();
- SharedPreferences sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
- if ("String".equals(type)) {
- return sp.getString(key, (String) defaultObject);
- } else if ("Integer".equals(type)) {
- return sp.getInt(key, (Integer) defaultObject);
- } else if ("Boolean".equals(type)) {
- return sp.getBoolean(key, (Boolean) defaultObject);
- } else if ("Float".equals(type)) {
- return sp.getFloat(key, (Float) defaultObject);
- } else if ("Long".equals(type)) {
- return sp.getLong(key, (Long) defaultObject);
- }
- return null;
- }
- }
三、软键盘弹出和关闭
- import android.app.Activity;
- import android.graphics.Rect;
- import android.view.View;
- import android.view.ViewTreeObserver;
- /**
- * 监听键盘
- * Created by xulu on 16/7/29.
- */
- public class SoftKeyBoardListener {
- private View rootView;//activity的根视图
- int rootViewVisibleHeight;//纪录根视图的显示高度
- private OnSoftKeyBoardChangeListener onSoftKeyBoardChangeListener;
- public SoftKeyBoardListener(Activity activity) {
- //获取activity的根视图
- rootView = activity.getWindow().getDecorView();
- //监听视图树中全局布局发生改变或者视图树中的某个视图的可视状态发生改变
- rootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
- @Override
- public void onGlobalLayout() {
- //获取当前根视图在屏幕上显示的大小
- Rect r = new Rect();
- rootView.getWindowVisibleDisplayFrame(r);
- int visibleHeight = r.height();
- if (rootViewVisibleHeight == 0) {
- rootViewVisibleHeight = visibleHeight;
- return;
- }
- //根视图显示高度没有变化,可以看作软键盘显示/隐藏状态没有改变
- if (rootViewVisibleHeight == visibleHeight) {
- return;
- }
- //根视图显示高度变小超过200,可以看作软键盘显示了
- if (rootViewVisibleHeight - visibleHeight > 200) {
- if (onSoftKeyBoardChangeListener != null) {
- onSoftKeyBoardChangeListener.keyBoardShow(rootViewVisibleHeight - visibleHeight);
- }
- rootViewVisibleHeight = visibleHeight;
- return;
- }
- //根视图显示高度变大超过200,可以看作软键盘隐藏了
- if (visibleHeight - rootViewVisibleHeight > 200) {
- if (onSoftKeyBoardChangeListener != null) {
- onSoftKeyBoardChangeListener.keyBoardHide(visibleHeight - rootViewVisibleHeight);
- }
- rootViewVisibleHeight = visibleHeight;
- return;
- }
- }
- });
- }
- private void setOnSoftKeyBoardChangeListener(OnSoftKeyBoardChangeListener onSoftKeyBoardChangeListener) {
- this.onSoftKeyBoardChangeListener = onSoftKeyBoardChangeListener;
- }
- public interface OnSoftKeyBoardChangeListener {
- void keyBoardShow(int height);
- void keyBoardHide(int height);
- }
- public static void setListener(Activity activity, OnSoftKeyBoardChangeListener onSoftKeyBoardChangeListener) {
- SoftKeyBoardListener softKeyBoardListener = new SoftKeyBoardListener(activity);
- softKeyBoardListener.setOnSoftKeyBoardChangeListener(onSoftKeyBoardChangeListener);
- }
- }
四、其他一些工具
- import android.app.Activity;
- import android.content.Context;
- import android.graphics.Color;
- import android.os.Build;
- import android.util.DisplayMetrics;
- import android.util.Log;
- import android.view.Display;
- import android.view.View;
- import android.view.Window;
- import android.view.WindowManager;
- import android.view.inputmethod.InputMethodManager;
- import com.jyjapp.jyjzhibo.JYJApp;
- /**
- * 尺寸工具
- * Created by xulu on 16/8/10.
- */
- public class Utils {
- /**
- * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
- */
- public static int dip2px(Context context, float dpValue) {
- final float scale = context.getResources().getDisplayMetrics().density;
- return (int) (dpValue * scale + 0.5f);
- }
- /**
- * 根据手机的分辨率从 px(像素) 的单位 转成为 dp
- */
- public static int px2dip(Context context, float pxValue) {
- final float scale = context.getResources().getDisplayMetrics().density;
- return (int) (pxValue / scale + 0.5f);
- }
- /**
- * 获取屏幕宽高
- *
- * @param context
- * @return
- */
- public static int[] getScreenWidthAndHeight(Activity context) {
- WindowManager wm = context.getWindowManager();
- int width = wm.getDefaultDisplay().getWidth();
- int height1 = wm.getDefaultDisplay().getHeight();
- return new int[]{width, height1};
- }
- /**
- * 隐藏键盘
- *
- * @param view
- */
- public static void hideKeyboard(View view) {
- InputMethodManager imm = (InputMethodManager) JYJApp.getInstance().getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
- imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
- }
- /**
- * 显示键盘
- *
- * @param view
- */
- public static void showKeyboard(View view) {
- InputMethodManager imm = (InputMethodManager) JYJApp.getInstance().getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
- imm.showSoftInput(view, InputMethodManager.SHOW_FORCED);
- }
- /**
- * 获取屏幕真实宽度,横屏时
- *
- * @param context
- * @return
- */
- public static int getScreentWidth(Activity context) {
- int widthPixels;
- WindowManager w = context.getWindowManager();
- Display d = w.getDefaultDisplay();
- DisplayMetrics metrics = new DisplayMetrics();
- d.getMetrics(metrics);
- // since SDK_INT = 1;
- widthPixels = metrics.widthPixels;
- // includes window decorations (statusbar bar/navigation bar)
- if (Build.VERSION.SDK_INT >= 14 && Build.VERSION.SDK_INT < 17)
- try {
- widthPixels = (Integer) Display.class
- .getMethod("getRawWidth").invoke(d);
- } catch (Exception ignored) {
- }
- // includes window decorations (statusbar bar/navigation bar)
- else if (Build.VERSION.SDK_INT >= 17)
- try {
- android.graphics.Point realSize = new android.graphics.Point();
- Display.class.getMethod("getRealSize",
- android.graphics.Point.class).invoke(d, realSize);
- widthPixels = realSize.x;
- } catch (Exception ignored) {
- }
- Log.e("realHightPixels:", widthPixels + "width");
- return widthPixels;
- }
- }