Last active
July 5, 2017 16:06
-
-
Save hector6872/5e9a729965100ef279bb28405a043108 to your computer and use it in GitHub Desktop.
DashedLineView
This file contains hidden or 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
| <?xml version="1.0" encoding="utf-8"?> | |
| <resources> | |
| <declare-styleable name="DashedLineView"> | |
| <attr format="color" | |
| name="dlw_color"/> | |
| <attr format="float" | |
| name="dlw_dash"/> | |
| <attr format="float" | |
| name="dlw_gap"/> | |
| <attr format="float" | |
| name="dlw_phase"/> | |
| </declare-styleable> | |
| </resources> |
This file contains hidden or 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 DashedLineView extends View { | |
| private static final int DEFAULT_COLOR = Color.BLACK; | |
| private static final float DEFAULT_DASH = 4f; | |
| private static final float DEFAULT_GAP = 2f; | |
| private static final float DEFAULT_PHASE = 0f; | |
| private final Paint paint = new Paint(); | |
| private final Path path = new Path(); | |
| private float dash; | |
| private float gap; | |
| private float phase; | |
| public DashedLineView(Context context) { | |
| this(context, null); | |
| } | |
| public DashedLineView(Context context, AttributeSet attrs) { | |
| this(context, attrs, 0); | |
| } | |
| public DashedLineView(Context context, AttributeSet attrs, int defStyle) { | |
| super(context, attrs, defStyle); | |
| init(attrs); | |
| } | |
| private void init(@Nullable AttributeSet attrs) { | |
| paint.setStyle(Paint.Style.STROKE); | |
| TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.DashedLineView); | |
| paint.setColor(typedArray.getColor(R.styleable.DashedLineView_dlw_color, DEFAULT_COLOR)); | |
| dash = typedArray.getFloat(R.styleable.DashedLineView_dlw_dash, DEFAULT_DASH); | |
| gap = typedArray.getFloat(R.styleable.DashedLineView_dlw_gap, DEFAULT_GAP); | |
| phase = typedArray.getFloat(R.styleable.DashedLineView_dlw_phase, DEFAULT_PHASE); | |
| typedArray.recycle(); | |
| paint.setPathEffect(new DashPathEffect(new float[] { dash, gap }, phase)); | |
| } | |
| @Override protected void onDraw(Canvas canvas) { | |
| super.onDraw(canvas); | |
| path.moveTo(0, 0); | |
| int measuredHeight = getMeasuredHeight(); | |
| int measuredWidth = getMeasuredWidth(); | |
| if (measuredHeight <= measuredWidth) { | |
| // horizontal | |
| path.lineTo(measuredWidth, 0); | |
| } else { | |
| // vertical | |
| path.lineTo(0, measuredHeight); | |
| } | |
| canvas.drawPath(path, paint); | |
| } | |
| public void setColor(@ColorInt int color) { | |
| paint.setColor(color); | |
| invalidate(); | |
| } | |
| public void setColorResource(@ColorRes int color) { | |
| paint.setColor(ContextCompat.getColor(getContext(), color)); | |
| invalidate(); | |
| } | |
| public void setDash(float dash) { | |
| this.dash = dash; | |
| updateDashPathEffect(); | |
| } | |
| public void setGap(float gap) { | |
| this.gap = gap; | |
| updateDashPathEffect(); | |
| } | |
| public void setPhase(float phase) { | |
| this.phase = phase; | |
| updateDashPathEffect(); | |
| } | |
| public void setDashedLine(float dash, float gap, float phase) { | |
| this.dash = dash; | |
| this.gap = gap; | |
| this.phase = phase; | |
| updateDashPathEffect(); | |
| } | |
| private void updateDashPathEffect() { | |
| paint.setPathEffect(new DashPathEffect(new float[] { dash, gap }, phase)); | |
| invalidate(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment