• android画虚线的自定义VIew


    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();
        }
    
    }
  • 相关阅读:
    53、Gif 控件GifView 的使用,播放gif图片
    52、图片缩放库 PhotoView
    51、自定义View基础和原理
    Adapter适配器 final int Id 导致选中的Item不在当前界面
    Linux目录结构
    Linux包管理工具分析
    Linux 软件包安装管理
    MySQL配置详解
    MySQL 5.5.x配置文件详解
    Linux Apache2 配置介绍
  • 原文地址:https://www.cnblogs.com/tianshidechibang234/p/4535591.html
Copyright © 2020-2023  润新知