Last active
February 8, 2022 12:34
-
-
Save slightfoot/5519582 to your computer and use it in GitHub Desktop.
ParallelogramDrawable
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
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