RecyclerView ItemDecoration時(shí)間軸肛炮、流程節(jié)點(diǎn)View

Log pos

 D/rihehriiherihihre: i......0.....top..50........bottom......323
 D/rihehriiherihihre: i......1.....top..373........bottom......493
 D/rihehriiherihihre: i......2.....top..543........bottom......663

Code ItemDecoration

/**
* Created by niudong 退款流程節(jié)點(diǎn)狀態(tài)View
*/
public class OrderRefundProcessDecoration extends RecyclerView.ItemDecoration {

// 寫右邊字的畫筆(具體信息)
private Paint mPaint;
// 左 上偏移長度
private int itemView_leftinterval;
private int itemView_topinterval;

// 圖標(biāo)
private final Bitmap mSuccessIcon;
private final Bitmap mProcessCompleteIcon;
private Bitmap mIcon;
private final int mIconWidth;
private final int IMG_W_H = ScreenUtils.dp2px( 17);//


// 在構(gòu)造函數(shù)里進(jìn)行繪制的初始化族购,如畫筆屬性設(shè)置等
public OrderRefundProcessDecoration(Context context) {
    // 軸點(diǎn)畫筆(紅色)
    mPaint = new Paint();
    mPaint.setStrokeWidth(ScreenUtils.dp2px(1.2f));
    mPaint.setAntiAlias(true);
    mPaint.setColor(Color.parseColor("#1FDCC6"));

    // 賦值ItemView的左偏移長度為200
    itemView_leftinterval = ScreenUtils.dp2px(15f);

    // 賦值ItemView的上偏移長度為50
    itemView_topinterval = ScreenUtils.dp2px(19f);

    // 獲取圖標(biāo)資源
    mSuccessIcon = scale(BitmapFactory.decodeResource(context.getResources(), R.drawable.icon_refund_process_success),IMG_W_H,IMG_W_H );
    mProcessCompleteIcon = scale(BitmapFactory.decodeResource(context.getResources(), R.drawable.icon_curr_refund_complete),IMG_W_H,IMG_W_H);

    mIcon = mProcessCompleteIcon;
    mIconWidth = mIcon.getWidth() / 2;
}

// 重寫getItemOffsets()方法
// 作用:設(shè)置ItemView 左 & 上偏移長度
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
    super.getItemOffsets(outRect, view, parent, state);
    // 設(shè)置ItemView的左 & 上偏移長度分別為200 px & 50px,即此為onDraw()可繪制的區(qū)域
    outRect.set(itemView_leftinterval, view == parent.getChildAt(0) ? itemView_topinterval / 2 : itemView_topinterval, 0, 0);

}

// 重寫onDraw()
// 作用:在間隔區(qū)域里繪制時(shí)光軸線 & 時(shí)間文本
@Override
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
    super.onDraw(c, parent, state);
    // 獲取RecyclerView的Child view的個(gè)數(shù)
    int childCount = parent.getChildCount();
    // 遍歷每個(gè)Item韩脑,分別獲取它們的位置信息谆奥,然后再繪制對應(yīng)的分割線
    for (int i = 0; i < childCount; i++) {
        // 獲取每個(gè)Item對象
        ConstraintLayout constraintLayout = (ConstraintLayout) parent.getChildAt(i);
        boolean isPass = TextUtils.equals("1", (String) constraintLayout.getTag(R.id.order_detail_refund_process));
        processIcon(isPass);
        /**
         * 繪制軸點(diǎn)
         */
        // 軸點(diǎn) = 圓 = 圓心(x,y)
        float centerx = mIconWidth;

        View oneChild = constraintLayout.getChildAt(0);
        int[] location = new int[2];
        oneChild.getLocationInWindow(location);
        float centery = constraintLayout.getTop() + oneChild.getHeight() / 2;

        /**
         * 繪制上半軸線
         */
        float upLine_up_x = centerx;
        float upLine_up_y = 0f;

        // 下端點(diǎn)坐標(biāo)(x,y)
        float upLine_bottom_x = centerx;
        float upLine_bottom_y = centery;
        if (i > 0) {
            //繪制上半部軸線

            processPaintColor(isPass);

            ConstraintLayout childPre = (ConstraintLayout) parent.getChildAt(i - 1);
            upLine_up_y = constraintLayout.getTop() - childPre.getHeight() / 2;
            c.drawLine(upLine_up_x, upLine_up_y, upLine_bottom_x, upLine_bottom_y, mPaint);
        }

        /**
         * 繪制下半軸線
         */

        // 上端點(diǎn)坐標(biāo)(x,y)
        float bottomLine_up_x = centerx;
        float bottom_up_y = centery;

        // 下端點(diǎn)坐標(biāo)(x,y)
        float bottomLine_bottom_x = centerx;
        float bottomLine_bottom_y = constraintLayout.getBottom();

        //繪制下半部軸線
        if (i != childCount - 1) {
            processPaintColor(isPass);
            c.drawLine(bottomLine_up_x, bottom_up_y, bottomLine_bottom_x, bottomLine_bottom_y, mPaint);
        }
        c.drawBitmap(mIcon, centerx - mIconWidth, centery - mIconWidth, mPaint);
    }
}

private void processIcon(boolean isPass) {
    if (isPass) {
        if (mIcon != mProcessCompleteIcon) {
            mIcon = mProcessCompleteIcon;
        }
    } else {
        if (mIcon != mSuccessIcon) {
            mIcon = mSuccessIcon;
        }
    }
}

private void processPaintColor(boolean isPass) {
    if (isPass) {
        if (mPaint.getColor() == Color.parseColor("#DEDEDE")) {
            mPaint.setColor(Color.parseColor("#1FDCC6"));
        }
    } else {
        if (mPaint.getColor() == Color.parseColor("#1FDCC6")) {
            mPaint.setColor(Color.parseColor("#DEDEDE"));
        }
    }
}

/**
 * 縮放圖片
 *
 * @return 縮放后的圖片
 */
public static Bitmap scale(final Bitmap bitmap,
                           final float w,
                           final float h) {

    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    Matrix matrix = new Matrix();
    float scaleWidht = ((float) w / width);
    float scaleHeight = ((float) h / height);
    /*
     * 通過Matrix類的postScale方法進(jìn)行縮放
     */
    matrix.postScale(scaleWidht, scaleHeight);
    Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
    return newbmp;

}
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末绰咽,一起剝皮案震驚了整個(gè)濱河市菇肃,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌取募,老刑警劉巖琐谤,帶你破解...
    沈念sama閱讀 217,826評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異玩敏,居然都是意外死亡斗忌,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,968評論 3 395
  • 文/潘曉璐 我一進(jìn)店門聊品,熙熙樓的掌柜王于貴愁眉苦臉地迎上來飞蹂,“玉大人,你說我怎么就攤上這事翻屈〕卵疲” “怎么了?”我有些...
    開封第一講書人閱讀 164,234評論 0 354
  • 文/不壞的土叔 我叫張陵伸眶,是天一觀的道長惊窖。 經(jīng)常有香客問我,道長厘贼,這世上最難降的妖魔是什么界酒? 我笑而不...
    開封第一講書人閱讀 58,562評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮嘴秸,結(jié)果婚禮上毁欣,老公的妹妹穿的比我還像新娘庇谆。我一直安慰自己,他們只是感情好凭疮,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,611評論 6 392
  • 文/花漫 我一把揭開白布饭耳。 她就那樣靜靜地躺著,像睡著了一般执解。 火紅的嫁衣襯著肌膚如雪寞肖。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,482評論 1 302
  • 那天衰腌,我揣著相機(jī)與錄音新蟆,去河邊找鬼。 笑死右蕊,一個(gè)胖子當(dāng)著我的面吹牛琼稻,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播饶囚,決...
    沈念sama閱讀 40,271評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼欣簇,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了坯约?” 一聲冷哼從身側(cè)響起熊咽,我...
    開封第一講書人閱讀 39,166評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎闹丐,沒想到半個(gè)月后横殴,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,608評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡卿拴,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,814評論 3 336
  • 正文 我和宋清朗相戀三年衫仑,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片堕花。...
    茶點(diǎn)故事閱讀 39,926評論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡文狱,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出缘挽,到底是詐尸還是另有隱情瞄崇,我是刑警寧澤,帶...
    沈念sama閱讀 35,644評論 5 346
  • 正文 年R本政府宣布壕曼,位于F島的核電站苏研,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏腮郊。R本人自食惡果不足惜摹蘑,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,249評論 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望轧飞。 院中可真熱鬧衅鹿,春花似錦撒踪、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,866評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至兼犯,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間集漾,已是汗流浹背切黔。 一陣腳步聲響...
    開封第一講書人閱讀 32,991評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留具篇,地道東北人纬霞。 一個(gè)月前我還...
    沈念sama閱讀 48,063評論 3 370
  • 正文 我出身青樓,卻偏偏與公主長得像驱显,于是被迫代替她去往敵國和親诗芜。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,871評論 2 354

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