Android 自定義View—清爽小巧靈活的多節點進度條

来源:https://www.cnblogs.com/DMingO/archive/2020/05/05/12789840.html
-Advertisement-
Play Games

前言 最近項目有一個節點進度條的小需求,完成後,想分享出來希望可以幫到有需要的同學。 真機效果圖 自定義View完整代碼 開箱即用~,註釋已經炒雞詳細了 註意點 1. 控制項的節點總個數是與傳入的節點底部標題列表中元素個數控制(相同)的,簡而言之就是傳入的標題列表中有多少個標題,節點就會繪製多少個 2 ...


前言

最近項目有一個節點進度條的小需求,完成後,想分享出來希望可以幫到有需要的同學。

真機效果圖

自定義View完整代碼

開箱即用~,註釋已經炒雞詳細了

/**
 * @description: 節點進度條
 * @author: DMingO
 * @date: 2020/4/15
 */
public class PointProcessBar extends View {

    /**
     * 未選中時的連線畫筆
     */
    private Paint mLinePaint;
    /**
     * 選中時的連線畫筆
     */
    private Paint mLineSelectedPaint;
    /**
     * 未選中時的文字畫筆
     */
    private Paint mTextPaint;
    /**
     * 選中時的文字畫筆
     */
    private Paint mTextSelPaint;

    /**
     * 未選中時的實心圓畫筆
     */
    private Paint mCirclePaint;
    /**
     * 選中時的內部實心圓畫筆
     */
    private Paint mCircleSelPaint;
    /**
     * 選中時的邊框圓畫筆
     */
    private Paint mCircleStrokeSelPaint;

    /**
     * 未選中時的線,節點圓的顏色
     */
    private int mColorUnselected  = Color.parseColor("#1ca8b0d9");
    /**
     * 選中時的顏色
     */
    private int mColorSelected = Color.parseColor("#61A4E4");
    /**
     * 未選中的文字顏色
     */
    private int mColorTextUnselected  = Color.parseColor("#5c030f09");

    /**
     * 繪製的節點個數,由底部節點標題數量控制
     */
    int circleCount ;

    /**
     * 連線的高度
     */
    float mLineHeight = 7f;

    //圓的直徑
    float mCircleHeight = 50f;
    float mCircleSelStroke = 8f;
    float mCircleFillRadius = 15f;

    //文字大小
    float mTextSize  = 35f;

    //文字離頂部的距離
    float mMarginTop = 40f;
    /**
     * 首個圓向中心偏移的距離
     */
    float marginLeft = 30f;

    /**
     * 最後一個圓向中心偏移的距離
     */
    float marginRight = marginLeft;

    /**
     * 每個節點相隔的距離
     */
    float divideWidth;

    int defaultHeight;

    /**
     * 節點底部的文字列表
     */
    List<String> textList = new ArrayList<>();

    /**
     * 文字同寬高的矩形,用來測量文字
     */
    List<Rect> mBounds;
    /**
     * 存儲每個圓心在同一直線上的節點圓的 x 坐標值
     */
    List<Float> circleLineJunctions = new ArrayList<>();
    /**
     * 選中項集合
     */
    Set<Integer> selectedIndexSet = new HashSet<>();

    public PointProcessBar(Context context) {
        super(context);
    }

    public PointProcessBar(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initPaint();
    }

    public PointProcessBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public PointProcessBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    /**
     * 初始化畫筆屬性
     */
    private void initPaint(){

        mLinePaint = new Paint();
        mLineSelectedPaint = new Paint();
        mCirclePaint = new Paint();
        mTextPaint = new Paint();
        mCircleStrokeSelPaint = new Paint();
        mTextSelPaint=new Paint();
        mCircleSelPaint = new Paint();

        mLinePaint.setColor(mColorDef);
        //設置填充
        mLinePaint.setStyle(Paint.Style.FILL);
        //筆寬像素
        mLinePaint.setStrokeWidth(mLineHeight);
        //鋸齒不顯示
        mLinePaint.setAntiAlias(true);

        mLineSelectedPaint.setColor(mColorSelected);
        mLineSelectedPaint.setStyle(Paint.Style.FILL);
        mLineSelectedPaint.setStrokeWidth(mLineHeight);
        mLineSelectedPaint.setAntiAlias(true);

        mCirclePaint.setColor(mColorDef);
        //設置填充
        mCirclePaint.setStyle(Paint.Style.FILL);
        //筆寬像素
        mCirclePaint.setStrokeWidth(1);
        //鋸齒不顯示
        mCirclePaint.setAntiAlias(true);

        //選中時外框空心圓圈畫筆
        mCircleStrokeSelPaint.setColor(mColorSelected);
        mCircleStrokeSelPaint.setStyle(Paint.Style.STROKE);
        mCircleStrokeSelPaint.setStrokeWidth(mCircleSelStroke);
        mCircleStrokeSelPaint.setAntiAlias(true);
        //選中時的內部填充圓畫筆
        mCircleSelPaint.setStyle(Paint.Style.FILL);
        mCircleSelPaint.setStrokeWidth(1);
        mCircleSelPaint.setAntiAlias(true);
        mCircleSelPaint.setColor(mColorSelected);

        //普通狀態的文本 畫筆
        mTextPaint.setTextSize(mTextSize);
        mTextPaint.setColor(mColorTextDef);
        mTextPaint.setAntiAlias(true);
        mTextPaint.setTextAlign(Paint.Align.CENTER);
        //選中後的文本畫筆
        mTextSelPaint.setTextSize(mTextSize);
        mTextSelPaint.setColor(mColorSelected);
        mTextSelPaint.setAntiAlias(true);
        mTextSelPaint.setTextAlign(Paint.Align.CENTER);
    }

    /**
     * 測量文字的長寬,將文字視為rect矩形
     */
    private void measureText(){
        mBounds = new ArrayList<>();
        for(String name : textList){
            Rect mBound = new Rect();
            mTextPaint.getTextBounds(name, 0, name.length(), mBound);
            mBounds.add(mBound);
        }
    }




    /**
     * 測量view的高度
     */
    private void measureHeight(){
        if (mBounds!=null && mBounds.size()!=0) {
            defaultHeight = (int) (mCircleHeight + mMarginTop + mCircleSelStroke + mBounds.get(0).height()/2);
        } else {
            defaultHeight  = (int) (mCircleHeight + mMarginTop+mCircleSelStroke);
        }
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
        //寬高都設置為wrap_content
       if(widthSpecMode == MeasureSpec.AT_MOST && heightSpecMode == MeasureSpec.AT_MOST){
            //寬設置為wrap_content
            setMeasuredDimension(widthSpecSize,defaultHeight);
        }else if(widthSpecMode == MeasureSpec.AT_MOST){
            setMeasuredDimension(widthSpecSize,heightSpecSize);
        }else if(heightSpecMode == MeasureSpec.AT_MOST){
            //高設置為wrap_content
            setMeasuredDimension(widthSpecSize, defaultHeight);
        }else{
            //寬高都設置為match_parent或具體的dp值
            setMeasuredDimension(widthSpecSize, heightSpecSize);
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        //若未設置節點標題或者選中項的列表,則取消繪製
        if (textList == null || textList.isEmpty() ||
                selectedIndexSet == null || selectedIndexSet.isEmpty() ||
                mBounds == null || mBounds.isEmpty()) {
            return;
        }
        //畫灰色圓圈的個數
        circleCount=textList.size();
        //每個圓相隔的距離(重要),可以通過這個調節節點間距
        divideWidth = (getWidth() - mCircleHeight ) / (circleCount - 1);
        //繪製文字和圓形
        for (int i=0; i < circleCount ;i++){
            float cx;
            float cy;
            float textX;
            if (i==0){
                //第一個節點,圓心需要向右偏移
                cx = mCircleHeight / 2 + i * divideWidth + marginLeft;
                cy = mCircleHeight / 2 + mCircleSelStroke;
                textX = cx;
                circleLineJunctions.add(cx + mCircleHeight / 2);
            }else if (i==textList.size()-1){
                //最後一個節點,圓心需要向左偏移
                cx = mCircleHeight / 2 + i * divideWidth - marginRight;
                cy = mCircleHeight / 2 + mCircleSelStroke;
                textX = cx;
                circleLineJunctions.add(cx - mCircleHeight / 2);
            }else {
                //中間部分的節點
                cx = mCircleHeight / 2 + i * divideWidth;
                cy = mCircleHeight / 2+mCircleSelStroke;
                textX = cx;
                circleLineJunctions.add(cx - mCircleHeight / 2);
                circleLineJunctions.add(cx + mCircleHeight / 2);
            }
            if (getSelectedIndexSet().contains(i)){
                //若當前位置節點被包含在選中項Set中,判定此節點被選中
                canvas.drawCircle(cx , cy, mCircleHeight / 2, mCircleStrokeSelPaint);
                canvas.drawCircle(cx, cy, mCircleFillRadius, mCircleSelPaint);
                canvas.drawText(textList.get(i), textX, (float) (mCircleHeight + mMarginTop +mCircleSelStroke+mBounds.get(i).height()/2.0), mTextSelPaint);
            }else {
                //若當前位置節點沒有被包含在選中項Set中,判定此節點沒有被選中
                canvas.drawCircle(cx , cy, mCircleHeight / 2, mCirclePaint);
                canvas.drawText(textList.get(i), textX, (float) (mCircleHeight + mMarginTop +mCircleSelStroke+mBounds.get(i).height()/2.0), mTextPaint);
            }
        }
        for(int i = 1 , j = 1 ; j <= circleLineJunctions.size() && ! circleLineJunctions.isEmpty()  ; ++i , j=j+2){
            if(getSelectedIndexSet().contains(i)){
                canvas.drawLine(circleLineJunctions.get(j-1),mCircleHeight/2+mCircleSelStroke,
                        circleLineJunctions.get(j) ,mCircleHeight/2+mCircleSelStroke,mLineSelectedPaint);
            }else {
                canvas.drawLine(circleLineJunctions.get(j-1),mCircleHeight/2+mCircleSelStroke,
                        circleLineJunctions.get(j) ,mCircleHeight/2+mCircleSelStroke,mLinePaint);
            }
        }
    }

    /**
     * 供外部調用,顯示控制項
     * @param titles 底部標題內容列表
     * @param indexSet 選中項Set
     */
    public void show(List<String> titles , Set<Integer> indexSet){
        if(titles != null && ! titles.isEmpty()){
            this.textList = titles;
        }
        if(indexSet != null  && ! indexSet.isEmpty()){
            this.selectedIndexSet = indexSet;
        }
        measureText();
        measureHeight();
        //繪製
        invalidate();
    }

    /**
     * 更新底部節點標題內容
     * @param textList 節點標題內容列表
     */
    public void refreshTextList(List<String> textList) {
        this.textList = textList;
        measureText();
        measureHeight();
        invalidate();
    }

    /**
     * 獲取節點選中狀態
     * @return 節點選中狀態列表
     */
    public Set<Integer> getSelectedIndexSet() {
        return selectedIndexSet;
    }

    /**
     * 更新選中項
     * @param set 選中項Set
     */
    public void refreshSelectedIndexSet(Set<Integer> set) {
        this.selectedIndexSet = set;
        invalidate();
    }


}

註意點

  1. 控制項的節點總個數是與傳入的節點底部標題列表中元素個數控制(相同)的,簡而言之就是傳入的標題列表中有多少個標題,節點就會繪製多少個
  2. 控制項通過show方法進行View的初始化和顯示內容,傳入節點標題列表和節點選中項集合,控制View的選中狀態和顯示的內容
  3. 控制項初始化顯示後,可以通過refreshTextList(),refreshSelectedIndexSet() 更新標題和選中項
  4. 具體不同的顏色,大小可以具體在View中調整

總結

可以看到效果不複雜,因此自定義View的代碼行數不多,也很容易看懂,直接拿走代碼即可在項目中食用啦。

由於不同項目設計稿會有不同,這裡也僅僅給有需要的同學一個思路,可以改造具體實現代碼~

謝謝閱讀到這裡的同學~歡迎與我討論和交流


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • @2020.5.5 練習:賬號信息表,用戶組,主機表,主機組 ...
  • 【目錄】 一 系統資料庫 二 創建資料庫 三 資料庫相關操作 一 系統資料庫 information_schema: 虛擬庫,不占用磁碟空間,存儲的是資料庫啟動後的一些參數,如用戶表信息、列信息、許可權信息、字元信息等 performance_schema: MySQL 5.5開始新增一個資料庫,主要 ...
  • 伴隨著Redis6.0的發佈,作為最令人怦然心動的特性之一,Redis官方同時推出Redis集群的proxy了:redis-cluster-proxy,https://github.com/RedisLabs/redis-cluster-proxy 相比從前訪問Redis集群時需要制定集群中所有的I ...
  • mysql命令gruop by報錯this is incompatible with sql_mode=only_full_group_by 出現這個錯誤已導致在開發中mybatis的sql也運行不了 原因: 看一下group by的語法: select 選取分組中的列+聚合函數 from 表名稱 ...
  • 目錄:andorid jar/庫源碼解析 RxJava2: 作用: 通過提供一種,觀察者和訂閱者的模式,的架構,來優化邏輯流程。適用於複雜和需要多數據轉換和長流程。 慄子: 定義三個對象類 public class ResultInfo { public int code; public Strin ...
  • 目錄:andorid jar/庫源碼解析 Bolts: 作用: 用於鏈式執行跨線程代碼,且傳遞數據 慄子: Task.call(new Callable<Boolean>() { @Override public Boolean call() throws Exception { return tr ...
  • 快速接入訊飛語音聽寫SDK(內附空指針解決和修改對話框文字方法) ...
  • 簡單又詳細,Android Library 發佈開源庫 JCenter & JitPack 攻略~ ...
一周排行
    -Advertisement-
    Play Games
  • 前言 插件化的需求主要源於對軟體架構靈活性的追求,特別是在開發大型、複雜或需要不斷更新的軟體系統時,插件化可以提高軟體系統的可擴展性、可定製性、隔離性、安全性、可維護性、模塊化、易於升級和更新以及支持第三方開發等方面的能力,從而滿足不斷變化的業務需求和技術挑戰。 一、插件化探索 在WPF中我們想要開 ...
  • 歡迎ReaLTaiizor是一個用戶友好的、以設計為中心的.NET WinForms項目控制項庫,包含廣泛的組件。您可以使用不同的主題選項對項目進行個性化設置,並自定義用戶控制項,以使您的應用程式更加專業。 項目地址:https://github.com/Taiizor/ReaLTaiizor 步驟1: ...
  • EDP是一套集組織架構,許可權框架【功能許可權,操作許可權,數據訪問許可權,WebApi許可權】,自動化日誌,動態Interface,WebApi管理等基礎功能於一體的,基於.net的企業應用開發框架。通過友好的編碼方式實現數據行、列許可權的管控。 ...
  • Channel 是乾什麼的 The System.Threading.Channels namespace provides a set of synchronization data structures for passing data between producers and consume ...
  • efcore如何優雅的實現按年分庫按月分表 介紹 本文ShardinfCore版本 本期主角: ShardingCore 一款ef-core下高性能、輕量級針對分表分庫讀寫分離的解決方案,具有零依賴、零學習成本、零業務代碼入侵適配 距離上次發文.net相關的已經有很久了,期間一直在從事java相關的 ...
  • 前言 Spacesniffer 是一個免費的文件掃描工具,通過使用樹狀圖可視化佈局,可以立即瞭解大文件夾的位置,幫助用戶處理找到這些文件夾 當前系統C盤空間 清理後系統C盤空間 下載 Spacesniffer 下載地址:https://spacesniffer.en.softonic.com/dow ...
  • EDP是一套集組織架構,許可權框架【功能許可權,操作許可權,數據訪問許可權,WebApi許可權】,自動化日誌,動態Interface,WebApi管理等基礎功能於一體的,基於.net的企業應用開發框架。通過友好的編碼方式實現數據行、列許可權的管控。 ...
  • 一、ReZero簡介 ReZero是一款.NET中間件 : 全網唯一開源界面操作就能生成API , 可以集成到任何.NET6+ API項目,無破壞性,也可讓非.NET用戶使用exe文件 免費開源:MIT最寬鬆協議 , 一直從事開源事業十年,一直堅持開源 1.1 純ReZero開發 適合.Net Co ...
  • 一:背景 1. 講故事 停了一個月沒有更新文章了,主要是忙於寫 C#內功修煉系列的PPT,現在基本上接近尾聲,可以回頭繼續更新這段時間分析dump的一些事故報告,有朋友微信上找到我,說他們的系統出現了大量的http超時,程式不響應處理了,讓我幫忙看下怎麼回事,dump也抓到了。 二:WinDbg分析 ...
  • 開始做項目管理了(本人3年java,來到這邊之後真沒想到...),天天開會溝通整理需求,他們講話的時候忙裡偷閑整理一下常用的方法,其實語言還是有共通性的,基本上看到方法名就大概能猜出來用法。出去打水的時候看到外面太陽好好,真想在外面坐著曬太陽,回來的時候好兄弟三年前送給我的鍵盤D鍵不靈了,在打"等待 ...