您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關(guān)如何在Android中利用itemdecoration實(shí)現(xiàn)一個(gè)時(shí)間線效果,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
代碼如下:
// 時(shí)間線裝飾器 public class TimeLineDecoration extends RecyclerView.ItemDecoration { private Paint mPaint; public TimeLineDecoration() { mPaint = new Paint(); mPaint.setStyle(Paint.Style.FILL); mPaint.setColor(Color.BLUE); mPaint.setStrokeWidth(5); } @Override public void onDraw(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) { super.onDraw(c, parent, state); RecyclerView.LayoutManager layoutManager = parent.getLayoutManager(); // 這里的childcount為可見(jiàn)item的個(gè)數(shù)。 與item的個(gè)數(shù)不一定相同。 int childCount = parent.getChildCount(); for (int i = 0; i < childCount; i++) { View child = parent.getChildAt(i); // 避免硬編碼,這里通過(guò)代碼獲取在getItemOffsets方法中設(shè)置的寬度 int leftDecoWidth = layoutManager.getLeftDecorationWidth(child); // 根據(jù)position獲取當(dāng)前的數(shù)據(jù),然后根據(jù)數(shù)據(jù)狀態(tài)繪制不同的形狀 int position = parent.getChildAdapterPosition(child); int cx = leftDecoWidth / 2; int cy = child.getTop() + child.getHeight() / 2; int radius = 20; if (position == 2) { c.drawRect(cx - radius, cy - radius, cx + radius, cy + radius, mPaint); } else if (position == 4) { // 繪制外圈為空心圓,內(nèi)圈為實(shí)心圓 mPaint.setStyle(Paint.Style.STROKE); c.drawCircle(cx, cy, radius, mPaint); mPaint.setStyle(Paint.Style.FILL); c.drawCircle(cx, cy, radius >> 1, mPaint); } else { c.drawCircle(cx, cy, radius, mPaint); } // 繪制item中間的連接線,第一個(gè)item與最后一個(gè)item的連接線需單獨(dú)處理一下。 if (position == 0) { c.drawLine(cx, cy + mPaint.getStrokeWidth() + radius, cx, child.getBottom(), mPaint); } else if (position == parent.getAdapter().getItemCount() - 1) { c.drawLine(cx, child.getTop(), cx, cy - mPaint.getStrokeWidth() - radius, mPaint); } else { c.drawLine(cx, cy + mPaint.getStrokeWidth() + radius, cx, child.getBottom(), mPaint); c.drawLine(cx, child.getTop(), cx, cy - mPaint.getStrokeWidth() - radius, mPaint); } } } @Override public void onDrawOver(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) { super.onDrawOver(c, parent, state); // 不受outRect設(shè)置的范圍影響,可以繪制在item上。 } @Override public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) { super.getItemOffsets(outRect, view, parent, state); // 在item左邊留下100像素的空間。 item的布局會(huì)在減掉這100像素后處理。 outRect.left = 100; } }
然后將該itemdecoration設(shè)置到recyclerview上。
RecyclerAdapter adapter = new RecyclerAdapter(this, data); mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); mRecyclerView.addItemDecoration(new TimeLineDecoration()); mRecyclerView.setAdapter(adapter);
看完上述內(nèi)容,你們對(duì)如何在Android中利用itemdecoration實(shí)現(xiàn)一個(gè)時(shí)間線效果有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。