您的位置:首页 > 其它

画一条虚线

2017-03-18 07:28 134 查看
方法一:

1、

通过自定义控件

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

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical" >

<com.example.drawxuxian.DotView

android:layout_marginTop="10dp"

android:layout_height="2dp"

android:layout_width="match_parent">

</com.example.drawxuxian.DotView>

</LinearLayout>

2、

复制进自己项目的时候注意上面xml中自定义控件名称

public class DotView extends View {

private Paint p;

private int width;

private int height;

private int dash;

public DotView(Context context, AttributeSet attrs) {

super(context, attrs);

init(context);

}

public DotView(Context context) {

super(context);

init(context);

}

private void init(Context context){

p = new Paint(Paint.ANTI_ALIAS_FLAG);

p.setStyle(Style.FILL);

p.setColor(Color.parseColor("#d8d8d8"));

final float scale = context.getResources().getDisplayMetrics().density;

dash=(int) (6 * scale + 0.5f);

}

@Override

protected void onDraw(Canvas canvas) {

if(width>10){

for(int i=0;i<width;i+=dash){

canvas.drawLine(i, 0, i+=dash, 0, p);

}

}

super.onDraw(canvas);

}

@Override

public void onSizeChanged(int w, int h, int oldw, int oldh) {

super.onSizeChanged(w, h, oldw, oldh);

width=w;

height=h;

p.setStrokeWidth(height);

this.postInvalidate();

}

}

方法二:

1、

在drawable目录下,定义一个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时,为实线 -->

<stroke android:width="1dp" android:color="#D5D5D5"

android:dashWidth="2dp" android:dashGap="3dp" />

<!-- 虚线的高度 -->

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

</shape>

2、

在布局文件中定义一个view

<View

android:layout_marginTop="10dp"

android:layout_height="2dp"

android:layout_width="match_parent"

android:background="@drawable/line"

/>

3、

关闭硬件加速:(这句代码一定要有)

<activity

XXX

android:hardwareAccelerated="false"

XXX

</activity>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: