【Android自定义控件】Android自定义虚线<三>

很多时候,画虚线都是使用美工切图(一个实点,一个虚点),然后使用Bitmap的repeat属性

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/repeat_bg"
    android:tileMode="repeat" />

下面我们来画一条虚线,首先定义一个 dashedline类,继承于View,重写Ondraw()方法

public class DashedLine extends View {
	private final String namespace = "http://www.android-study.com/";
	private float startX;
	private float startY;
	private float endX;
	private float endY;
	private Rect mRect;

	public DashedLine(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	@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(Color.DKGRAY);
		Path path = new Path();
		//通过moveto,lineto的x,y坐标确定虚线实横,纵,还是倾斜
		path.moveTo(0, 10);//Set the beginning of the next contour to the point (x,y)
		path.lineTo(480, 10);//Add a line from the last point to the specified point (x,y).
		//DashPathEffect  可以使用DashPathEffect来创建一个虚线的轮廓(短横线/小圆点),而不是使用实线
		//float[] { 5, 5, 5, 5 }值控制虚线间距,密度
		PathEffect effects = new DashPathEffect(new float[] { 5, 5, 5, 5 }, 1);
		paint.setPathEffect(effects);
		canvas.drawPath(path, paint);
	}
}


定义完View,就可以在XML中使用了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值