Android開發(fā)WaveLoadingView畫圖

目的

創(chuàng)建一個(gè)class實(shí)現(xiàn)WaveLoadingView進(jìn)度條動(dòng)畫

相關(guān)技術(shù)、及其使用

1、創(chuàng)建一個(gè)類實(shí)現(xiàn)CircleView繼承于View
并實(shí)現(xiàn)相應(yīng)的方法實(shí)現(xiàn)
自定義屬性:lineSize,LineColor,textColor,textSize,創(chuàng)建init方法定義文字和圓的畫筆

 public CircleView(Context context) {
        super(context);
        inti();
    }

    public CircleView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        inti();
    }
    public void inti(){
        cirlePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        cirlePaint.setColor(lineColor);
        cirlePaint.setStyle(Paint.Style.STROKE);
        cirlePaint.setStrokeWidth(lineSize);

        textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        textPaint.setColor(textColor);
        textPaint.setTextSize(textSize);
    }

2、畫圓
調(diào)用OnDraw方法:

  protected void onDraw(Canvas canvas) {
        //確定半徑
        int radius = Math.min(getWidth(),getHeight())/2-lineSize;
        //畫圓
        canvas.drawCircle(getPivotX(),getPivotY(),radius,cirlePaint);

        //畫文本
        String text = (int)(progress*100)+"%";
        //計(jì)算文本寬度
        int width = (int)textPaint.measureText(text);
        //獲取文字矩陣
        Paint.FontMetricsInt fm =textPaint.getFontMetricsInt();

        canvas.drawText(text,getPivotX()-width/2,getPivotY()+(-fm.ascent)/2,textPaint);
    }

3、定義進(jìn)度圈CircleView,貝塞爾曲線WaveView,進(jìn)度Progress
調(diào)用onlayout方法創(chuàng)建CircllieView和WaveVIew并布局:

protected void onLayout(boolean changed, int l, int t, int r, int b) {
        //創(chuàng)建CircleView
         cv = new CircleView(getContext());
        cv.setLineColor(Color.RED);
        cv.setLineSize(50);
        cv.setTextColor(Color.RED);
        cv.setCenterYSpace(-90);
        //對(duì)子視圖進(jìn)行布局
        cv.layout(0,0,getWidth(),getHeight());
        //將子視圖添加到容器中
        addView(cv);

        //創(chuàng)建WaveView
        wv = new WaveView(getContext());
        wv.setLineSize(5);
        wv.setWaveCrest(30);
        wv.setWaveLenth(100);
        wv.setLineColor(Color.BLACK);

        //布局
        wv.layout(getWidth()/4,getHeight()/2-30,getWidth()*3/4,getHeight()/2+30);
        //添加
        addView(wv);
    }

在調(diào)setProgress方法實(shí)現(xiàn)改變進(jìn)度的值

public void setProgress(float progress) {
        Progress = progress;
        //改變CircleView的進(jìn)度值
        cv.setProgress(progress);
        if(progress <=1 ){
            cv.setProgress(progress);
        }
        if((int)progress == 1.01){
            wv.stopWave();
        }
    }

PS

由于每周周末才上課的原因,有一種不情愿的小情緒,學(xué)習(xí)的畫圖主要就是創(chuàng)建畫筆和畫板,同時(shí)調(diào)用onDraw畫圖,畫圖同時(shí)還需注意圖的布局。

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

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

  • 【Android 動(dòng)畫】 動(dòng)畫分類補(bǔ)間動(dòng)畫(Tween動(dòng)畫)幀動(dòng)畫(Frame 動(dòng)畫)屬性動(dòng)畫(Property ...
    Rtia閱讀 6,425評(píng)論 1 38
  • 1. Outline 本文主要從以下三個(gè)大的方面來說明一下2D Graphic 繪圖的一些相關(guān)函數(shù)及應(yīng)用。 Col...
    lee_3do閱讀 3,364評(píng)論 0 11
  • Swift1> Swift和OC的區(qū)別1.1> Swift沒有地址/指針的概念1.2> 泛型1.3> 類型嚴(yán)謹(jǐn) 對(duì)...
    cosWriter閱讀 11,680評(píng)論 1 32
  • iOS開發(fā)系列--網(wǎng)絡(luò)開發(fā) 概覽 大部分應(yīng)用程序都或多或少會(huì)牽扯到網(wǎng)絡(luò)開發(fā),例如說新浪微博、微信等,這些應(yīng)用本身可...
    lichengjin閱讀 4,051評(píng)論 2 7
  • 一、簡歷準(zhǔn)備 1、個(gè)人技能 (1)自定義控件、UI設(shè)計(jì)、常用動(dòng)畫特效 自定義控件 ①為什么要自定義控件? Andr...
    lucas777閱讀 5,393評(píng)論 2 54

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