android之hardwareAccelerated你不知道的一些問題

在Android中,可以四給不同層次上開啓硬件加速:

1、Application
<application android:hardwareAccelerated="true">

2、Activity
<activity android: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,會使得應用無法使用硬件加速進行繪圖。


注:有些朋友可能會遇到在開發過程中用到虛線的情況,實現有兩種方式:

方式一:

Danous_include_dotted_line.xml

<?xml version="1.0" encoding="utf-8"?>

<View xmlns:android="http://schemas.android.com/apk/res/android"

    style="@style/DanousStyleWmHw"

    android:background="@drawable/danous_shape_dotted_line" />


Danous_shape_dotted_line.xml

<?xml version="1.0" encoding="utf-8"?>

<shape xmlns:android="http://schemas.android.com/apk/res/android"

    android:shape="line" >


<!-- 顯示一條虛線,破折線的寬度爲dashWith,破折線之間的空隙的寬度爲dashGap,當dashGap=0dp時,爲實線(使用時需要把這個Activity的硬件加速關了) -->

    <stroke

        android:dashGap="@dimen/danous_dimen_wee_spacing"

        android:dashWidth="@dimen/danous_dimen_small_spacing"

        android:width="@dimen/danous_dimen_normal_divide"

        android:color="@color/danous_app_color_divide" />


    <!-- 虛線高度 -->

    <size android:height="2dp" />

</shape>


這種方式需要在使用該虛線的Activity中設置<activity android:hardwareAccelerated="false">,缺點是這個Activity會變慢,如果有網絡請求時,大概率出現收到數據後不會回調問題(斷點時,每次都會回調,這是很多人遇到的奇怪問題)。


方式二(建議使用這種方式):

Danous_include_dash_line.xml

<?xml version="1.0" encoding="utf-8"?>

<View xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="@dimen/danous_dimen_normal_divide"

    android:background="@drawable/danous_shape_dash" />


Danous_shape_dash.xml

<?xml version="1.0" encoding="utf-8"?>

<bitmap xmlns:android="http://schemas.android.com/apk/res/android"

    android:src="@drawable/danous_dashed_line"

    android:tileMode="repeat" />


Danous_dashed_line.png是一張圖片,只要很小很小就行,自動平鋪的

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章