码迷,mamicode.com
首页 > 移动开发 > 详细

android画虚线的自定义VIew

时间:2015-05-28 13:53:46      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:

package com.yesway.ycarplus.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Path;
import android.graphics.PathEffect;
import android.util.AttributeSet;
import android.view.View;

/**
 * 画虚线的一个view
 * 
 * @author liulm
 *
 */
public class DottedLine extends View {

    private int color = Color.WHITE;

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

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @SuppressLint("DrawAllocation")
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Paint p = new Paint();
        p.setAntiAlias(true);
        p.setStyle(Style.STROKE);
        p.setColor(color);
        p.setStrokeWidth(10);
        PathEffect effects = new DashPathEffect(new float[] { 0, 10, 35, 2 }, 1);

        Path path = new Path();
        path.moveTo(0, 0);
        path.lineTo(0, getMeasuredHeight());
        path.lineTo(getMeasuredWidth(), getMeasuredHeight());

        p.setPathEffect(effects);
        canvas.drawPath(path, p);
    }

    /**
     * 设置颜色
     * 
     * @param color
     */
    public void setColor(int color) {
        this.color = color;
        invalidate();
    }

    /**
     * 设置颜色值
     * 
     * @param color
     */
    public void setColor(String color) {
        this.color = Color.parseColor(color);
        invalidate();
    }

}技术分享

 

android画虚线的自定义VIew

标签:

原文地址:http://www.cnblogs.com/tianshidechibang234/p/4535591.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!