Android 自定义OrderView

先上个图:

Android 自定义OrderView_第1张图片
image

我将整个View分为三个大的模块,并对他们分别进行绘制。包块外面有一个完整的圆环、一段弧(弧度的终点是一个小圆) 以及中间的文字

首先是外面的圆环,圆环很简单直接调用canvas.drawCircle(),这个方法提供圆心半径和画笔就ok。

private void drawBlackCircle(Canvas canvas){

mPaint.setColor(Color.parseColor("#535353"));

mPaint.setStrokeWidth(circleRingWidth);

mPaint.setStyle(Paint.Style.STROKE);

canvas.drawCircle(getWidth()/2,getHeight()/2,Math.min(getWidth(),getHeight())/2-ringWidth,mPaint);

}
(2)弧

外面的弧带颜色的弧,首先要确定画弧的位置,API里给我们提供的确定弧度位置的方式是一个RectF也就是一个矩形的区域;当View的形状不为正方形的时候 长度和宽度不相同的时候,我们要取小边,也就是长度和宽度里比较短的作为半径,并且弧始终出去view的中心。

Android 自定义OrderView_第2张图片
image

正常的弧度位置是在如图位置0度,顺时针方向叠加相应的度数绘制。而当前我们绘制的view是正方位置逆时针绘制的。所以要对canvas进行旋转处理,绘制弧度。具体代码如下

  private void drawRing(Canvas canvas){
        mPaint.setColor(ringColor);
        mPaint.setStrokeWidth(ringWidth);
        mPaint.setStyle(Paint.Style.STROKE);
        canvas.save();
        canvas.rotate(-90,getWidth()/2,getHeight()/2) ;
        int radus = Math.min(getWidth(),getHeight())/2 -ringWidth;
        RectF rectF = new RectF(getWidth()/2-radus,getHeight()/2-radus,getWidth()/2+radus,getHeight()/2+radus);
        canvas.drawArc(rectF,0,deGree*-1,false,mPaint);
        canvas.restore();
    }
    
(3)内容文字

上半部分文字使用了自定义的字体,具体设置的方式参照代码。
下半部分由于字体个数不确定在适当的时候需要换行,使用StaticLayout 可实现这一效果,需要注意的是StaticLayout 不能指定绘制的位置 所以需要通过移动画布实现这一效果。

    private void drawOrderNumberText(Canvas canvas){
        /*
         * 上半部分
         */
        Typeface typeface = Typeface.createFromAsset(getContext().getAssets(),"fonts/DIN1451EF_EngAlt.otf");
        textPaint.setTypeface(typeface);
        textPaint.setTextSize(ScreenUtil.sp2px(orderTextSize));
        canvas.drawText(orderNum,getWidth()/2-textPaint.measureText(orderNum),getHeight()/2-ScreenUtil.dip2px(totalTextSize)/3,textPaint);

        textPaint.setTextSize(ScreenUtil.sp2px(totalTextSize));
        textPaint.setColor(Color.parseColor("#bbbbbb"));
        canvas.drawText("/"+totalNum,getWidth()/2,getHeight()/2-ScreenUtil.dip2px(totalTextSize)/3,textPaint);

        /*
         * 中间线
         */
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(ScreenUtil.dip2px(1));
        mPaint.setColor(Color.parseColor("#cccccc"));
        int radius = Math.min(getWidth(),getHeight())/2;
        canvas.drawLine((getWidth()/2-radius)+ringWidth*2,getHeight()/2,getWidth()/2+radius-ringWidth*2,getHeight()/2,mPaint);

        /*
         * 下半部分
         */
        canvas.save();
        textPaint.setTextSize(ScreenUtil.sp2px(nameTextSize));
        textPaint.setTypeface(Typeface.DEFAULT);
        textPaint.setColor(Color.parseColor("#6f6f6f"));
        canvas.translate(getWidth()/2-getWidth()/4,getHeight()/2);
        StaticLayout staticLayout = new StaticLayout(workName,textPaint,getWidth()/2, Layout.Alignment.ALIGN_CENTER,1.0f,0f,true);
        staticLayout.draw(canvas);
        canvas.restore();
    }

最后完成的代码

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.support.annotation.Nullable;
import android.text.Layout;
import android.text.StaticLayout;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import java.util.Queue;

import weshape.com.ifactory.util.ScreenUtil;

/**
 * Created by 李不凡 on 2017/5/10.
 */

public class OrderCircleView extends View {

    private int deGree = 90;
    private int ringWidth = ScreenUtil.dip2px(8);
    private int circleRingWidth =ScreenUtil.dip2px(2);
    private int ringColor = Color.parseColor("#FB0103");

    private int nameTextSize = 13;
    private int totalTextSize = 14;
    private int orderTextSize = 30;
    private String totalNum = "";
    private String orderNum = "";
    private String workName = "";

    /**
     * 初始化数据
     */
    public  void initData(int totalNum,int orderNum,String workName,int ringColor){
        this.totalNum = totalNum+"";
        this.orderNum = String.format("%02d",orderNum);
        this.workName = workName;
        this.ringColor = ringColor;
        this.deGree = (int) (360*(orderNum/(totalNum*1f)));
        Log.d("drummor",orderNum/(totalNum*0.1f)+"deg"+deGree);
        invalidate();
    }

    /**
     *
     * @param ringWidth 外面狐的宽度
     * @param circleRingWidth 下面圆环的宽度
     */
    public void setting(int ringWidth,int circleRingWidth){
        this.ringWidth = ringWidth;
        this.circleRingWidth = circleRingWidth;
        invalidate();

    }


    /**
     * 画环
     * @param canvas
     */
    private void drawBlackCircle(Canvas canvas){
        mPaint.setColor(Color.parseColor("#535353"));
        mPaint.setStrokeWidth(circleRingWidth);
        mPaint.setStyle(Paint.Style.STROKE);
        canvas.drawCircle(getWidth()/2,getHeight()/2,Math.min(getWidth(),getHeight())/2-ringWidth,mPaint);
    }



    /**
     * 画狐
     * @param canvas
     */
    private void drawRing(Canvas canvas){
        mPaint.setColor(ringColor);
        mPaint.setStrokeWidth(ringWidth);
        mPaint.setStyle(Paint.Style.STROKE);
        canvas.save();
        canvas.rotate(-90,getWidth()/2,getHeight()/2) ;
        int radus = Math.min(getWidth(),getHeight())/2 -ringWidth;
        RectF rectF = new RectF(getWidth()/2-radus,getHeight()/2-radus,getWidth()/2+radus,getHeight()/2+radus);
        canvas.drawArc(rectF,0,deGree*-1,false,mPaint);
        canvas.restore();
    }
    /**
     * 画球
     * @param canvas
     */
    private void drawLittePoint(Canvas canvas){
        mPaint.setColor(Color.parseColor("#f1f1f1"));
        mPaint.setStyle(Paint.Style.FILL);
        canvas.save();
        canvas.rotate(-deGree,getWidth()/2,getHeight()/2);
        canvas.drawCircle(getWidth()/2,ringWidth,ringWidth/2,mPaint);
        canvas.restore();
    }
    /**
     * 写字 内容
     * @param canvas
     */
    private void drawOrderNumberText(Canvas canvas){
        /*
         * 上半部分
         */
        Typeface typeface = Typeface.createFromAsset(getContext().getAssets(),"fonts/DIN1451EF_EngAlt.otf");
        textPaint.setTypeface(typeface);
        textPaint.setTextSize(ScreenUtil.sp2px(orderTextSize));
        canvas.drawText(orderNum,getWidth()/2-textPaint.measureText(orderNum),getHeight()/2-ScreenUtil.dip2px(totalTextSize)/3,textPaint);

        textPaint.setTextSize(ScreenUtil.sp2px(totalTextSize));
        textPaint.setColor(Color.parseColor("#bbbbbb"));
        canvas.drawText("/"+totalNum,getWidth()/2,getHeight()/2-ScreenUtil.dip2px(totalTextSize)/3,textPaint);

        /*
         * 中间线
         */
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(ScreenUtil.dip2px(1));
        mPaint.setColor(Color.parseColor("#cccccc"));
        int radius = Math.min(getWidth(),getHeight())/2;
        canvas.drawLine((getWidth()/2-radius)+ringWidth*2,getHeight()/2,getWidth()/2+radius-ringWidth*2,getHeight()/2,mPaint);

        /*
         * 下半部分
         */
        canvas.save();
        textPaint.setTextSize(ScreenUtil.sp2px(nameTextSize));
        textPaint.setTypeface(Typeface.DEFAULT);
        textPaint.setColor(Color.parseColor("#6f6f6f"));
        canvas.translate(getWidth()/2-getWidth()/4,getHeight()/2);
        StaticLayout staticLayout = new StaticLayout(workName,textPaint,getWidth()/2, Layout.Alignment.ALIGN_CENTER,1.0f,0f,true);
        staticLayout.draw(canvas);
        canvas.restore();
    }
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawBlackCircle(canvas);//绘制黑圆圈
        drawRing(canvas);
        drawLittePoint(canvas);
        drawOrderNumberText(canvas);
    }
    public OrderCircleView(Context context) {
        this(context,null);
    }
    public OrderCircleView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }
    public OrderCircleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }
    private Paint mPaint;
    private TextPaint textPaint;
    private void initView(){
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        textPaint = new TextPaint();
        textPaint.setAntiAlias(true);
        textPaint.setColor(Color.parseColor("#535353"));
        textPaint.setTextSize(ScreenUtil.dip2px(10));
    }
}

你可能感兴趣的:(Android 自定义OrderView)