自定義View之IndexView進(jìn)度條(一)

最近項(xiàng)目有個(gè)找回密碼的狀態(tài)進(jìn)度條顯示需求:

項(xiàng)目需求

以前遇到這種情況,基本上都是直接讓美工幫忙實(shí)現(xiàn)的盖灸,你懂的蚁鳖,現(xiàn)在想研究一下自定義View,然后就開(kāi)始挖坑填坑了赁炎,不過(guò)總體感覺(jué)這個(gè)UI其實(shí)實(shí)現(xiàn)原理比較簡(jiǎn)單醉箕,最后還是很不愉快地實(shí)現(xiàn)了,并且可以動(dòng)態(tài)擴(kuò)展徙垫,支持動(dòng)態(tài)配置步驟的數(shù)量讥裤,看一下實(shí)現(xiàn)的效果

最終實(shí)現(xiàn)的效果:

效果圖

關(guān)于自定義View的知識(shí)這里就不再多說(shuō)了,這個(gè)View沒(méi)有子控件姻报,所以選擇集成系統(tǒng)的View己英,整個(gè)界面其實(shí)看上去比較簡(jiǎn)單,就是畫(huà)圓吴旋,畫(huà)線损肛,畫(huà)文字厢破,只是想分享一下自己在實(shí)現(xiàn)的過(guò)程中遇到的一些問(wèn)題,雖然界面上有好多需求治拿。

實(shí)現(xiàn)步驟

1.自定義屬性

因?yàn)樾枰L制的控件比較多摩泪,所以涉及到的顏色跟間距需要進(jìn)行屬性聲明,然后由于考慮到控件的擴(kuò)展性劫谅,以后可能會(huì)涉及到更多的步驟见坑,所以步驟的數(shù)量也是動(dòng)態(tài)配置的,并沒(méi)有寫(xiě)死捏检,只需要配置好相應(yīng)的數(shù)據(jù)源荞驴,就可以動(dòng)態(tài)地生成相應(yīng)的控件,屬性的命名比較規(guī)范未檩,看名字就能知道:

   <!--自定義屬性集合:ProgressView-->
    <declare-styleable name="PasswordView">
        <attr name="circle_color_checked" format="color"/>
        <attr name="circle_color_unchecked" format="color"/>
        <attr name="number_color_checked" format="color"/>
        <attr name="number_color_unchecked" format="color"/>
        <attr name="line_color" format="color"/>
        <attr name="text_color" format="color"/>
        <attr name="text_size" format="dimension"/>
        <attr name="text_padding" format="dimension"/>
        <attr name="circle_radius" format="dimension"/>
        <attr name="edge_line_width" format="dimension"/>
        <attr name="center_line_width" format="dimension"/>
        <attr name="topName" format="reference"/>
        <attr name="bottomName" format="reference"/>
        <attr name="checkedNumber" format="integer"/>
    </declare-styleable>

需要說(shuō)明的是bottomName和topName這兩個(gè)屬性戴尸,對(duì)應(yīng)的是數(shù)組id,也就是上面的數(shù)字?jǐn)?shù)組跟下面的文字?jǐn)?shù)組:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="topNames">
        <item>1</item>
        <item>2</item>
        <item>3</item>
    </string-array>

    <string-array name="bottomNames">
        <item>驗(yàn)證手機(jī)</item>
        <item>重設(shè)密碼</item>
        <item>重設(shè)成功</item>
    </string-array>
</resources>

然后是在布局文件中引用

 app:topName="@array/topNames"
 app:bottomName="@array/bottomNames"

2.在代碼中獲取相應(yīng)的屬性

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.PasswordView);
        mCircleColorChecked = typedArray.getColor(R.styleable.PasswordView_circle_color_checked, Color.RED);
        mCircleColorUnchecked = typedArray.getColor(R.styleable.PasswordView_circle_color_unchecked, Color.RED);
        mNumberColorChecked = typedArray.getColor(R.styleable.PasswordView_number_color_checked, Color.RED);
        mNumberColorUnchecked = typedArray.getColor(R.styleable.PasswordView_number_color_unchecked, Color.RED);
        mTextPadding = toPx(typedArray.getDimension(R.styleable.PasswordView_text_padding, toPx(12.0f)));
        mLineColor = typedArray.getColor(R.styleable.PasswordView_line_color, Color.RED);
        mTextColor = typedArray.getColor(R.styleable.PasswordView_text_color, Color.RED);
        mCircleRadius = toPx(typedArray.getDimension(R.styleable.PasswordView_circle_radius, 0));
        mTextSize = toPx(typedArray.getDimension(R.styleable.PasswordView_text_size, 0));
        mEdgeLineWidth = toPx(typedArray.getDimension(R.styleable.PasswordView_edge_line_width, 0));
        mCenterLineWidth = toPx(typedArray.getDimension(R.styleable.PasswordView_center_line_width, 0));
        mCheckedNumber = typedArray.getInteger(R.styleable.PasswordView_checkedNumber, 0);
        int topNamesId = typedArray.getResourceId(R.styleable.PasswordView_topName, 0);
        if (topNamesId != 0)
            mTopNames = getResources().getStringArray(topNamesId);
        int bottomNamesId = typedArray.getResourceId(R.styleable.PasswordView_bottomName, 0);
        if (bottomNamesId != 0)
            mBottomNames = getResources().getStringArray(bottomNamesId);
        childNumbers = mBottomNames.length;
        typedArray.recycle();

3.onMeasure

在此方法中獲取View的寬高,這里有幾點(diǎn)要說(shuō)明一下:

 @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(measuredWidth(widthMeasureSpec), measuredHeight(heightMeasureSpec));
    }

寬度測(cè)量

  • 當(dāng)width為match_parent或者具體的長(zhǎng)度

此時(shí)的測(cè)量模式為EXACTLY冤狡,View的寬度為父容器的寬度或者指定的寬度孙蒙,這個(gè)時(shí)候需要計(jì)算的就是mCenterLineWidth,也就是中間的那幾條線段的寬度悲雳,這個(gè)寬度用來(lái)定位中間的圓的橫坐標(biāo)挎峦,它們的寬度就是整個(gè)View的寬度減去已知控件的長(zhǎng)度之后除以中間線段條數(shù)的平均值。

  • 當(dāng)width為wrap_content的時(shí)候

此時(shí)的測(cè)量模式為AT_MOST,View的寬度就是所有已知控件的長(zhǎng)度之和合瓢,此時(shí)必須指定中間線的寬度坦胶,不然沒(méi)有辦法計(jì)算View的總寬度,具體計(jì)算方式見(jiàn)下面代碼


    //測(cè)量寬度
    private int measuredWidth(int widthMeasureSpec) {
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int mode = MeasureSpec.getMode(widthMeasureSpec);
        int result = 0;
        switch (mode) {
            case MeasureSpec.EXACTLY://width為match或者具體的長(zhǎng)度
                result = width;
                //通過(guò)均分來(lái)計(jì)算中間分發(fā)現(xiàn)的寬度
                mCenterLineWidth = (result - getPaddingLeft() - getPaddingRight() - 2 * mEdgeLineWidth - 2 * mCircleRadius * childNumbers) / (childNumbers - 1);
                break;
            case MeasureSpec.AT_MOST://width為wrap
                //通過(guò)自定義屬性來(lái)計(jì)算測(cè)量的寬度
                int realWidth = getPaddingLeft() + getPaddingLeft() + 2 * mEdgeLineWidth + 2 * mCircleRadius * childNumbers + mCenterLineWidth * (childNumbers - 1);
                result = Math.min(realWidth, width);
                break;
        }
        return result;
    }

    

高度測(cè)量

  • 當(dāng)height為match_parent或者具體的長(zhǎng)度

此時(shí)的測(cè)量模式為EXACTLY晴楔,View的高度為父容器的高度或者指定的高度

  • 當(dāng)width為wrap_content的時(shí)候

此時(shí)的測(cè)量模式為AT_MOST,View的高度就是所有已知控件的長(zhǎng)度之和顿苇,此時(shí)必須指定mTextPadding的高度,不然沒(méi)有辦法計(jì)算View的總高度税弃,具體計(jì)算方式見(jiàn)下面代碼

//測(cè)量高度
    private int measuredHeight(int heightMeasureSpec) {
        int height = MeasureSpec.getSize(heightMeasureSpec);
        int mode = MeasureSpec.getMode(heightMeasureSpec);
        int result = 0;
        switch (mode) {
            case MeasureSpec.EXACTLY://height為match或者具體的長(zhǎng)度
                result = height;
                break;
            case MeasureSpec.AT_MOST://height為wrap_content
                int realHeight = getPaddingTop() + getPaddingBottom() + 2 * mCircleRadius + mTextPadding + mTextHeight;
                result = Math.min(height, realHeight);
                break;
        }
        return result;
    }

4.onDraw

這里有一點(diǎn)需要注意的纪岁,就是要先畫(huà)線,再畫(huà)圓则果,而且都需要是實(shí)心圓幔翰,這樣線只需要畫(huà)一次,因?yàn)閳A可以把線給蓋住西壮,左端點(diǎn)到右端點(diǎn)遗增,不然需要繪制好幾段,特別麻煩款青,所以要注意策略的選擇做修,由于步驟的數(shù)量的不確定性導(dǎo)致圓心以及文字的起始坐標(biāo)都需要?jiǎng)討B(tài)計(jì)算,動(dòng)態(tài)繪制,這里需要簡(jiǎn)單計(jì)算一下缓待,也不是很麻煩蚓耽,

  • float cx = getPaddingLeft() + mEdgeLineWidth + mCircleRadius + i * (mCenterLineWidth + 2 * mCircleRadius);//圓心橫坐標(biāo)
  • float cy = getPaddingTop() + mCircleRadius;//圓心縱坐標(biāo)

圓心確定了,周?chē)目丶捕急容^好確定了旋炒,坐標(biāo)訂好了步悠,就直接進(jìn)行繪制了,感覺(jué)自定義控件就是各種計(jì)算padding瘫镇,margin之類(lèi)鼎兽,真正需要繪制的并不是很復(fù)雜,當(dāng)然可能我這個(gè)自定義控件比較簡(jiǎn)單铣除,只有UI效果谚咬,沒(méi)有交互邏輯,基本上到這兒已經(jīng)完成了尚粘,放一下繪制的代碼:

 @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        float startX = getPaddingLeft();
        float endX = getMeasuredWidth() - getPaddingRight();
        float startY = getPaddingTop() + mCircleRadius;
        canvas.drawLine(startX,startY,endX,startY,mPaint);
        //畫(huà)圓及圓中的數(shù)字
        for (int i = 0; i < mTopNames.length; i++) {
            float cx = getPaddingLeft() + mEdgeLineWidth + mCircleRadius + i * (mCenterLineWidth + 2 * mCircleRadius);//圓心橫坐標(biāo)
            float cy = getPaddingTop() + mCircleRadius;//圓心縱坐標(biāo)
            float baseNumberX = cx - mNumberWidth / 2;//數(shù)字文本框的左上定點(diǎn)
            float baseNumberY = cy + mNumberHeight / 2 - mFontMetrics.bottom;//文字文本框的基線
            float baseTextX = cx - mTextWidth / 2;//文字文本框的左上定點(diǎn)
            float baseTextY = getHeight() - getPaddingBottom() - mFontMetrics.bottom;//文字文本框的基線
            if (i == mCheckedNumber) {
                //畫(huà)實(shí)心圓
                mPaint.setColor(mCircleColorChecked);
                mPaint.setStyle(Paint.Style.FILL);
                canvas.drawCircle(cx, cy, mCircleRadius, mPaint);
                //描邊
                mPaint.setColor(mCircleColorChecked);
                mPaint.setStyle(Paint.Style.STROKE);
                mPaint.setStrokeWidth(2);
                canvas.drawCircle(cx, cy, mCircleRadius, mPaint);
                //畫(huà)數(shù)字
                mPaint.setStyle(Paint.Style.FILL);
                mPaint.setColor(mNumberColorChecked);
                canvas.drawText(mTopNames[i], baseNumberX, baseNumberY, mPaint);
            } else {
                //畫(huà)空心圓
                mPaint.setColor(mCircleColorUnchecked);
                mPaint.setStyle(Paint.Style.FILL);
                mPaint.setStrokeWidth(2);
                canvas.drawCircle(cx, cy, mCircleRadius, mPaint);
                //描邊
                mPaint.setColor(mNumberColorUnchecked);
                mPaint.setStyle(Paint.Style.STROKE);
                mPaint.setStrokeWidth(2);
                canvas.drawCircle(cx, cy, mCircleRadius, mPaint);
                //畫(huà)數(shù)字
                mPaint.setStyle(Paint.Style.FILL);
                mPaint.setColor(mNumberColorUnchecked);
                canvas.drawText(mTopNames[i], baseNumberX, baseNumberY, mPaint);

            }
            //畫(huà)文字
            mPaint.setColor(mTextColor);
            mPaint.setStyle(Paint.Style.FILL);
            canvas.drawText(mBottomNames[i], baseTextX, baseTextY, mPaint);
            mPaint.setColor(mLineColor);
            mPaint.setStyle(Paint.Style.FILL);
            mPaint.setStrokeWidth(3);

        }
    }

5.使用


    <com.fatchao.passwordview.PasswordView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white"
        android:paddingBottom="@dimen/dp_10"
        android:paddingLeft="@dimen/dp_10"
        android:paddingRight="@dimen/dp_10"
        android:paddingTop="@dimen/dp_15"
        app:bottomName="@array/bottomNames"
        app:checkedNumber="1"
        app:circle_color_checked="@color/grey"
        app:circle_color_unchecked="@color/white"
        app:circle_radius="15dp"
        app:edge_line_width="30dp"
        app:line_color="@color/grey"
        app:number_color_checked="@color/white"
        app:number_color_unchecked="@color/black"
        app:text_color="@color/grey"
        app:text_padding="@dimen/dp_12"
        app:text_size="14sp"
        app:topName="@array/topNames"
        />

點(diǎn)擊下載Demo

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末择卦,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子郎嫁,更是在濱河造成了極大的恐慌秉继,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,185評(píng)論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件泽铛,死亡現(xiàn)場(chǎng)離奇詭異尚辑,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)盔腔,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,652評(píng)論 3 393
  • 文/潘曉璐 我一進(jìn)店門(mén)杠茬,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人弛随,你說(shuō)我怎么就攤上這事瓢喉。” “怎么了舀透?”我有些...
    開(kāi)封第一講書(shū)人閱讀 163,524評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵灯荧,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我盐杂,道長(zhǎng),這世上最難降的妖魔是什么哆窿? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,339評(píng)論 1 293
  • 正文 為了忘掉前任链烈,我火速辦了婚禮,結(jié)果婚禮上挚躯,老公的妹妹穿的比我還像新娘强衡。我一直安慰自己,他們只是感情好码荔,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,387評(píng)論 6 391
  • 文/花漫 我一把揭開(kāi)白布漩勤。 她就那樣靜靜地躺著感挥,像睡著了一般。 火紅的嫁衣襯著肌膚如雪越败。 梳的紋絲不亂的頭發(fā)上触幼,一...
    開(kāi)封第一講書(shū)人閱讀 51,287評(píng)論 1 301
  • 那天,我揣著相機(jī)與錄音究飞,去河邊找鬼置谦。 笑死,一個(gè)胖子當(dāng)著我的面吹牛亿傅,可吹牛的內(nèi)容都是我干的媒峡。 我是一名探鬼主播,決...
    沈念sama閱讀 40,130評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼葵擎,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼谅阿!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起酬滤,我...
    開(kāi)封第一講書(shū)人閱讀 38,985評(píng)論 0 275
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤签餐,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后敏晤,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體贱田,經(jīng)...
    沈念sama閱讀 45,420評(píng)論 1 313
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,617評(píng)論 3 334
  • 正文 我和宋清朗相戀三年嘴脾,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了男摧。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,779評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡译打,死狀恐怖耗拓,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情奏司,我是刑警寧澤乔询,帶...
    沈念sama閱讀 35,477評(píng)論 5 345
  • 正文 年R本政府宣布,位于F島的核電站韵洋,受9級(jí)特大地震影響竿刁,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜搪缨,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,088評(píng)論 3 328
  • 文/蒙蒙 一食拜、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧副编,春花似錦负甸、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,716評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)打月。三九已至,卻和暖如春蚕捉,著一層夾襖步出監(jiān)牢的瞬間奏篙,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,857評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工鱼冀, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留报破,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,876評(píng)論 2 370
  • 正文 我出身青樓千绪,卻偏偏與公主長(zhǎng)得像充易,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子荸型,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,700評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容