收集一些学习Android过程中使用到的小技巧,或网上看到一些有用的技巧,持续更新
1、Android模拟器中,快捷键"Ctrl+F11/F12"可以实现转屏
2、取得屏幕像素
DisplayMetrics dm=new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm);
3、位图缩放
public Bitmap zoomImage(Bitmap bgimage, int newWidth, int newHeight) { int width = bgimage.getWidth(); int height = bgimage.getHeight(); Matrix matrix = new Matrix(); float scaleWidth = ((float) newWidth) / width; float scaleHeight = ((float) newHeight) / height; matrix.postScale(scaleWidth, scaleHeight); Bitmap bitmap = Bitmap.createBitmap(bgimage, 0, 0, width, height, matrix, true); return bitmap; }4、保存到android多媒体图片文件夹
android.provider.MediaStore.Images.Media.insertImage(getContentResolver(),resultBitmap, "picName", "descrition");
6、获取当前设备的电话类型
boolean flag = false; TelephonyManager telephony = (TelephonyManager) MainActivity.this .getSystemService(Context.TELEPHONY_SERVICE); int type = telephony.getPhoneType(); System.out.println("type = " + type); if (type == TelephonyManager.PHONE_TYPE_NONE) { Toast.makeText(getApplicationContext(), "平板",Toast.LENGTH_SHORT).show(); } else { Toast.makeText(getApplicationContext(), "非平板",Toast.LENGTH_SHORT).show(); }7、获取当前应用程序的路径
private void getCurDir() { String fileDir = MainActivity.this.getFilesDir().toString(); String cacheDir = MainActivity.this.getCacheDir().toString(); System.out.println("fileDir = " + fileDir); System.out.println("cacheDir = " + cacheDir); }如果有临时文件需要保存,可以保存到cache文件夹下
8、判断设备是否带GPS
public boolean hasGPSDevice(Context context) { final LocationManager mgr = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE); if ( mgr == null ) return false; final List<String> providers = mgr.getAllProviders(); if ( providers == null ) return false; return providers.contains(LocationManager.GPS_PROVIDER); }9、获取状态栏高度
11、内存泄露的解析:https://github.com/CharonChui/AndroidNote/blob/master/Android%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/%E5%86%85%E5%AD%98%E6%B3%84%E6%BC%8F.md