android imageview 重写,android – 使用ImageView自定义drawable

我已经有一个带有以下参数的’

ImageView’:

android:layout_width="wrap_content"

android:layout_height="wrap_content"

并设置自定义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(Y.x, Y.y);

hexagonPath.lineTo(A.x, A.y);

hexagonPath.lineTo(B.x, B.y);

hexagonPath.lineTo(C.x, C.y);

hexagonPath.lineTo(D.x, D.y);

hexagonPath.lineTo(E.x, E.y);

hexagonPath.lineTo(F.x, F.y);

hexagonPath.lineTo(X.x, X.y);

}

@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); // set the color

paint.setStrokeWidth(mStrokeWidth); // set the size

paint.setDither(true); // set the dither to true

paint.setStyle(Paint.Style.STROKE); // set to STOKE

paint.setStrokeJoin(Paint.Join.ROUND); // set the join to round you want

paint.setStrokeCap(Paint.Cap.ROUND); // set the paint cap to round too

paint.setPathEffect(new CornerPathEffect(mStrokeWidth)); // set the path effect when they join.

paint.setAntiAlias(true);

canvas.drawPath(hexagonPath, paint);

canvas.clipPath(hexagonPath, Region.Op.INTERSECT);

canvas.drawColor(mBackgroundColor);

canvas.drawPath(hexagonPath, paint);

canvas.save();

}

@Override

public void setAlpha(int alpha) {

}

@Override

public void setColorFilter(ColorFilter colorFilter) {

}

@Override

public int getOpacity() {

return 0;

}

}

在这种情况下,ImageView似乎使用了所有宽度.

如何正确实现Drawable与ImageView一起使用?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值