分辨率及密度获取:
DisplayMetrics dm = new DisplayMetrics();
dm = getResources().getDisplayMetrics();
int screenWidth = dm.widthPixels;
int screenHeight = dm.heightPixels;
float density = dm.density;
float xdpi = dm.xdpi;
float ydpi = dm.ydpi;
对于不同分辨率屏幕等比缩放方法:
private int getValues_x(int value_x)
{
return (int)((float)value_x/480*width);
}
private int getValues_y(int value_y)
{
return (int)((float)value_y/854*height);
}
屏幕朝向设置:
//强制为横屏
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
要设置成竖屏设置成 SCREEN_ORIENTATION_PORTRAIT
AndroidManifest.xml 中设置屏幕朝向
<activity android:name=".HandlerActivity" android:screenOrientation="landscape"/>
<activity android:name=".HandlerActivity" android:screenOrientation="portrait"/>
设置app在不同分辨率时,是否支持多密度的方法。
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
...
<supports-screens
android:smallScreens="true"
android:normalScreens="true"
android:largeScreens="true"
android:xlargeScreens="true"
android:anyDensity="true" />
</manifest>
dp与px转换的方法:
public static int dip2px(Context context, float dipValue){
final float scale = context.getResources().getDisplayMetrics().density;
return (int)(dipValue * scale +0.5f);
}
public static int px2dip(Context context, float pxValue){
final float scale = context.getResource().getDisplayMetrics().density;
return (int)(pxValue / scale +0.5f);
}
屏幕截取实现:
1,
/**
* 简易截屏方法
* @param v 视图
* @param filePath 保存路径
*/
private void getScreenHot(View v, String filePath) {
try
{
Bitmap bitmap = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas();
canvas.setBitmap(bitmap);
v.draw(canvas);
try{
FileOutputStream fos = new FileOutputStream(filePath);
bitmap.compress(CompressFormat.PNG, 90, fos);
}catch (FileNotFoundException e){
Log.e("errormsg", e.toString());
Toast.makeText(this, "找不到路径", 200).show();
}
}catch (Exception e){
Log.e("errormsg", e.toString());
Toast.makeText(this, "截图失败", 200).show();
}
}
调用getScreenHot((View) getWindow().getDecorView(), "/sdcard/pic1.png"); 即可
2,
/**
* 获取和保存当前屏幕的截图
*/
private void SaveCurrentImage()
{
//创建Bitmap
WindowManager windowManager = getWindowManager();
Display display = windowManager.getDefaultDisplay();
int w = display.getWidth();
int h = display.getHeight();
Bitmap bmp = Bitmap.createBitmap( w, h, Config.ARGB_8888 );
//获取屏幕
View decorview = this.getWindow().getDecorView();
decorview.setDrawingCacheEnabled(true);
bmp = decorview.getDrawingCache();
String SavePath = getSDCardPath()+"/ScreenImage";
//存储为Bitmap
try {
SimpleDateFormat sdf = new SimpleDateFormat(
"yyyy-MM-dd_HH-mm-ss", Locale.CHINA);
File path = new File(SavePath);
//文件
String filepath = SavePath + "/"+sdf.format(new Date()) + ".png";
File file = new File(filepath);
if(!path.exists()){
path.mkdirs();
}
if (!file.exists()) {
file.createNewFile();
}
FileOutputStream fos = null;
fos = new FileOutputStream(file);
if (null != fos) {
bmp.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.flush();
fos.close();
Toast.makeText(this, "截屏文件已保存至SDCard/ScreenImage/下", Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
e.printStackTrace();
}
}