美文网首页Android技术知识
RecyclerView ItemDecoration时间轴、流

RecyclerView ItemDecoration时间轴、流

作者: 咚咚_Coding | 来源:发表于2021-03-03 12:50 被阅读0次

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 退款流程节点状态View
*/
public class OrderRefundProcessDecoration extends RecyclerView.ItemDecoration {

// 写右边字的画笔(具体信息)
private Paint mPaint;
// 左 上偏移长度
private int itemView_leftinterval;
private int itemView_topinterval;

// 图标
private final Bitmap mSuccessIcon;
private final Bitmap mProcessCompleteIcon;
private Bitmap mIcon;
private final int mIconWidth;
private final int IMG_W_H = ScreenUtils.dp2px( 17);//


// 在构造函数里进行绘制的初始化,如画笔属性设置等
public OrderRefundProcessDecoration(Context context) {
    // 轴点画笔(红色)
    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);

    // 获取图标资源
    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()方法
// 作用:设置ItemView 左 & 上偏移长度
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
    super.getItemOffsets(outRect, view, parent, state);
    // 设置ItemView的左 & 上偏移长度分别为200 px & 50px,即此为onDraw()可绘制的区域
    outRect.set(itemView_leftinterval, view == parent.getChildAt(0) ? itemView_topinterval / 2 : itemView_topinterval, 0, 0);

}

// 重写onDraw()
// 作用:在间隔区域里绘制时光轴线 & 时间文本
@Override
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
    super.onDraw(c, parent, state);
    // 获取RecyclerView的Child view的个数
    int childCount = parent.getChildCount();
    // 遍历每个Item,分别获取它们的位置信息,然后再绘制对应的分割线
    for (int i = 0; i < childCount; i++) {
        // 获取每个Item对象
        ConstraintLayout constraintLayout = (ConstraintLayout) parent.getChildAt(i);
        boolean isPass = TextUtils.equals("1", (String) constraintLayout.getTag(R.id.order_detail_refund_process));
        processIcon(isPass);
        /**
         * 绘制轴点
         */
        // 轴点 = 圆 = 圆心(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;

        // 下端点坐标(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);
        }

        /**
         * 绘制下半轴线
         */

        // 上端点坐标(x,y)
        float bottomLine_up_x = centerx;
        float bottom_up_y = centery;

        // 下端点坐标(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方法进行缩放
     */
    matrix.postScale(scaleWidht, scaleHeight);
    Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
    return newbmp;

}
}

相关文章

网友评论

    本文标题:RecyclerView ItemDecoration时间轴、流

    本文链接:https://www.haomeiwen.com/subject/jrpxqltx.html