如何在图像上绘制封装边框?

时间:2018-06-11 17:48:15

标签: java android

我已覆盖图像绘制方法以在AppCompatImageView上绘制边框。

对于某些图像可以正常工作,但对于某些图像,图像上方和下方有一些空白区域,如下图所示。我想在图像尺寸上精确绘制边框。

enter image description here

以下是代码

private static final int PADDING = 8;
    private static final float STROKE_WIDTH = 8.0f;

    private Paint mBorderPaint;

   private void initBorderPaint() {
        mBorderPaint = new Paint();
        mBorderPaint.setAntiAlias(true);
        mBorderPaint.setStyle(Paint.Style.STROKE);
        mBorderPaint.setColor(Color.WHITE);
        mBorderPaint.setStrokeWidth(STROKE_WIDTH);
    }

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.drawRect(PADDING, PADDING, getWidth() - PADDING, getHeight() - PADDING, mBorderPaint);
}

0 个答案:

没有答案