android 屏幕分辨率获取,等比缩放,屏幕横竖屏设置,屏幕截取
来源:互联网 发布:淘宝图片制作视频教程 编辑:程序博客网 时间:2024/06/16 01:03
分辨率及密度获取:
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(); } }
- android 屏幕分辨率获取,等比缩放,屏幕横竖屏设置,屏幕截取
- android:获取屏幕分辨率,屏幕密度
- android手机设置屏幕横竖屏朝向
- 解决Android屏幕横竖屏设置
- android整合--屏幕全屏 屏幕横竖屏设置
- Android 判断横竖屏幕以及设置横竖屏幕的方法
- 获取 屏幕分辨率 屏幕大小
- Android 屏幕横竖屏切换
- Android学习笔记之屏幕横竖设置
- Android 屏幕旋转(横竖设置)
- Cocos2d设置横竖屏幕
- iOS 9的屏幕横竖屏设置
- android 获取屏幕尺寸,设置屏幕方向
- Android横竖屏幕切换
- Android 横竖屏幕切换
- android 屏幕全屏、 横竖
- Android 横竖屏幕切换
- android 横竖屏幕切换
- 在Windows 7中轻松管理Virtual PC/Hyper-V虚拟机磁盘文件VHD
- C趣味编程之汉诺塔问题
- 整理一系列优秀的Android开发源码
- 使用Fitnesse进行接口自动化测试
- 一个ListCtrl的详细实现
- android 屏幕分辨率获取,等比缩放,屏幕横竖屏设置,屏幕截取
- WinForm多线程+委托防止界面卡死
- json注意事项
- ProtoBuf
- protobuf的编码规则
- php_paypal接口开发!
- Cannot convert value '0000-00-00 00:00:00' from column 1 to TIMESTAMP
- DSP之信号采集
- 嵌入式系统Linux内核开发工程师必须掌握的三十道题