用xml画水平虚线和竖直虚线.md

1.画水平虚线

直接建一个shape,设置stroke属性就行了,再将这个属性直接作为background的drawable属性引入就行了
注意在4.0以上的真机加一句

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="line">
    <solid android:color="@color/red"></solid>
    <stroke
        android:dashWidth="10dp" //虚线的小线段长度
        android:dashGap="10dp" //间隔距离
        android:color="@color/app_color"
        android:width="4dp"
        />
</shape>
<View
    android:id="@+id/content"
    android:layout_width="match_parent"
    android:layout_height="30dp"
    android:layout_gravity="center_vertical"
    android:background="@drawable/dottde_line"
    android:layerType="software"  />  //4.0以上的加,不然真机中是实线

2.画竖直虚线

这里借鉴一个 rotate属性去实现,代码如下

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:left="-30dp"
        android:right="-30dp">
        <rotate
            android:drawable="@drawable/dottde_line"
            android:visible="true"
            android:fromDegrees="90">
        </rotate>
    </item>
</layer-list>
<View
    android:id="@+id/content_2"
    android:layout_width="30dp"
    android:layout_height="match_parent"
    android:layout_gravity="center_horizontal"
    android:background="@drawable/dottde_v_line"
    android:layerType="software" />

原理:
设置了fromDegress之后,会先画一条水平虚线,然后再瞬间顺时针旋转90度角,但是单这样还不行,因为我的view的宽度设置的是2dp,高度是match_parent,发现出来的只有一个点,郁闷了。
之前说过,他的原理是先画一条水平线,然后再旋转,那么view的宽度只有2dp,他就只能画2dp,所以旋转后也就是一个点。因此用item的可以设置偏移量的属性,我们将rotate节点放于一个item节点下面,然后给item设置左右都为-30dp的偏移量,这样他在先画水平线的时候,由于负的偏移量(就和负的margin一样),就可以画出60dp长的线,然后再旋转,就可以得到一条竖直虚线。

3用自定义view去画

public class DashedLineView extends View{
    public  Context ctx;
    public DashedLineView(Context context, AttributeSet attrs) {
        super(context, attrs);
        ctx=context;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        super.onDraw(canvas);
        Paint paint = new Paint();
        paint.setStyle(Paint.Style.STROKE);
        paint.setColor(getResources().getColor(R.color.red));
        paint.setStrokeWidth(dip2px(ctx,2));
        Path path = new Path();
        path.moveTo(0, 0);
        path.lineTo(0, 900);
        PathEffect effects = new DashPathEffect(new float[]{6, 4, 4, 4}, 2);
        paint.setPathEffect(effects);
        canvas.drawPath(path, paint);
    }

    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
}

原理:

4.认识图层列表

地址 https://developer.android.google.cn/guide/topics/resources/drawable-resource.html?hl=zh-cn#LayerList

posted @ 2017-03-31 13:59  一粒尘_PM  阅读(5907)  评论(0编辑  收藏  举报

Android应用开发&研究 - 创建于 2017年8月17日

这是一位Android开发工程师的个人站,内容主要是网站开发方面的技术文章,大部分来自学习或工作,部分来源于网络,希望对大家有所帮助。

致力于Android应用开发&研究工作,专注移动开发,关注互联网前沿技术与趋势。


Font Awesome | Respond.js | Bootstrap中文网