Android 如何在位图内容周围获得边框?

Android 如何在位图内容周围获得边框?,android,drawing,border,android-canvas,Android,Drawing,Border,Android Canvas,我无法获取位图内容周围的边框,我如何才能获取内容边框,下面的代码只能给我一个图像周围的边框,而不是一个内容 mPaint.setStyle(Paint.Style.STROKE); mPaint.setStrokeWidth(10); // set stroke width mPaint.setColor(getResources().getColor(R.color.black)); // set stroke color mPaint.setAntiAlias(t

我无法获取位图内容周围的边框,我如何才能获取内容边框,下面的代码只能给我一个图像周围的边框,而不是一个内容

  mPaint.setStyle(Paint.Style.STROKE);
    mPaint.setStrokeWidth(10); // set stroke width
    mPaint.setColor(getResources().getColor(R.color.black)); // set stroke color
    mPaint.setAntiAlias(true);
    bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icons8_bus_36_2);
    tempBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas tempCanvas = new Canvas(tempBitmap);
    Rect rect = new Rect(
            10 / 2,
            10 / 2,
            tempCanvas.getWidth() - 10 / 2,
            tempCanvas.getHeight() - 10 / 2);
    tempCanvas.drawRect(rect,mPaint);
    tempCanvas.drawBitmap(bitmap, 0, 0, mPaint);
关于这一部分:

Rect rect = new Rect(
            10 / 2,
            10 / 2,
            tempCanvas.getWidth() - 10 / 2,
            tempCanvas.getHeight() - 10 / 2);
改为

Rect rect = new Rect(
            10 / 2,
            10 / 2,
            (tempCanvas.getWidth() - 10) / 2,
            (tempCanvas.getHeight() - 10) / 2);
这是因为10先被2除

另外,在绘制矩形之前先绘制位图

你可以用这个画:

Path clipPath = new Path();
RectF rect = new RectF(0, 0, this.getWidth(), this.getHeight());
clipPath.addRoundRect(rect, radius, radius, Path.Direction.CW);
canvas.clipPath(clipPath);

我已经试过那一步了,泰兄弟。泰兄弟,你救了我的命。没问题,如果我的回答解决了你的问题,你可以接受。快乐编码