Android开发小技巧
16lz
2022-03-12
收集一些学习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");
方法一:
只要在配置文件内activity属性配置内加上
android:theme="@android:style/Theme.Translucent"
这样就调用了android的透明样式!
方法二:
先在res/values下建colors.xml文件,写入:
<?xmlversionxmlversion="1.0"encoding="UTF-8"?>
<resources>
<colornamecolorname="transparent">#9000</color>
</resources>
这个值设定了整个界面的透明度,为了看得见效果,现在设为透明度为56%(9/16)左右。
再在res/values/下建styles.xml,设置程序的风格
<?xmlversionxmlversion="1.0"encoding="utf-8"?>
<resources>
<stylenamestylename="Transparent">
<itemnameitemname="android:windowBackground">@color/transparent</item>
<itemnameitemname="android:windowIsTranslucent">true</item>
<itemnameitemname="android:windowAnimationStyle">@+android:style/Animation.Translucent</item>
</style>
</resources>
最后一步,把这个styles.xml用在相应的Activity上。即在AndroidManifest.xml中的任意<activity>标签中添加
android:theme="@style/transparent"
如果想设置所有的activity都使用这个风格,可以把这句标签语句添加在<application>中。
最后可以把背景色#9000换成#0000,运行程序后,就全透明了,看得见背景下的所有东西可以却都操作无效。
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、获取状态栏高度
decorView是window中的最顶层view,可以从window中获取到decorView,然后decorView有个getWindowVisibleDisplayFrame方法可以获取到程序显示的区域,包括标题栏,但不包括状态栏。
于是,我们就可以算出状态栏的高度了。
Rect frame = new Rect();
getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
int statusBarHeight = frame.top;
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
更多相关文章
- android 建议在onPause和onStop处理的事情
- android通过httpClient请求获取JSON数据并且解析
- 好的android 相关的技术博客
- Android小米(miui)获取通话记录为null解决办法
- android 获取sim卡运营商信息
- android保持在休眠时,后台程序继续运行(让程序获取设备电源锁)
- Android(安卓)手动显示和隐藏软键盘 android 隐藏显示输入法键盘
- android学习笔记二
- Android自动提示文本框(AutoCompleteTextView)