画布绘制矩形android

时间:2014-03-02 15:58:55

标签: android android-canvas autolayout

     public static Bitmap getCircularBitmapWithWhiteBorder(Bitmap bitmap,
        int borderWidth) {
    if (bitmap == null || bitmap.isRecycled()) {
        return null;
    }
    final int width = bitmap.getWidth() + borderWidth;
    final int height = bitmap.getHeight() + borderWidth;
    Bitmap canvasBitmap = Bitmap.createBitmap(width*2, height, Bitmap.Config.ARGB_8888);
    BitmapShader shader = new BitmapShader(bitmap, TileMode.CLAMP, TileMode.CLAMP);
    Paint paint = new Paint();
    paint.setAntiAlias(true);
    paint.setShader(shader);
    Canvas canvas = new Canvas(canvasBitmap);
    float radius = width > height ? ((float) height) / 2f : ((float) width) / 2f;
    canvas.drawCircle(width / 2, height / 2, radius, paint);
    paint.setShader(null);
    paint.setStyle(Paint.Style.STROKE);
    paint.setColor(Color.WHITE);
    paint.setStrokeWidth(borderWidth);
    canvas.drawCircle(width / 2, height / 2, radius - borderWidth / 2, paint);      
    return canvasBitmap;
}

我有圆形图像,我想在圆形图像旁边附加一个矩形...类似于他的 enter image description here 这是一个圆圈,旁边有一个矩形。我怎么能这样做?

1 个答案:

答案 0 :(得分:0)

您可以或多或少地绘制您的矩形:

  canvas.drawRect(width/2 + radius, height/2 - radius, width , height, paint);

只需调整参数,以便在需要时正确定位