自定义流程节点View

实现的功能为,输入节点名称的字符串数组,就能自动生成流程节点。因为项目上遇到该需求,找了很多实例都不能很好的满足其可维护性,所以自己动手写一个。
本人的非常支持“不要重复发明轮子”的观点,项目的稳定性为最重要的,只有在实在没有成熟的实例控件可以用时才自己动手“发明”。
先贴一下代码:

public class LoanNodesView extends View{
    private DisplayMetrics metrics;
    private String[] nodes;
    private String[] times;
    private int currentNode;
    private Paint paint1=new Paint();
    private Paint paint2=new Paint();
    //数字画笔
    private TextPaint textPaint=new TextPaint();
    //文字画笔
    private TextPaint textPaint2=new TextPaint();
    //时间画笔
    private TextPaint textPaint3=new TextPaint();
    float width;
    float circle1;
    float circle2;

    float density;
    float screen_scaled_density;

    public LoanNodesView(Context context, AttributeSet attrs) {
        super(context, attrs);

        DisplayMetrics metric = new DisplayMetrics();
        ((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(metric);
        float screenWidth = metric.widthPixels;  // 屏幕宽度(像素)
        int screenHeight = metric.heightPixels;  // 屏幕高度(像素)
         density = metric.density;  // 屏幕密度(0.75 / 1.0 / 1.5)
        int densityDpi = metric.densityDpi;  // 屏幕密度DPI(120 / 160 / 240)

        this.width=densityDpi;
        paint1.setColor(Color.parseColor("#33000088"));
        paint2.setColor(Color.parseColor("#ff228ce2"));
        textPaint2.setColor(Color.BLACK);
        textPaint2.setTextAlign(Paint.Align.CENTER);
        textPaint2.setTextSize(12*density);

        textPaint3.setColor(Color.BLACK);
        textPaint3.setTextAlign(Paint.Align.CENTER);
        textPaint3.setTextSize(9*density);
    }

    public LoanNodesView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void setNodes(String[] nodes,int currentNode){
        this.nodes=nodes;
        this.currentNode=currentNode;
        invalidate();
    }
    public void setTimes(String[] times){
        this.times=times;
        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int totalheight=getMeasuredHeight();
        int totalwidth=getWidth();
        width=totalwidth/6;
        circle1=width/4;
        circle2=width/6;

        if(nodes!=null) {
            for (int i = 0; i < nodes.length; i++) {
                //画圆圈
                if  (i < currentNode) {
                    drawNodes(canvas, i);
                } else {
                    drawGrayNodes(canvas, i);
                }
                //划线
                if (i < currentNode-1) {drawLine(canvas, i, 1);}
                else if(i==currentNode-1&&i<nodes.length-1){drawLine(canvas,i,2);}
                else if(i<nodes.length-1){drawLine(canvas,i,3);}
                //写字
                if(i<currentNode) {
                    drawText(canvas, i);
                }else{
                    drawGrayText(canvas,i);
                }
                //写时间
                if(times!=null) {
                    drawTime(canvas, i);
                }
            }
        }
    }

    public void drawTime(Canvas canvas,int i){
        if(i<times.length&&times[i]!=null&&times[i].contains(" ")) {
            canvas.drawText(times[i].split(" ")[0], width / 2 + i * width, width / 2 + 40*density, textPaint3);
            canvas.drawText(times[i].split(" ")[1],width / 2 + i * width, width / 2 + 48*density, textPaint3);

        }
    }

    private void drawGrayNodes(Canvas canvas,int i){
        paint2.setColor(Color.GRAY);
        RectF rectF2=new RectF(width/2-circle2+width*i,width/2-circle2,width/2+circle2+width*i,width/2+circle2);
        canvas.drawOval(rectF2, paint2);
        textPaint.setTextAlign(Paint.Align.CENTER);
        textPaint.setTextSize(width/5);
        textPaint.setColor(Color.BLACK);
        textPaint.setStrokeCap(Paint.Cap.SQUARE);
        canvas.drawText(i+1+"",width/2+width*i,width/2+4*density,textPaint);
    }
    private void drawNodes(Canvas canvas,int i){
        paint1.setColor(Color.parseColor("#33000088"));
        RectF rectF=new RectF(width/2-circle1+width*i,width/2-circle1,width/2+circle1+width*i,width/2+circle1);
        canvas.drawOval(rectF, paint1);
        paint2.setColor(Color.parseColor("#ff228ce2"));
        RectF rectF2=new RectF(width/2-circle2+width*i,width/2-circle2,width/2+circle2+width*i,width/2+circle2);
        canvas.drawOval(rectF2, paint2);
        textPaint.setTextAlign(Paint.Align.CENTER);
        textPaint.setTextSize(10*density);
        textPaint.setColor(Color.WHITE);
        textPaint.setStrokeCap(Paint.Cap.SQUARE);
        canvas.drawText(i + 1 + "", width / 2 + width * i, width / 2 + 4*density, textPaint);

    }
    //type 1 2 3为三种线: 短、左短右长 长
    private void drawLine(Canvas canvas,int i,int type){
        paint1.setStrokeWidth(3*density);
        switch (type){
            case 1:
                canvas.drawLine(width / 2 + circle1 + i * width, width / 2,
                        width / 2 - circle1 + (i + 1) * width, width / 2, paint1);
                break;
            case 2:
                canvas.drawLine(width / 2 + circle1 + i * width, width / 2,
                        width / 2 - circle2 + (i + 1) * width, width / 2, paint1);
                break;
            case 3:
                canvas.drawLine(width / 2 + circle2 + i * width, width / 2,
                        width / 2 - circle2 + (i + 1) * width, width / 2, paint1);
                break;
        }
    }

    private void drawText(Canvas canvas,int i){
        textPaint2.setColor(Color.parseColor("#ff228ce2"));
        canvas.drawText(nodes[i], width / 2 + i * width, width / 2 + 30*density, textPaint2);
    }
    private void drawGrayText(Canvas canvas,int i){
        textPaint2.setColor(Color.BLACK);
        canvas.drawText(nodes[i],width/2+i*width,width/2+30*density,textPaint2);
    }

}

在使用的时候就非常简单了,类似于一般的View,通过findViewById(int)方法获取该控件的实例之后,
setNodes(String[] nodes,int currentNode)、
setTimes(String[] times)方法,会根据传入的字符串数组nodes和times的长度,生成节点。

在绘制View的过程中,考虑不同分辨率屏幕的绘制。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值