Android 自定義OrderView

先上個(gè)圖:

image

我將整個(gè)View分為三個(gè)大的模塊,并對(duì)他們分別進(jìn)行繪制。包塊外面有一個(gè)完整的圓環(huán)、一段?。ɑ《鹊慕K點(diǎn)是一個(gè)小圓) 以及中間的文字

首先是外面的圓環(huán),圓環(huán)很簡單直接調(diào)用canvas.drawCircle(),這個(gè)方法提供圓心半徑和畫筆就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里給我們提供的確定弧度位置的方式是一個(gè)RectF也就是一個(gè)矩形的區(qū)域;當(dāng)View的形狀不為正方形的時(shí)候 長度和寬度不相同的時(shí)候,我們要取小邊,也就是長度和寬度里比較短的作為半徑,并且弧始終出去view的中心。

image

正常的弧度位置是在如圖位置0度,順時(shí)針方向疊加相應(yīng)的度數(shù)繪制。而當(dāng)前我們繪制的view是正方位置逆時(shí)針繪制的。所以要對(duì)canvas進(jìn)行旋轉(zhuǎn)處理,繪制弧度。具體代碼如下

  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)內(nèi)容文字

上半部分文字使用了自定義的字體,具體設(shè)置的方式參照代碼。
下半部分由于字體個(gè)數(shù)不確定在適當(dāng)?shù)臅r(shí)候需要換行,使用StaticLayout 可實(shí)現(xiàn)這一效果,需要注意的是StaticLayout 不能指定繪制的位置 所以需要通過移動(dòng)畫布實(shí)現(xiàn)這一效果。

    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 = "";

    /**
     * 初始化數(shù)據(jù)
     */
    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 下面圓環(huán)的寬度
     */
    public void setting(int ringWidth,int circleRingWidth){
        this.ringWidth = ringWidth;
        this.circleRingWidth = circleRingWidth;
        invalidate();

    }


    /**
     * 畫環(huán)
     * @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();
    }
    /**
     * 寫字 內(nèi)容
     * @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));
    }
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容