自定义view--虚线的绘制


今天给大家带来的是自定义view-虚线的绘制。


说到android绘制虚线其实通过shape也是可以完成的,例如:


<?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="#000000"      
             android:dashWidth="2dp" android:dashGap="3dp" />     
             <!-- 虚线的高度 -->   
     <size android:height="2dp" />      
</shape>  

但是当你放入手机运行后会发现怎么也显示不了虚线。原来从android3.0开始,安卓开启了硬件加速功能,导致不能显示,解决方法是在 AndroidManifest.xml中设置android:hardwareAccelerated="false";或者activity中把硬件加速的功能关掉view.setLayerType(View.LAYER_TYPE_SOFTWARE, null)。

不过既然Google开启了硬件加速那说明是有好处的,我们还是做保险的方法自己来绘制:


首先在 /工程/res/values/attrs.xml 中加入属性代码:

<declare-styleable name="dashedline">
        <attr name="lineColor" format="color" />
        <attr name="orientation">
            <enum name="horizontal" value="0" />
            <enum name="vertical" value="1" />
        </attr>
    </declare-styleable>

有不懂自定义属性方面的可以看我的上一篇博客  自定义view-SlideSwitch开源详解 里面对自定义方面讲解很详细。

接下来是自定义代码:

public class DashedLineView extends View {

	private Paint paint = null;
	private Path path = null;
	private PathEffect effects = null;

	private int orientation;
	private int lineColor;

	public static final int HORIZONTAL = 0;
	public static final int VERTICAL = 1;
	private static final int DEFAULT_COLOR_THEME = Color
			.parseColor("#ff000000");

	public DashedLineView(Context context) {
		super(context, null);
		// TODO Auto-generated constructor stub
	}

	public DashedLineView(Context context, AttributeSet attrs) {
		super(context, attrs, 0);
		// TODO Auto-generated constructor stub
		setCustomAttributes(attrs);
	}

	public DashedLineView(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		// TODO Auto-generated constructor stub
		setCustomAttributes(attrs);
	}

	private void setCustomAttributes(AttributeSet attrs) {
		TypedArray a = getContext().obtainStyledAttributes(attrs,
				R.styleable.dashedline);
		lineColor = a.getColor(R.styleable.dashedline_lineColor,
				DEFAULT_COLOR_THEME);
		orientation = a.getInt(R.styleable.dashedline_orientation, HORIZONTAL);
		paint = new Paint();
		path = new Path();
		paint.setStyle(Paint.Style.STROKE);
		paint.setColor(lineColor);
		paint.setAntiAlias(true);
		a.recycle();
	}

	@SuppressLint("DrawAllocation")
	public void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		path.moveTo(0, 0);
		if (orientation == VERTICAL) {
			path.lineTo(0, this.getHeight());
		} else {
			path.lineTo(this.getWidth(), 0);
		}
		// PathEffect是用来控制绘制轮廓(线条)的方式
		// 代码中的float数组,必须是偶数长度,且>=2,指定了多少长度的实线之后再画多少长度的空白.如本代码中,绘制长度5的实线,再绘制长度5的空白,再绘制长度5的实线,再绘制长度5的空白,依次重复.1是偏移量,可以不用理会.

		effects = new DashPathEffect(new float[] { 5, 5, 5, 5 }, 1);
		paint.setPathEffect(effects);
		canvas.drawPath(path, paint);

	}
}

代码比较简单,相信大家看一下就明白了。这里主要说下 PathEffect类

PathEffect是用来控制绘制轮廓(线条)的方式。

PathEffect对于绘制Path基本图形特别有用,但是它们也可以应用到任何Paint中从而影响线条绘制的方式。

使用PathEffect,可以改变一个形状的边角的外观并且控制轮廓的外表。

Android包含了多个PathEffect,包括:

CornerPathEffect  可以使用圆角来代替尖锐的角从而对基本图形的形状尖锐的边角进行平滑。

DashPathEffect  可以使用DashPathEffect来创建一个虚线的轮廓(短横线/小圆点),而不是使用实线。你还可以指定任意的虚/实线段的重复模式。

DiscretePathEffect  与DashPathEffect相似,但是添加了随机性。当绘制它的时候,需要指定每一段的长度和与原始路径的偏离度。

PathDashPathEffect  这种效果可以定义一个新的形状(路径)并将其用作原始路径的轮廓标记。

下面的效果可以在一个Paint中组合使用多个Path Effect。

SumPathEffect  顺序地在一条路径中添加两种效果,这样每一种效果都可以应用到原始路径中,而且两种结果可以结合起来。

ComposePathEffect  将两种效果组合起来应用,先使用第一种效果,然后在这种效果的基础上应用第二种效果。

对象形状的PathEffect的改变会影响到形状的区域。这就能够保证应用到相同形状的填充效果将会绘制到新的边界中。


接下来是在xml中使用

<你的view所在的包名.DashedLineView
                android:layout_width="1dp"
                android:layout_height="match_parent"
                dash:lineColor="@color/red"
                dash:orientation="vertical" 
            />
自定义属性中可以自定义颜色和虚线的方向   垂直(vertical)、水平(horizontal)。


好了今天的分享就到这里了,以后会给大家带来更多有用的小工具。


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值