分辨率及密度获取:

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






更多相关文章

  1. android 遍历assets下的文件
  2. Android手势 GestureDectector
  3. android webview 任何密度,屏幕大小。
  4. android 判断某个服务是否正在运行的方法
  5. Android(安卓)三种获取页面数据方法
  6. Android(安卓)ApiDemos示例解析(13):App->Activity->Reorder Acti
  7. Android的屏幕适配
  8. 检查Android是否支持指纹识别以及是否已经录入指纹
  9. Android(安卓)中 handle Message 的简单使用笔记

随机推荐

  1. Android(安卓)应用安装过程分析
  2. 百度地图2.1获取密钥配置的Android签名证
  3. Android之自定义ListView滚动条样式
  4. Fresco的使用
  5. android linker (1) —— __linker_init(
  6. OpenCV4Android开发
  7. Android Studio 导入包时报 Duplicate fi
  8. Android 中与 Touch 事件分发和消费机制
  9. Android ORM SQL Top 5
  10. android 中如何分析内存泄漏