描画可能にパスを描画し、DrawableをView/ImageViewの背景またはsrcとして設定する際に問題があります。起こっているように見えるのは、矢印を描く側のパスが常に直線よりも少し太いということです。固定ディメンションのビューでテストしています。誰でも私はそれを修正する方法についてのアイデアがありますか?Androidカスタム描画可能なペイントストロークの異なる幅
は、ここに私の描画可能なコードです。
public class ArrowDrawable extends Drawable {
public static final String TAG = ArrowDrawable.class.getSimpleName();
private Paint outlinePaint;
private Paint fillPaint;
int padding = 40;
int arrowPosition = 50;
int arrowHeight = 60;
int strokeWidth = 10;
Path path = new Path();
public enum Direction {
RIGHT,
LEFT;
}
Direction direction = Direction.RIGHT;
public ArrowDrawable() {
init();
}
private void init() {
outlinePaint = new Paint();
outlinePaint.setStyle(Paint.Style.STROKE); // set to STOKE
outlinePaint.setStrokeJoin(Paint.Join.BEVEL); // set the join to round you want
outlinePaint.setStrokeCap(Paint.Cap.ROUND); // set the outlinePaint cap to round too
outlinePaint.setPathEffect(new CornerPathEffect(2)); // set the path effect when they join.
outlinePaint.setAntiAlias(true);
outlinePaint.setStrokeWidth(strokeWidth);
fillPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
fillPaint.setStrokeWidth(strokeWidth);
fillPaint.setStrokeJoin(Paint.Join.ROUND);
fillPaint.setPathEffect(new CornerPathEffect(2));
fillPaint.setStyle(Paint.Style.FILL);
fillPaint.setAntiAlias(true);
fillPaint.setColor(Color.WHITE);
}
public void setDirection(Direction direction) {
this.direction = direction;
invalidateSelf();
}
public void setStrokeColor(int color) {
outlinePaint.setColor(color);
invalidateSelf();
}
public void setFillColor(int color) {
fillPaint.setColor(color);
invalidateSelf();
}
public void setPadding(int padding) {
this.padding = padding;
invalidateSelf();
}
public void setArrowPosition(int arrowPosition) {
this.arrowPosition = arrowPosition;
invalidateSelf();
}
public void setArrowHeight(int arrowHeight) {
this.arrowHeight = arrowHeight;
invalidateSelf();
}
public void setStrokeWidth(int strokeWidth) {
this.strokeWidth = strokeWidth;
invalidateSelf();
}
@Override
public void draw(Canvas canvas) {
final Rect bounds = getBounds();
Log.d(TAG, "draw: " + canvas.getWidth() + " " + canvas.getHeight());
Path path;
if (direction == Direction.RIGHT) {
path = pointRight(bounds);
} else if (direction == Direction.LEFT) {
path = pointLeft(bounds);
} else {
throw new IllegalArgumentException("Direction is not supported");
}
path.computeBounds(new RectF(bounds), false);
canvas.drawPath(path, outlinePaint);
}
@Override
public void invalidateSelf() {
path = null;
super.invalidateSelf();
}
@Override
protected void onBoundsChange(Rect bounds) {
super.onBoundsChange(bounds);
invalidateSelf();
}
public Path pointRight(Rect bounds) {
if (path != null) {
return path;
}
final Rect newRect = new Rect(bounds.left, bounds.top, bounds.right - padding - strokeWidth, bounds.bottom);
path = new Path();
path.moveTo(newRect.left, newRect.top);
path.lineTo(newRect.right, newRect.top);
path.lineTo(newRect.right, newRect.top + arrowPosition);
path.lineTo(bounds.right - strokeWidth, newRect.top + arrowPosition + arrowHeight/2.0f);
path.lineTo(newRect.right, newRect.top + arrowPosition + arrowHeight);
path.lineTo(newRect.right, newRect.bottom);
path.lineTo(newRect.left, newRect.bottom);
path.close();
return path;
}
public Path pointLeft(Rect bounds) {
if (path != null) {
return path;
}
final Rect newRect = new Rect(bounds.left + padding + strokeWidth, bounds.top, bounds.right, bounds.bottom);
path = new Path();
path.moveTo(newRect.left, newRect.top);
path.lineTo(newRect.left, newRect.top + arrowPosition);
path.lineTo(bounds.left + strokeWidth, newRect.top + arrowPosition + arrowHeight/2.0f);
path.lineTo(newRect.left, newRect.top + arrowPosition + arrowHeight);
path.lineTo(newRect.left, newRect.bottom);
path.lineTo(newRect.right, newRect.bottom);
path.lineTo(newRect.right, newRect.top);
path.close();
return path;
}
@Override
public void setAlpha(int alpha) {
}
@Override
public void setColorFilter(ColorFilter colorFilter) {
}
@Override
public int getOpacity() {
return PixelFormat.UNKNOWN;
}
}
(最初申し訳ストローク幅/ 2 – pskink
によってbounddオフセットが、私はあなたが示唆されているものを得ることはありません申し訳ありません – DArkO
:はめ込み、bounds.inset(strokeWidth/2)を呼び出す – pskink