I already have an "ImageView" with these options:
android:layout_width="wrap_content" android:layout_height="wrap_content"
and install custom Drawable :
public class HexDrawable extends Drawable { private Path hexagonPath; private float mWidth, mHeight; private int mBackgroundColor; private int mStrokeColor; private int mStrokeWidth; public HexDrawable(){ init(); } public void setBackgroundColor(int color) { mBackgroundColor = color; } public void setStrokeWidth(int width) { mStrokeWidth = width; } public void setStrokeColor(int color) { mStrokeColor = color; } @Override public int getIntrinsicHeight() { return 60; } @Override public int getIntrinsicWidth() { return 60; } private void init() { hexagonPath = new Path(); mBackgroundColor = Color.BLUE; mStrokeColor = Color.GREEN; mStrokeWidth = 4; } private void calculatePath() { float p = mStrokeWidth / 2; float w = mWidth - 2 * p; float h = mHeight - 2 * p; float r = h / 2; float a = (float) (r / Math.sqrt(3)); PointF X = new PointF(p + a + r / 2, p); PointF Y = new PointF(p + a + r , p); PointF A = new PointF(p + a, p + 0f); PointF B = new PointF(p + 0f, p + r); PointF C = new PointF(p + a, p + 2 * r); PointF D = new PointF(p + w - a, p + 2 * r); PointF E = new PointF(p + w, p + r); PointF F = new PointF(p + w - a, p + 0); hexagonPath.moveTo(Yx, Yy); hexagonPath.lineTo(Ax, Ay); hexagonPath.lineTo(Bx, By); hexagonPath.lineTo(Cx, Cy); hexagonPath.lineTo(Dx, Dy); hexagonPath.lineTo(Ex, Ey); hexagonPath.lineTo(Fx, Fy); hexagonPath.lineTo(Xx, Xy); } @Override protected void onBoundsChange(Rect bounds) { mWidth = bounds.width(); mHeight = bounds.height(); calculatePath(); } @Override public void draw(Canvas canvas) { Paint paint = new Paint(); paint.setColor(mStrokeColor);
ImageView seems to use full width in this case.
How to use Drawable to use it with ImageView ?
source share