Created
September 8, 2017 11:11
-
-
Save abbas-oveissi/27df6d7f74f97dff26ab54a16f345ee0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class VerticalTimelineDecoration extends RecyclerView.ItemDecoration { | |
Paint paintFill = null; | |
Paint paintStrokeFill = null; | |
Paint paintStroke = null; | |
Paint paintDotLine = null; | |
Paint paintText=null; | |
Paint paintTextBold=null; | |
int phase=0; | |
public VerticalTimelineDecoration() { | |
paintFill = new Paint(); | |
paintFill.setStyle(Paint.Style.FILL); | |
paintFill.setColor(Color.BLACK); | |
paintStroke = new Paint(Paint.ANTI_ALIAS_FLAG); | |
paintStroke.setStyle(Paint.Style.STROKE); | |
paintStroke.setColor(Color.BLACK); | |
paintText = new Paint(); | |
paintText.setARGB(200, 254, 0, 0); | |
paintText.setTextAlign(Paint.Align.CENTER); | |
paintText.setTextSize(Utility.dpToPx(15)); | |
paintTextBold = new Paint(); | |
paintTextBold.setARGB(200, 0,149,255); | |
paintTextBold.setTextAlign(Paint.Align.CENTER); | |
paintTextBold.setTextSize(Utility.dpToPx(20)); | |
paintDotLine = new Paint(); | |
paintDotLine.setARGB(255, 0, 0,0); | |
paintDotLine.setStyle(Paint.Style.STROKE); | |
paintDotLine.setStrokeWidth(Utility.dpToPx(2)); | |
paintDotLine.setPathEffect(new DashPathEffect(new float[] {10,10}, phase)); | |
} | |
@Override | |
public void onDraw(Canvas canvas, RecyclerView parent, RecyclerView.State state) { | |
canvas.save(); | |
final int leftWithMargin = Utility.dpToPx(80); | |
final int right = parent.getWidth(); | |
final int childCount = parent.getChildCount(); | |
for (int i = 0; i < childCount; i++) { | |
final View child = parent.getChildAt(i); | |
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child | |
.getLayoutParams(); | |
int adapterPosition = parent.getChildAdapterPosition(child); | |
MovieAdapter.ViewHolder viewHolder = (MovieAdapter.ViewHolder) parent.getChildViewHolder(child); | |
// draw divider | |
final int top = child.getBottom() + params.bottomMargin; | |
final int bottom = top + Utility.dpToPx(2); | |
canvas.drawRect(new RectF(leftWithMargin,top,right,bottom), paintFill); | |
// draw circle | |
int pCenterX=Utility.dpToPx(40); | |
int pCenterY=child.getTop()+(child.getBottom()-child.getTop())/2; | |
int radius=Utility.dpToPx(20); | |
paintStroke.setStrokeWidth(Utility.dpToPx(2)); | |
canvas.drawCircle(pCenterX,pCenterY,radius,paintStroke); | |
// draw line (top of circle) | |
Path mPath = new Path(); | |
mPath.moveTo(pCenterX, child.getTop()); | |
mPath.lineTo(pCenterX,pCenterY-radius); | |
paintDotLine.setPathEffect(new DashPathEffect(new float[] {10,10}, 0)); | |
if(adapterPosition!=0) | |
canvas.drawPath(mPath,paintDotLine); | |
// draw line (bottom of circle) | |
mPath = new Path(); | |
mPath.moveTo(pCenterX, pCenterY+radius); | |
mPath.lineTo(pCenterX, child.getBottom()); | |
paintDotLine.setPathEffect(new DashPathEffect(new float[] {10,10}, 10)); | |
if(adapterPosition!=9) | |
canvas.drawPath(mPath,paintDotLine); | |
//draw text | |
int xPos = pCenterX; | |
int yPos = (int) ((pCenterY) - ((paintText.descent() + paintText.ascent()) / 2)) ; | |
if(viewHolder.item.isChecked==false) | |
canvas.drawText(String.valueOf(adapterPosition), xPos, yPos, paintText); | |
else | |
canvas.drawText(String.valueOf(adapterPosition), xPos, yPos, paintTextBold); | |
} | |
canvas.restore(); } | |
@Override | |
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) { | |
super.getItemOffsets(outRect, view, parent, state); | |
outRect.left = Utility.dpToPx(80); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
nice :)