分享:android之hardwareAccelerated你不知道的一些问题
2015-12-09 10:41
302 查看
在Android中,可以四给不同层次上开启硬件加速:
1、Application
<applicationandroid:hardwareAccelerated="true">
2、Activity
<activityandroid:hardwareAccelerated="true">
3、Window
getWindow().setFlags(WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
4、View
view.setLayerType(View.LAYER_TYPE_SOFTWARE,null);
在这四个层次中,应用和Activity是可以选择的,Window只能打开,View只能关闭。
在apk的AndroidManifest中,如果指定了minSDKVersion&targetSDKVersion=7,会使得应用无法使用硬件加速进行绘图。
注:有些朋友可能会遇到在开发过程中用到虚线的情况,实现有两种方式:
方式一:
hundsun_include_dotted_line.xml
<?xml version="1.0"encoding="utf-8"?>
<Viewxmlns:android="http://schemas.android.com/apk/res/android"
style="@style/HundsunStyleWmHw"
android:background="@drawable/hundsun_shape_dotted_line" />
hundsun_shape_dotted_line.xml
<?xml version="1.0"encoding="utf-8"?>
<shapexmlns:android="http://schemas.android.com/apk/res/android"
android:shape="line" >
<!-- 显示一条虚线,破折线的宽度为dashWith,破折线之间的空隙的宽度为dashGap,当dashGap=0dp时,为实线(使用时需要把这个Activity的硬件加速关了) -->
<stroke
android:dashGap="@dimen/hundsun_dimen_wee_spacing"
android:dashWidth="@dimen/hundsun_dimen_small_spacing"
android:width="@dimen/hundsun_dimen_normal_divide"
android:color="@color/hundsun_app_color_divide" />
<!-- 虚线高度 -->
<size android:height="2dp" />
</shape>
这种方式需要在使用该虚线的Activity中设置<activity android:hardwareAccelerated="false">,缺点是这个Activity会变慢,如果有网络请求时,大概率出现收到数据后不会回调问题(断点时,每次都会回调,这是很多人遇到的奇怪问题)。
方式二(建议使用这种方式):
hundsun_include_dash_line.xml
<?xml version="1.0"encoding="utf-8"?>
<Viewxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="@dimen/hundsun_dimen_normal_divide"
android:background="@drawable/hundsun_shape_dash" />
hundsun_shape_dash.xml
<?xml version="1.0"encoding="utf-8"?>
<bitmapxmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/hundsun_dashed_line"
android:tileMode="repeat" />
hundsun_dashed_line.png是一张图片,只要很小很小就行,自动平铺的。另外对APP进行在线全方位的安全性、兼容性测试,我都会用这个:www.ineice.com。
1、Application
<applicationandroid:hardwareAccelerated="true">
2、Activity
<activityandroid:hardwareAccelerated="true">
3、Window
getWindow().setFlags(WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
4、View
view.setLayerType(View.LAYER_TYPE_SOFTWARE,null);
在这四个层次中,应用和Activity是可以选择的,Window只能打开,View只能关闭。
在apk的AndroidManifest中,如果指定了minSDKVersion&targetSDKVersion=7,会使得应用无法使用硬件加速进行绘图。
注:有些朋友可能会遇到在开发过程中用到虚线的情况,实现有两种方式:
方式一:
hundsun_include_dotted_line.xml
<?xml version="1.0"encoding="utf-8"?>
<Viewxmlns:android="http://schemas.android.com/apk/res/android"
style="@style/HundsunStyleWmHw"
android:background="@drawable/hundsun_shape_dotted_line" />
hundsun_shape_dotted_line.xml
<?xml version="1.0"encoding="utf-8"?>
<shapexmlns:android="http://schemas.android.com/apk/res/android"
android:shape="line" >
<!-- 显示一条虚线,破折线的宽度为dashWith,破折线之间的空隙的宽度为dashGap,当dashGap=0dp时,为实线(使用时需要把这个Activity的硬件加速关了) -->
<stroke
android:dashGap="@dimen/hundsun_dimen_wee_spacing"
android:dashWidth="@dimen/hundsun_dimen_small_spacing"
android:width="@dimen/hundsun_dimen_normal_divide"
android:color="@color/hundsun_app_color_divide" />
<!-- 虚线高度 -->
<size android:height="2dp" />
</shape>
这种方式需要在使用该虚线的Activity中设置<activity android:hardwareAccelerated="false">,缺点是这个Activity会变慢,如果有网络请求时,大概率出现收到数据后不会回调问题(断点时,每次都会回调,这是很多人遇到的奇怪问题)。
方式二(建议使用这种方式):
hundsun_include_dash_line.xml
<?xml version="1.0"encoding="utf-8"?>
<Viewxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="@dimen/hundsun_dimen_normal_divide"
android:background="@drawable/hundsun_shape_dash" />
hundsun_shape_dash.xml
<?xml version="1.0"encoding="utf-8"?>
<bitmapxmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/hundsun_dashed_line"
android:tileMode="repeat" />
hundsun_dashed_line.png是一张图片,只要很小很小就行,自动平铺的。另外对APP进行在线全方位的安全性、兼容性测试,我都会用这个:www.ineice.com。
相关文章推荐
- Android GridView属性集合
- Android系统五大布局详解Layout
- android dialog关闭按钮 在半透明区域(最简单实现)
- Android Studio基础之主界面及使用(三)-学习篇
- android camera中的预览图片变形的解决办法
- Android:初识异步任务AsyncTask
- Android Camera 使用详解
- Android点击EditText文本框之外任何地方隐藏键盘的解决办法
- android开发之流量监控
- android 流量监控实现想法
- Android递归方式删除某文件夹下的所有文件
- Android调用系统安装/卸载apk过程
- Android 编译系统 (二)
- Android的四种启动模式
- android java.io.IOException: open failed: EBUSY (Device or resource busy)
- Handler常见问题
- android 创建快捷方式
- Android studio的使用 -- 代码混淆与打包
- Android自定义相机
- Android adb常用指令和常见问题