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;
}
}