Skip to content

Instantly share code, notes, and snippets.

@parallelcross
Forked from briangriffey/CircularDrawable.java
Last active January 12, 2023 04:52
Show Gist options
  • Select an option

  • Save parallelcross/7073058 to your computer and use it in GitHub Desktop.

Select an option

Save parallelcross/7073058 to your computer and use it in GitHub Desktop.
package com.example.pathshaderdrawable;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
public class CircularDrawable extends PathDrawable {
private Paint mPaint;
public CircularDrawable(Bitmap bitmap) {
super(bitmap);
}
@Override
public void draw(Canvas canvas) {
Rect bounds = getBounds();
Path path = new Path();
path.addCircle(bounds.centerX(), bounds.centerY(), (float)bounds.width() / 2f, Path.Direction.CW);
path.close();
setPath(path);
super.draw(canvas);
}
}
package com.example.pathshaderdrawable;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.drawable.Drawable;
public class PathDrawable extends Drawable {
private final Bitmap mBitmap;
private Bitmap mTransformedBitmap;
private Path mPath;
private Paint mPaint;
public PathDrawable(Bitmap bitmap) {
mBitmap = bitmap;
mPaint = new Paint();
mPaint.setStyle(Paint.Style.FILL);
mPaint.setColor(Color.BLACK);
}
@Override
protected void onBoundsChange(Rect bounds) {
super.onBoundsChange(bounds);
if(mTransformedBitmap != null)
mTransformedBitmap.recycle();
mTransformedBitmap = Bitmap.createScaledBitmap(mBitmap, bounds.width(), bounds.height(), false);
BitmapShader shader = new BitmapShader(mTransformedBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
mPaint.setShader(shader);
mPaint.setAntiAlias(true);
}
@Override
public void draw(Canvas canvas) {
Rect bounds = getBounds();
//get bounds of path to scale to bounds of view if different
RectF pathBounds = new RectF();
mPath.computeBounds(pathBounds, true);
//scale path to view
Matrix pathToBoundsMatrix = new Matrix();
pathToBoundsMatrix.postScale((float)bounds.width()/pathBounds.width(), (float)bounds.height()/pathBounds.height());
pathToBoundsMatrix.postTranslate(bounds.left, bounds.top);
canvas.concat(pathToBoundsMatrix);
canvas.drawPath(mPath, mPaint);
}
@Override
public void setAlpha(int alpha) {
mPaint.setAlpha(alpha);
}
@Override
public void setColorFilter(ColorFilter cf) {
}
@Override
public int getOpacity() {
return 0;
}
protected void setPath(Path path) {
mPath = path;
}
}
package com.example.pathshaderdrawable;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
public class RectDrawable extends PathDrawable {
private Paint mPaint;
public RectDrawable(Bitmap bitmap) {
super(bitmap);
}
@Override
public void draw(Canvas canvas) {
Rect bounds = getBounds();
Path path = new Path();
path.addRect(bounds.left, bounds.top, bounds.right, bounds.bottom, Path.Direction.CW);
path.close();
setPath(path);
super.draw(canvas);
}
}
package com.example.pathshaderdrawable;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.RectF;
public class RoundedRectDrawable extends PathDrawable {
private Paint mPaint;
private static final float DEFAULT_X_RADIUS = 20f;
private static final float DEFAULT_Y_RADIUS = 20f;
private float mXRadius;
private float mYRadius;
public RoundedRectDrawable(Bitmap bitmap) {
super(bitmap);
mXRadius = DEFAULT_X_RADIUS;
mYRadius = DEFAULT_Y_RADIUS;
}
public RoundedRectDrawable(Bitmap bitmap, float xRadius, float yRadius) {
super(bitmap);
mXRadius = xRadius;
mYRadius = yRadius;
}
@Override
public void draw(Canvas canvas) {
Rect bounds = getBounds();
Path path = new Path();
RectF rect = new RectF(bounds.left, bounds.top, bounds.right, bounds.bottom);
path.addRoundRect(rect, mXRadius, mYRadius, Path.Direction.CW);
path.close();
setPath(path);
super.draw(canvas);
}
public void setXRadius(float xRadius) {
mXRadius = xRadius;
}
public void setYRadius(float yRadius) {
mYRadius = yRadius;
}
}
package com.example.pathshaderdrawable;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
public class StarDrawable extends PathDrawable {
private Paint mPaint;
public StarDrawable(Bitmap bitmap) {
super(bitmap);
}
@Override
public void draw(Canvas canvas) {
Rect bounds = getBounds();
final float rectSegmentsForStar = 7f;
float startPointX = bounds.left + bounds.width() / rectSegmentsForStar;
float startPointY = bounds.bottom;
float secondPointX = bounds.right;
float secondPointY = bounds.top + (float)bounds.height() / rectSegmentsForStar * 2f;
float thirdPointX = bounds.left;
float thirdPointY = bounds.top + bounds.height() / rectSegmentsForStar * 2f;
float fourthPointX = bounds.right - bounds.width() / rectSegmentsForStar;
float fourthPointY = bounds.bottom;
float fifthPointX = (bounds.left + bounds.right) / 2f;
float fifthPointY = bounds.top;
Path path = new Path();
path.moveTo(startPointX, startPointY);
path.lineTo(secondPointX, secondPointY);
path.lineTo(thirdPointX, thirdPointY);
path.lineTo(fourthPointX, fourthPointY);
path.lineTo(fifthPointX, fifthPointY);
path.lineTo(startPointX, startPointY);
path.close();
setPath(path);
super.draw(canvas);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment