Skip to content

Instantly share code, notes, and snippets.

@slightfoot
Last active February 8, 2022 12:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save slightfoot/5519582 to your computer and use it in GitHub Desktop.
Save slightfoot/5519582 to your computer and use it in GitHub Desktop.
ParallelogramDrawable
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.Shape;
public class ParallelogramDrawable extends ShapeDrawable
{
private double mAngle;
public ParallelogramDrawable()
{
setShape(new ParallelogramShape());
}
public ParallelogramDrawable(int color)
{
this();
getPaint().setColor(color);
}
public void setAngle(double angle)
{
mAngle = angle;
onBoundsChange(getBounds()); // force resize
}
public double getAngle()
{
return mAngle;
}
public class ParallelogramShape extends Shape
{
private final Path mPath = new Path();
@Override
protected void onResize(float width, float height)
{
int offset = (int) (Math.tan(degreesToRadians(mAngle)) * height);
Path path = mPath;
path.reset();
if(offset < 0){
path.moveTo(0, 0);
path.lineTo(width + offset, 0);
path.lineTo(width, height);
path.lineTo(-offset, height);
}else{
path.moveTo(offset, 0);
path.lineTo(width, 0);
path.lineTo(width - offset, height);
path.lineTo(0, height);
}
path.close();
}
@Override
public void draw(Canvas canvas, Paint paint)
{
canvas.drawPath(mPath, paint);
}
}
private static double degreesToRadians(double degrees)
{
return (degrees / 360.0) * (2.0 * Math.PI);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment