/**
* Copyright 2016 JustWayward Team
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.justwayward.reader.utils;
import android.animation.ValueAnimator;
import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.provider.Settings;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.Window;
import android.view.WindowManager;
import java.lang.reflect.Field;
/**
* 屏幕亮度工具类
*
* @author yuyh.
* @date 16/4/10.
*/
public class ScreenUtils {
public enum EScreenDensity {
XXHDPI, //超高分辨率 1080×1920
XHDPI, //超高分辨率 720×1280
HDPI, //高分辨率 480×800
MDPI, //中分辨率 320×480
}
public static EScreenDensity getDisply(Context context) {
EScreenDensity eScreenDensity;
//初始化屏幕密度
DisplayMetrics dm = context.getApplicationContext().getResources().getDisplayMetrics();
int densityDpi = dm.densityDpi;
if (densityDpi <= 160) {
eScreenDensity = EScreenDensity.MDPI;
} else if (densityDpi <= 240) {
eScreenDensity = EScreenDensity.HDPI;
} else if (densityDpi < 400) {
eScreenDensity = EScreenDensity.XHDPI;
} else {
eScreenDensity = EScreenDensity.XXHDPI;
}
return eScreenDensity;
}
/**
* 获取屏幕宽度
*
* @return
*/
public static int getScreenWidth() {
return AppUtils.getAppContext().getResources().getDisplayMetrics().widthPixels;
}
/**
* 获取屏幕高度
*
* @return
*/
public static int getScreenHeight() {
return AppUtils.getAppContext().getResources().getDisplayMetrics().heightPixels;
}
/**
* 将dp转换成px
*
* @param dp
* @return
*/
public static float dpToPx(float dp) {
return dp * AppUtils.getAppContext().getResources().getDisplayMetrics().density;
}
public static int dpToPxInt(float dp) {
return (int) (dpToPx(dp) + 0.5f);
}
/**
* 将px转换成dp
*
* @param px
* @return
*/
public static float pxToDp(float px) {
return px / AppUtils.getAppContext().getResources().getDisplayMetrics().density;
}
public static int pxToDpInt(float px) {
return (int) (pxToDp(px) + 0.5f);
}
/**
* 将px值转换为sp值
*
* @param pxValue
* @return
*/
public static float pxToSp(float pxValue) {
return pxValue / AppUtils.getAppContext().getResources().getDisplayMetrics().scaledDensity;
}
/**
* 将sp值转换为px值
*
* @param spValue
* @return
*/
public static float spToPx(float spValue) {
return spValue * AppUtils.getAppContext().getResources().getDisplayMetrics().scaledDensity;
}
public static int getStatusBarHeight(Context context) {
int result = 0;
int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
result = context.getResources().getDimensionPixelSize(resourceId);
}
return result;
}
public static int getActionBarSize(Context context) {
TypedValue tv = new TypedValue();
if (context.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) {
int actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data, context.getResources().getDisplayMetrics());
return actionBarHeight;
}
return 0;
}
private int getStatusBarHeight() {
Class<?> c = null;
Object obj = null;
Field field = null;
int x = 0, sbar = 0;
try {
c = Class.forName("com.android.internal.R$dimen");
obj = c.newInstance();
field = c.getField("status_bar_height");
x = Integer.parseInt(field.get(obj).toString());
sbar = AppUtils.getAppContext().getResources().getDimensionPixelSize(x);
} catch (Exception e1) {
e1.printStackTrace();
}
return sbar;
}
/**
* 当前是否是横屏
*
* @param context context
* @return boolean
*/
public static final boolean isLandscape(Context context) {
return context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE;
}
/**
* 当前是否是竖屏
*
* @param context context
* @return boolean
*/
public static final boolean isPortrait(Context context) {
return context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT;
}
/**
* 调整窗口的透明度 1.0f,0.5f 变暗
*
* @param from from>=0&&from<=1.0f
* @param to to>=0&&to<=1.0f
* @param context 当前的activity
*/
public static void dimBackground(final float from, final float to, Activity context) {
final Window window = context.getWindow();
ValueAnimator valueAnimator = ValueAnimator.ofFloat(from, to);
valueAnimator.setDuration(500);
valueAnimator.addUpdateListener(
new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
WindowManager.LayoutParams params = window.getAttributes();
params.alpha = (Float) animation.getAnimatedValue();
window.setAttributes(params);
}
});
valueAnimator.start();
}
/**
* 判断是否开启了自动亮度调节
*
* @param activity
* @return
*/
public static boolean isAutoBrightness(Activity activity) {
boolean isAutoAdjustBright = false;
try {
isAutoAdjustBright = Settings.System.getInt(
activity.getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS_MODE) == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC;
} catch (Settings.SettingNotFoundException e) {
e.printStackTrace();
}
return isAutoAdjustBright;
}
/**
* 关闭亮度自动调节
*
* @param activity
*/
public static void stopAutoBrightness(Activity activity) {
Settings.System.putInt(activity.getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS_MODE,
Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
}
/**
* 开启亮度自动调节
*
* @param activity
*/
public static void startAutoBrightness(Activity activity) {
Settings.System.putInt(activity.getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS_MODE,
Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
}
/**
* 获得当前屏幕亮度值
*
* @param mContext
* @return 0~100
*/
public static float getScreenBrightness(Context mContext) {
int screenBrightness = 255;
try {
screenBrightness = Settings.System.getInt(mContext.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS);
} catch (Exception e) {
e.printStackTrace();
}
return screenBrightness / 255.0F * 100;
}
/**
* 设置当前屏幕亮度值
*
* @param paramInt 0~100
* @param mContext
*/
public static void saveScreenBrightness(int paramInt, Context mContext) {
if (paramInt <= 5) {
paramInt = 5;
}
try {
float f = paramInt / 100.0F * 255;
Settings.System.putInt(mContext.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, (int) f);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 设置Activity的亮度
*
* @param paramInt
* @param mActivity
*/
public static void setScreenBrightness(int paramInt, Activity mActivity) {
if (paramInt <= 5) {
paramInt = 5;
}
Window localWindow = mActivity.getWindow();
WindowManager.LayoutParams localLayoutParams = localWindow.getAttributes();
float f = paramInt / 100.0F;
localLayoutParams.screenBrightness = f;
localWindow.setAttributes(localLayoutParams);
}
}