0%

Android 画虚线显示实线的BUG

画虚线一般写个xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="line" >
    <stroke
        android:dashGap="8dp"
        android:dashWidth="8dp"
        android:color="@color/appendrate_divider_color" />
</shape>

其中dashGap是空隙宽度,dashWidth是线的宽度,当dashGap也就是空隙宽度为0时,显示一条实线。然后在background或src中引用,但在android 4.0以上版本中会有BUG,发现不管dashGap设置多大,显示的都是一条实线。 解决方法: 关闭硬件加速。 可以在AndroidManifest.xml时的Application标签加上android:hardwareAccelerated=”false”,这样整件应用都关闭了硬件加速,虚线可以正常显示,但是,关闭硬件加速对性能有些影响,会感觉明显比没关卡。 也可以给虚线的view单独关闭硬件加速:

divider_under_pic.setLayerType(View.LAYER_TYPE_SOFTWARE, null);