package cn.flightfeather.thirdapp.util;
|
|
/**
|
* Created by note_ff_1602 on 2017/7/25.
|
*/
|
import android.app.Activity;
|
import android.content.Context;
|
import android.graphics.Bitmap;
|
import android.graphics.Rect;
|
import android.util.DisplayMetrics;
|
import android.util.TypedValue;
|
import android.view.View;
|
import android.view.WindowManager;
|
import android.widget.ImageView;
|
|
/**
|
* Created by Nate on 2015/9/10. 屏幕相关工具类,可以获取屏幕宽高度,还有截取屏幕
|
*/
|
public class ScreenUtils {
|
|
private ScreenUtils() {
|
/* cannot be instantiated */
|
throw new UnsupportedOperationException("cannot be instantiated");
|
}
|
|
/**
|
* 获得屏幕高度
|
*
|
* @param context
|
* @return
|
*/
|
public static int getScreenWidth(Context context) {
|
WindowManager wm = (WindowManager) context
|
.getSystemService(Context.WINDOW_SERVICE);
|
DisplayMetrics outMetrics = new DisplayMetrics();
|
wm.getDefaultDisplay().getMetrics(outMetrics);
|
return outMetrics.widthPixels;
|
}
|
|
/**
|
* 获得屏幕宽度
|
*
|
* @param context
|
* @return
|
*/
|
public static int getScreenHeight(Context context) {
|
WindowManager wm = (WindowManager) context
|
.getSystemService(Context.WINDOW_SERVICE);
|
DisplayMetrics outMetrics = new DisplayMetrics();
|
wm.getDefaultDisplay().getMetrics(outMetrics);
|
return outMetrics.heightPixels;
|
}
|
|
/**
|
* 获得状态栏的高度
|
*
|
* @param context
|
* @return
|
*/
|
public static int getStatusHeight(Context context) {
|
|
int statusHeight = -1;
|
try {
|
Class<?> clazz = Class.forName("com.android.internal.R$dimen");
|
Object object = clazz.newInstance();
|
int height = Integer.parseInt(clazz.getField("status_bar_height")
|
.get(object).toString());
|
statusHeight = context.getResources().getDimensionPixelSize(height);
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
return statusHeight;
|
}
|
|
/**
|
* 获取当前屏幕截图,包含状态栏
|
*
|
* @param activity
|
* @return
|
*/
|
public static Bitmap snapShotWithStatusBar(Activity activity) {
|
View view = activity.getWindow().getDecorView();
|
view.setDrawingCacheEnabled(true);
|
view.buildDrawingCache();
|
Bitmap bmp = view.getDrawingCache();
|
int width = getScreenWidth(activity);
|
int height = getScreenHeight(activity);
|
Bitmap bp = null;
|
bp = Bitmap.createBitmap(bmp, 0, 0, width, height);
|
view.destroyDrawingCache();
|
return bp;
|
|
}
|
|
/**
|
* 获取当前屏幕截图,不包含状态栏
|
*
|
* @param activity
|
* @return
|
*/
|
public static Bitmap snapShotWithoutStatusBar(Activity activity) {
|
View view = activity.getWindow().getDecorView();
|
view.setDrawingCacheEnabled(true);
|
view.buildDrawingCache();
|
Bitmap bmp = view.getDrawingCache();
|
Rect frame = new Rect();
|
activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
|
int statusBarHeight = frame.top;
|
int width = getScreenWidth(activity);
|
int height = getScreenHeight(activity);
|
Bitmap bp = null;
|
bp = Bitmap.createBitmap(bmp, 0, statusBarHeight, width, height
|
- statusBarHeight);
|
view.destroyDrawingCache();
|
return bp;
|
}
|
|
/**
|
* dp转px
|
*
|
* @param context
|
* @param dpVal
|
* @return
|
*/
|
public static int dp2px(Context context, float dpVal) {
|
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
|
dpVal, context.getResources().getDisplayMetrics());
|
}
|
|
/**
|
* sp转px
|
*
|
* @param context
|
* @param spVal
|
* @return
|
*/
|
public static int sp2px(Context context, float spVal) {
|
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,
|
spVal, context.getResources().getDisplayMetrics());
|
}
|
|
/**
|
* px转dp
|
*
|
* @param context
|
* @param pxVal
|
* @return
|
*/
|
public static float px2dp(Context context, float pxVal) {
|
final float scale = context.getResources().getDisplayMetrics().density;
|
return (pxVal / scale);
|
}
|
|
/**
|
* px转sp
|
*
|
* @param context
|
* @param pxVal
|
* @return
|
*/
|
public static float px2sp(Context context, float pxVal) {
|
return (pxVal / context.getResources().getDisplayMetrics().scaledDensity);
|
}
|
|
/**
|
* 动态设置图片宽高
|
*/
|
/**
|
* 动态设置图片宽高
|
*/
|
public static float[] getBitmapConfiguration(Bitmap bitmap, ImageView imageView, float screenRadio) {
|
int screenWidth = getScreenWidth(imageView.getContext());
|
float rawWidth = 0;
|
float rawHeight = 0;
|
float width = 0;
|
float height = 0;
|
if (bitmap == null) {
|
width = (float) (screenWidth / screenRadio);
|
height = (float) width;
|
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
|
} else {
|
rawWidth = bitmap.getWidth();
|
rawHeight = bitmap.getHeight();
|
if (rawHeight > 10 * rawWidth) {
|
imageView.setScaleType(ImageView.ScaleType.CENTER);
|
} else {
|
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
|
}
|
float radio = rawHeight / rawWidth;
|
width = (float) (screenWidth / screenRadio);
|
height = (float) (radio * width);
|
}
|
return new float[]{width, height};
|
}
|
}
|