自定義審核過渡頁的實(shí)現(xiàn)

項(xiàng)目介紹

在審核某些問題的時(shí)候會(huì)出現(xiàn)加載進(jìn)度展示的view。給用戶一個(gè)感官上的效果锉走,增強(qiáng)了APP與用戶之間的交互體驗(yàn)。自定義審核過渡頁純粹就只是一個(gè)自定義view,支持展示文字以及展示圖片和進(jìn)度加載的更換翰铡。也方便了大家的使用吧。

效果展示:
result.gif
功能實(shí)現(xiàn)

1.自定義屬性

  <declare-styleable name="SmartLoadingView">
        <!-- 是否開啟抗鋸齒 -->
        <attr name="antiAlias" format="boolean" />
        <!--整個(gè)view背景-->
        <attr name="allBack" format="reference|color" />
        <!--分割線上面部分進(jìn)度條以及提示字-->
        <!--進(jìn)度條中間的圖片-->
        <attr name="circleBitmap" format="reference" />
        <!--展示icon的寬度-->
        <attr name="circleBitmapWidth" format="dimension" />
        <!--展示icon的高度-->
        <attr name="circleBitmapHeight" format="dimension" />
        <!--是否展示中間的圖片-->
        <attr name="isShowIcon" format="boolean" />
        <!--進(jìn)度條和圖片之間的距離-->
        <attr name="circlePadding" format="dimension" />
        <!--進(jìn)度條的寬度-->
        <attr name="circleWidth" format="dimension" />
        <!--進(jìn)度條距離外部周圍的距離-->
        <attr name="circleMargin" format="dimension" />
        <!--進(jìn)度條的背景顏色-->
        <attr name="circleBgColor" format="color|reference" />
        <!--進(jìn)度條的循環(huán)顏色-->
        <attr name="circleColor" format="color|reference" />
        <!--進(jìn)度條走完一圈需要時(shí)間-->
        <attr name="circleShowTime" format="integer" />
        <!--進(jìn)度條終止展示角度-->
        <attr name="circleEndAngle" format="float" />
        <!--進(jìn)度條循環(huán)起始角度-->
        <attr name="circleStartAngle" format="float" />
        <!--引導(dǎo)頁提示內(nèi)容-->
        <attr name="hint" format="string" />
        <!--引導(dǎo)提示字顏色-->
        <attr name="hintColor" format="color|reference" />
        <!--引導(dǎo)提示字大小-->
        <attr name="hintSize" format="dimension" />
        <!--引導(dǎo)提示字是否展示-->
        <attr name="hintIsShow" format="boolean" />
        <!--分割線-->
        <!--分割線高度-->
        <attr name="lineHeight" format="dimension" />
        <!--分割線的顏色-->
        <attr name="lineColor" format="color|reference" />
        <!--分割線上下的間距-->
        <attr name="lineMargin" format="dimension" />
        <!--底部內(nèi)容-->
        <!--未選中icon-->
        <attr name="bottomUnSelectIcon" format="reference" />
        <!--選中的的icon-->
        <attr name="bottomSelectIcon" format="reference" />
        <!--內(nèi)容icon的寬度-->
        <attr name="bottomIconWidth" format="dimension" />
        <!--內(nèi)容icon的高度-->
        <attr name="bottomIconHeight" format="dimension" />
        <!--默認(rèn)內(nèi)容展示顏色-->
        <attr name="defaultColor" format="color" />
        <!--選中內(nèi)容展示顏色-->
        <attr name="chooseColor" format="color" />
        <!--內(nèi)容標(biāo)題字體顏色-->
        <attr name="titleColor" format="color" />
        <!--展示標(biāo)題集合-->
        <attr name="titles" format="reference" />
        <!--默認(rèn)展示內(nèi)容集合-->
        <attr name="contentUnSelect" format="reference" />
        <!--選中展示內(nèi)容集合-->
        <attr name="contentSelect" format="reference" />
        <!--標(biāo)題字體大小-->
        <attr name="titleSize" format="dimension" />
        <!--內(nèi)容字體大小-->
        <attr name="contentSize" format="dimension" />
        <!--連接線寬度-->
       <attr name="connectLineWidth" format="dimension"/>
    </declare-styleable>

2.在view中繪制對(duì)應(yīng)view

  • 繪制進(jìn)度條
 private void drawProgress(Canvas canvas) {
        if (isShowIcon) {
            //創(chuàng)建指定大小的bitmap對(duì)象
            Bitmap bitmap = BitmapFactory.decodeResource(getResources(), circleBitmap);
            canvas.drawBitmap(bitmap, null, new RectF(width / 2 - circleBitmapWidth / 2,
                    (int) (circlePadding + circleMargin), (int) width / 2 + circleBitmapWidth / 2,
                    (int) (circlePadding + circleMargin) + circleBitmapHeight), bitmapPaint);
            bitmap.recycle();
            bitmap = null;
        }
        //創(chuàng)建指定大小的circle元
        canvas.drawCircle(width / 2, circlePadding + circleBitmapHeight / 2 + circleMargin, circleBitmapHeight / 2 + circlePadding, circleBgPaint);
        canvas.drawArc(width / 2 - circleBitmapWidth / 2 - circlePadding,
                circleMargin,
                width / 2 + circleBitmapWidth / 2 + circlePadding
                , circleBitmapHeight + circlePadding + circlePadding + circleMargin,
                circleStartAngle,
                circleEndAngle, false, circlePaint);
    }

    private int hintHeight = 0;

    private void drawHint(Canvas canvas) {
        String hints = TextUtils.isEmpty(hint) ? "正在努力評(píng)估中讽坏,請(qǐng)耐心等待..." : hint;
        float textWidth = hintPaint.measureText(hints);
        // 文本baseline在y軸方向的位置
        float baseLineY = Math.abs(hintPaint.ascent() + hintPaint.descent()) / 2;
        if (hintIsShow) {
            canvas.drawText(hints, width / 2 - textWidth / 2, (circlePadding + circleMargin) * 2 + circleBitmapHeight + baseLineY, hintPaint);
            hintHeight = (int) (hintPaint.density - hintPaint.ascent());
        } else {
            hintHeight = 0;
        }
    }
  • 繪制下方文字
   private void drawBottom(Canvas cavas) {
        //剩下的距離锭魔,根據(jù)titles的標(biāo)致均分
        remainingHeight = (int) (height - (circleMargin + circlePadding + lineMargin) * 2 - circleBitmapHeight - hintHeight - lineHeight);
        int y = (int) ((circleMargin + circlePadding + lineMargin) * 2 + circleBitmapHeight + hintHeight + lineHeight);
        int everyItem = remainingHeight / titles.size();
        //配置當(dāng)前的一些常見屬性

        //去左上角的點(diǎn)
        Point iconPoint = new Point(dp2px(mContext, 40), y + everyItem * 3 / 11);
        Point titlePoint = new Point(iconPoint.x+dp2px(mContext, 40), iconPoint.y);
        Point contentPoint = new Point(iconPoint.x+dp2px(mContext, 41), iconPoint.y + dp2px(mContext, 30));
        bottomBitmap(cavas, iconPoint, step);
        titleCenter(titles, titlePaint, cavas, titlePoint, Paint.Align.CENTER, step);
        contentCenter(contentSelect, contentUnSelect, contentPaint, cavas, contentPoint, Paint.Align.CENTER, step);
        ;
    }

    private void bottomBitmap(Canvas canvas, Point point, int step) {
        Bitmap bitmapUnSelect = BitmapFactory.decodeResource(getResources(), bottomUnSelectIcon);
        Bitmap bitmapSelect = BitmapFactory.decodeResource(getResources(), bottomSelectIcon);

        for (int i = 0; i < titles.size(); i++) {
            float yAxis = remainingHeight / titles.size();
            if (i <= step) {
                canvas.drawBitmap(bitmapSelect, null, new Rect(point.x,
                                point.y + (int) yAxis * i,
                                point.x + (int) bottomIconWidth,
                                point.y + (int) bottomIconHeight + (int) yAxis * i)
                        , bitmapPaint);
            } else {
                canvas.drawBitmap(bitmapUnSelect, null, new RectF(point.x,
                                point.y + (int) yAxis * i,
                                point.x + bottomIconWidth,
                                point.y + bottomIconHeight + (int) yAxis * i)
                        , bitmapPaint);
            }

        }
        for (int i = 1; i < titles.size(); i++) {
            if (i <= step) {
                connectLinePaint.setColor(chooseColor);
            } else {
                connectLinePaint.setColor(defaultColor);
            }
            float yAxis = remainingHeight / titles.size();
            canvas.drawLine(point.x + bottomIconWidth / 2, bottomIconHeight + point.y + yAxis * (i - 1)
                    , point.x + bottomIconWidth / 2, point.y + yAxis * i, connectLinePaint);
        }
        bitmapSelect.recycle();
        bitmapSelect = null;
        bitmapUnSelect.recycle();
        bitmapUnSelect = null;

    }

    //繪制標(biāo)題
    private void titleCenter(List<String> strings, Paint paint, Canvas canvas, Point point, Paint.Align aligin, int step) {
        Paint.FontMetrics fontMetrics = paint.getFontMetrics();
        float ascent = fontMetrics.ascent;
        float descent = fontMetrics.descent;
        float stringHeight = descent - ascent;
        int length = strings.size();
        for (int i = 0; i < length; i++) {
            if (i <= step) {
                titlePaint.setColor(chooseColor);
            } else {
                titlePaint.setColor(defaultColor);
            }
            float yAxis = remainingHeight / length;
            canvas.drawText(strings.get(i) + "", point.x, point.y + yAxis * i + stringHeight, paint);
        }
    }

    //繪制內(nèi)容
    private void contentCenter(List<String> select, List<String> unselect, Paint paint, Canvas canvas, Point point, Paint.Align aligin, int step) {
        Paint.FontMetrics fontMetrics = paint.getFontMetrics();
        float ascent = fontMetrics.ascent;
        float descent = fontMetrics.descent;
        float stringHeight = descent - ascent;
        for (int i = 0; i < titles.size(); i++) {
            float yAxis = remainingHeight / titles.size();
            if (i <= step) {
                contentPaint.setColor(chooseColor);
                canvas.drawText(select.get(i) + "", point.x, point.y + yAxis * i + stringHeight, paint);
            } else {
                contentPaint.setColor(defaultColor);
                canvas.drawText(unselect.get(i) + "", point.x, point.y + yAxis * i + stringHeight, paint);
            }
        }
    }
  • 設(shè)置回調(diào)方法
    public interface onViewCompleteFinshListener {
        void complete();
    }

    public void setCompleteFinshListener(onViewCompleteFinshListener completeFinshListener) {
        this.completeFinshListener = completeFinshListener;
    }

public class MainActivity extends AppCompatActivity implements SmartLoadingView.onViewCompleteFinshListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        SmartLoadingView loadingView = findViewById(R.id.smartView);
        loadingView.setCompleteFinshListener(this);
    }

    @Override
    public void complete() {
        Toast.makeText(this, "進(jìn)度已經(jīng)加載完畢,該執(zhí)行對(duì)應(yīng)的跳轉(zhuǎn)方法了", Toast.LENGTH_LONG).show();
        startActivity(new Intent(this, NextActivity.class));
        this.finish();
    }


}

3.在xml文件中調(diào)用

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <combudou.smartloadingview.view.SmartLoadingView
        android:id="@+id/smartView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:contentSelect="@array/verify_content"
        app:contentUnSelect="@array/default_content"
        app:titles="@array/titles" />


</RelativeLayout>
實(shí)現(xiàn)思路

功能很簡單路呜,都是一些基本的canvas方法調(diào)用迷捧,實(shí)現(xiàn)這個(gè)算是對(duì)基礎(chǔ)知識(shí)的一個(gè)回顧吧。

點(diǎn)擊查看源碼

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末胀葱,一起剝皮案震驚了整個(gè)濱河市漠秋,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌抵屿,老刑警劉巖庆锦,帶你破解...
    沈念sama閱讀 217,657評(píng)論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異轧葛,居然都是意外死亡搂抒,警方通過查閱死者的電腦和手機(jī)艇搀,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,889評(píng)論 3 394
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來求晶,“玉大人焰雕,你說我怎么就攤上這事》夹樱” “怎么了矩屁?”我有些...
    開封第一講書人閱讀 164,057評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長蚜锨。 經(jīng)常有香客問我档插,道長,這世上最難降的妖魔是什么亚再? 我笑而不...
    開封第一講書人閱讀 58,509評(píng)論 1 293
  • 正文 為了忘掉前任郭膛,我火速辦了婚禮,結(jié)果婚禮上氛悬,老公的妹妹穿的比我還像新娘则剃。我一直安慰自己,他們只是感情好如捅,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,562評(píng)論 6 392
  • 文/花漫 我一把揭開白布棍现。 她就那樣靜靜地躺著,像睡著了一般镜遣。 火紅的嫁衣襯著肌膚如雪己肮。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,443評(píng)論 1 302
  • 那天悲关,我揣著相機(jī)與錄音谎僻,去河邊找鬼。 笑死寓辱,一個(gè)胖子當(dāng)著我的面吹牛艘绍,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播秫筏,決...
    沈念sama閱讀 40,251評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼诱鞠,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了这敬?” 一聲冷哼從身側(cè)響起航夺,我...
    開封第一講書人閱讀 39,129評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎崔涂,沒想到半個(gè)月后阳掐,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,561評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,779評(píng)論 3 335
  • 正文 我和宋清朗相戀三年锚烦,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了觅闽。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,902評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡涮俄,死狀恐怖蛉拙,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情彻亲,我是刑警寧澤孕锄,帶...
    沈念sama閱讀 35,621評(píng)論 5 345
  • 正文 年R本政府宣布,位于F島的核電站苞尝,受9級(jí)特大地震影響畸肆,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜宙址,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,220評(píng)論 3 328
  • 文/蒙蒙 一轴脐、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧抡砂,春花似錦大咱、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,838評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至丑搔,卻和暖如春厦瓢,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背啤月。 一陣腳步聲響...
    開封第一講書人閱讀 32,971評(píng)論 1 269
  • 我被黑心中介騙來泰國打工煮仇, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人顽冶。 一個(gè)月前我還...
    沈念sama閱讀 48,025評(píng)論 2 370
  • 正文 我出身青樓欺抗,卻偏偏與公主長得像售碳,于是被迫代替她去往敵國和親强重。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,843評(píng)論 2 354

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

  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫贸人、插件间景、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 12,103評(píng)論 4 62
  • Swift1> Swift和OC的區(qū)別1.1> Swift沒有地址/指針的概念1.2> 泛型1.3> 類型嚴(yán)謹(jǐn) 對(duì)...
    cosWriter閱讀 11,101評(píng)論 1 32
  • 值得開心的事情
    潼樂閱讀 189評(píng)論 0 0
  • 還以為長大有多少奇遇倘要,還以為成長有多精彩,庸碌無為,平淡無奇封拧,目前的生活狀態(tài)志鹃,我小時(shí)候一直盼望著的年齡增長,絢爛花...
    冰燃閱讀 450評(píng)論 0 3
  • 看不清的喜怒哀樂 品不透的酸甜苦辣 這是你給我的感覺 也許一開始就是一個(gè)錯(cuò)誤 我卻動(dòng)了心 想讓脫軌的列車回到正軌 ...
    清霜溯月閱讀 274評(píng)論 0 0