Skip to content

Instantly share code, notes, and snippets.

@dineshmm23
Created August 23, 2019 12:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dineshmm23/3e15f98e69e6afcc42c332e755ddbb8b to your computer and use it in GitHub Desktop.
Save dineshmm23/3e15f98e69e6afcc42c332e755ddbb8b to your computer and use it in GitHub Desktop.
Dashed and Dotted Line XML class
<resources>
<declare-styleable name="DottedLine">
<attr name="minimumDotGap" format="dimension"/>
<attr name="dotRadius" format="dimension"/>
<attr name="dotColor" format="color"/>
<attr name="orientation" format="enum">
<enum name="horizontal" value="0"/>
<enum name="vertical" value="1"/>
</attr>
</declare-styleable>
<declare-styleable name="DashedLine">
<attr name="minimumDashGap" format="dimension"/>
<attr name="dashHeight" format="dimension"/>
<attr name="dashLength" format="dimension"/>
<attr name="dashColor" format="color"/>
<attr name="orientation"/>
</declare-styleable>
</resources>
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.View;
import androidx.annotation.Nullable;
import org.jetbrains.annotations.NotNull;
public class DashedLine extends View {
private float dashHeight;
private float dashLength;
private float minimumDashGap;
private Path path;
private Paint paint;
private Orientation orientation;
public DashedLine(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.orientation = Orientation.HORIZONTAL;
this.path = new Path();
this.paint = new Paint();
DisplayMetrics metrics = getResources().getDisplayMetrics();
float twoDpDefault = TypedValue.applyDimension(1, 2.0F, metrics);
int defaultBlack = Color.argb(255, 0, 0, 0);
if (attrs != null) {
TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.DashedLine, defStyleAttr, 0);
this.dashHeight = typedArray != null ? typedArray.getDimension(R.styleable.DashedLine_dashHeight, twoDpDefault) : twoDpDefault;
this.dashLength = typedArray != null ? typedArray.getDimension(R.styleable.DashedLine_dashLength, twoDpDefault) : twoDpDefault;
this.minimumDashGap = typedArray != null ? typedArray.getDimension(R.styleable.DashedLine_minimumDashGap, twoDpDefault) : twoDpDefault;
this.paint.setColor(typedArray != null ? typedArray.getColor(R.styleable.DashedLine_dashColor, defaultBlack) : defaultBlack);
int orientationOrdinal = typedArray != null ? typedArray.getInt(R.styleable.DashedLine_orientation, DottedLine.Orientation.HORIZONTAL.ordinal()) : DottedLine.Orientation.HORIZONTAL.ordinal();
if (orientationOrdinal == DottedLine.Orientation.VERTICAL.ordinal()) {
this.orientation = Orientation.VERTICAL;
} else {
this.orientation = Orientation.HORIZONTAL;
}
if (typedArray != null) {
typedArray.recycle();
}
} else {
this.dashHeight = twoDpDefault;
this.dashLength = twoDpDefault;
this.minimumDashGap = twoDpDefault;
this.paint.setColor(defaultBlack);
}
this.paint.setStrokeWidth(dashHeight);
this.paint.setStyle(Paint.Style.STROKE);
}
public final float getDashHeight() {
return this.dashHeight;
}
public final void setDashHeight(float dashHeight) {
this.dashHeight = dashHeight;
}
public final float getDashLength() {
return this.dashLength;
}
public final void setDashLength(float dashLength) {
this.dashLength = dashLength;
}
public final float getMinimumDashGap() {
return this.minimumDashGap;
}
public final void setMinimumDashGap(float minimumDashGap) {
this.minimumDashGap = minimumDashGap;
}
@NotNull
public final Orientation getOrientation() {
return this.orientation;
}
public final void setOrientation(@NotNull Orientation orientation) {
this.orientation = orientation;
}
@NotNull
public final Path getPath() {
return this.path;
}
@NotNull
public final Paint getPaint() {
return this.paint;
}
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int width;
int height;
if (this.orientation == Orientation.HORIZONTAL) {
int widthNeeded = this.getPaddingLeft() + this.getPaddingRight() + this.getSuggestedMinimumWidth();
width = View.resolveSize(widthNeeded, widthMeasureSpec);
float heightNeeded = (float) (this.getPaddingTop() + this.getPaddingBottom()) + this.dashHeight;
height = View.resolveSize((int) heightNeeded, heightMeasureSpec);
this.setMeasuredDimension(width, height);
} else {
float widthNeeded = (float) (this.getPaddingLeft() + this.getPaddingRight()) + this.dashHeight;
width = View.resolveSize((int) widthNeeded, widthMeasureSpec);
int heightNeeded = this.getPaddingTop() + this.getPaddingBottom() + this.getSuggestedMinimumHeight();
height = View.resolveSize(heightNeeded, heightMeasureSpec);
this.setMeasuredDimension(width, height);
}
}
protected void onDraw(@NotNull Canvas canvas) {
path.reset();
path.moveTo((float) this.getPaddingLeft(), (float) this.getPaddingTop());
float d;
float m;
int c;
float g;
if (this.orientation == Orientation.HORIZONTAL) {
int w = getWidth() - this.getPaddingLeft() - this.getPaddingRight();
d = this.dashLength;
m = this.minimumDashGap;
c = (int) Math.floor((double) (((float) w - d) / (d + m)));
g = ((float) w - d * (float) (c + 1)) / (float) c;
this.path.lineTo((float) (getWidth() - this.getPaddingLeft() - this.getPaddingRight()), (float) this.getPaddingTop());
this.paint.setPathEffect((new DashPathEffect(new float[]{d, g}, 0.0F)));
} else {
float h = (float) (getHeight() - this.getPaddingTop() - this.getPaddingBottom());
d = this.dashLength;
m = this.minimumDashGap;
c = (int) Math.floor((double) ((h - d) / (d + m)));
g = (h - d * (float) (c + 1)) / (float) c;
this.paint.setPathEffect((new DashPathEffect(new float[]{d, g}, 0.0F)));
this.path.lineTo((float) this.getPaddingLeft(), h);
}
canvas.drawPath(this.path, this.paint);
}
enum Orientation {
HORIZONTAL,
VERTICAL
}
}
import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.View;
import org.jetbrains.annotations.NotNull;
public class DottedLine extends View {
private float dotRadius;
private float minimumDotGap;
private DottedLine.Orientation orientation;
private Paint paint;
public DottedLine(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.orientation = DottedLine.Orientation.HORIZONTAL;
this.paint = new Paint();
Resources var10000 = this.getResources();
DisplayMetrics metrics = var10000.getDisplayMetrics();
float twoDpDefault = TypedValue.applyDimension(1, 2.0F, metrics);
int defaultBlack = Color.argb(255, 0, 0, 0);
if (attrs != null) {
TypedArray var10;
label44:
{
if (context != null) {
Resources.Theme var9 = context.getTheme();
if (var9 != null) {
var10 = var9.obtainStyledAttributes(attrs, R.styleable.DottedLine, defStyleAttr, 0);
break label44;
}
}
var10 = null;
}
TypedArray typedArray = var10;
this.dotRadius = typedArray != null ? typedArray.getDimension(R.styleable.DottedLine_dotRadius, twoDpDefault) : twoDpDefault;
this.minimumDotGap = typedArray != null ? typedArray.getDimension(R.styleable.DottedLine_minimumDotGap, twoDpDefault) : twoDpDefault;
this.paint.setColor(typedArray != null ? typedArray.getColor(R.styleable.DottedLine_dotColor, defaultBlack) : defaultBlack);
int orientationOrdinal = typedArray != null ? typedArray.getInt(R.styleable.DottedLine_orientation, DottedLine.Orientation.HORIZONTAL.ordinal()) : DottedLine.Orientation.HORIZONTAL.ordinal();
if (orientationOrdinal == DottedLine.Orientation.VERTICAL.ordinal()) {
this.orientation = DottedLine.Orientation.VERTICAL;
} else {
this.orientation = DottedLine.Orientation.HORIZONTAL;
}
if (typedArray != null) {
typedArray.recycle();
}
} else {
this.dotRadius = twoDpDefault;
this.minimumDotGap = twoDpDefault;
this.paint.setColor(defaultBlack);
}
this.paint.setStyle(Paint.Style.FILL);
this.paint.setFlags(1);
}
public final float getDotRadius() {
return this.dotRadius;
}
public final void setDotRadius(float var1) {
this.dotRadius = var1;
}
public final float getMinimumDotGap() {
return this.minimumDotGap;
}
public final void setMinimumDotGap(float var1) {
this.minimumDotGap = var1;
}
@NotNull
public final DottedLine.Orientation getOrientation() {
return this.orientation;
}
public final void setOrientation(@NotNull DottedLine.Orientation var1) {
this.orientation = var1;
}
@NotNull
public final Paint getPaint() {
return this.paint;
}
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int width;
int height;
if (this.orientation == DottedLine.Orientation.HORIZONTAL) {
int widthNeeded = this.getPaddingLeft() + this.getPaddingRight() + this.getSuggestedMinimumWidth();
width = View.resolveSize(widthNeeded, widthMeasureSpec);
float heightNeeded = (float) (this.getPaddingTop() + this.getPaddingBottom()) + (float) 2 * this.dotRadius;
height = View.resolveSize((int) heightNeeded, heightMeasureSpec);
this.setMeasuredDimension(width, height);
} else {
float widthNeeded = (float) (this.getPaddingLeft() + this.getPaddingRight()) + (float) 2 * this.dotRadius;
width = View.resolveSize((int) widthNeeded, widthMeasureSpec);
int heightNeeded = this.getPaddingTop() + this.getPaddingBottom() + this.getSuggestedMinimumHeight();
height = View.resolveSize(heightNeeded, heightMeasureSpec);
this.setMeasuredDimension(width, height);
}
}
protected void onDraw(@NotNull Canvas canvas) {
float d;
float m;
int c;
float g;
int i;
int var8;
if (this.orientation == DottedLine.Orientation.HORIZONTAL) {
int w = getWidth() - this.getPaddingLeft() - this.getPaddingRight();
d = (float) 2 * this.dotRadius;
m = this.minimumDotGap;
c = (int) Math.floor((double) (((float) w - d) / (d + m)));
g = ((float) w - d * (float) (c + 1)) / (float) c;
i = 0;
var8 = c;
if (i <= c) {
while (true) {
canvas.drawCircle((float) this.getPaddingLeft() + this.dotRadius + (float) i * (d + g), (float) this.getPaddingTop() + this.dotRadius, this.dotRadius, this.paint);
if (i == var8) {
break;
}
++i;
}
}
} else {
float h = (float) (getHeight() - this.getPaddingTop() - this.getPaddingBottom());
d = (float) 2 * this.dotRadius;
m = this.minimumDotGap;
c = (int) Math.floor((double) ((h - d) / (d + m)));
g = (h - d * (float) (c + 1)) / (float) c;
i = 0;
var8 = c;
if (i <= c) {
while (true) {
canvas.drawCircle((float) this.getPaddingLeft() + this.dotRadius, (float) this.getPaddingTop() + this.dotRadius + (float) i * (d + g), this.dotRadius, this.paint);
if (i == var8) {
break;
}
++i;
}
}
}
}
enum Orientation {
HORIZONTAL,
VERTICAL
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment