如何使用画布在位图中绘制笔画?

发布于 2024-11-10 08:18:13 字数 1574 浏览 0 评论 0原文

我需要圆化位图的角。之后,我需要为其添加边框。我已经完成了下面的源代码来圆角,但我不知道如何使用 CanvasBitmap 中绘制边框。有什么办法可以做到吗?谢谢

Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(),
    Config.ARGB_8888);
Canvas canvas = new Canvas(output);
            
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
final RectF rectF = new RectF(rect);
    
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
canvas.drawRoundRect(rectF, 20.0f, 20.0f, paint);
    
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
    
return output;

我已经添加了解决方案:

Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), 
    Config.ARGB_8888);
Canvas canvas = new Canvas(output);
            
Paint paint = new Paint();
Paint paintStroke = new Paint();
            
paintStroke.setStrokeWidth(2);
paintStroke.setStyle(Paint.Style.STROKE);
paintStroke.setColor(Color.RED);
paintStroke.setAntiAlias(true);
            
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
final RectF rectF = new RectF(rect);
    
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
            
canvas.drawRoundRect(rectF, 20.0f, 20.0f, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
canvas.drawRoundRect(rectF, round, round, paintStroke);
    
return output;

I need to round the corners of a Bitmap. After that, I need to add a border for it. I have done the source below to round the corners but I don't know how to draw a border in the Bitmap using Canvas. Is there any way to do it? Thanks

Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(),
    Config.ARGB_8888);
Canvas canvas = new Canvas(output);
            
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
final RectF rectF = new RectF(rect);
    
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
canvas.drawRoundRect(rectF, 20.0f, 20.0f, paint);
    
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
    
return output;

I have added the solution:

Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), 
    Config.ARGB_8888);
Canvas canvas = new Canvas(output);
            
Paint paint = new Paint();
Paint paintStroke = new Paint();
            
paintStroke.setStrokeWidth(2);
paintStroke.setStyle(Paint.Style.STROKE);
paintStroke.setColor(Color.RED);
paintStroke.setAntiAlias(true);
            
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
final RectF rectF = new RectF(rect);
    
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
            
canvas.drawRoundRect(rectF, 20.0f, 20.0f, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
canvas.drawRoundRect(rectF, round, round, paintStroke);
    
return output;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

十二 2024-11-17 08:18:13

尝试这个方法并传递位图,它将返回带有角度边框的位图:

Bitmap rotateAndFrame(Bitmap bitmap,float angle) {
    // final boolean positive = sRandom.nextFloat() >= 0.5f;
    // final float angle = (ROTATION_ANGLE_MIN + sRandom.nextFloat() * 
    //     ROTATION_ANGLE_EXTRA) * (positive ? 1.0f : -1.0f);
    final double radAngle = Math.toRadians(angle);

    final int bitmapWidth = bitmap.getWidth();
    final int bitmapHeight = bitmap.getHeight();

    final double cosAngle = Math.abs(Math.cos(radAngle));
    final double sinAngle = Math.abs(Math.sin(radAngle));

    final int strokedWidth = (int) (bitmapWidth + 2 * PHOTO_BORDER_WIDTH);
    final int strokedHeight = (int) (bitmapHeight + 2 * PHOTO_BORDER_WIDTH);

    final int width = (int) (strokedHeight * sinAngle + strokedWidth * cosAngle);
    final int height = (int) (strokedWidth * sinAngle + strokedHeight * cosAngle);

    final float x = (width - bitmapWidth) / 2.0f;
    final float y = (height - bitmapHeight) / 2.0f;

    final Bitmap decored = Bitmap.createBitmap(width, height, 
        Bitmap.Config.ARGB_8888);
       
    final Canvas canvas = new Canvas(decored);

    canvas.rotate(angle, width / 2.0f, height / 2.0f);
    canvas.drawBitmap(bitmap, x, y, sPaint);
    canvas.drawRect(x, y, x + bitmapWidth, y + bitmapHeight, sStrokePaint);
    canvas.drawText(String.valueOf(k_value),(one_piecewidth*0.84f),
        (one_pieceheight*0.35f),sNumPaint);
    k++;
    return decored;
}

希望它有帮助...

编辑:

private static final float PHOTO_BORDER_WIDTH2 = 3.0f;
private static final float PHOTO_BORDER_WIDTH = 1.0f;
private static final int PHOTO_BORDER_COLOR2=0x00000000;
private static final int PHOTO_BORDER_COLOR = 0xffffffff;

private static final Paint sPaint = 
    new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
private static final Paint sStrokePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
private static final Paint sNumPaint = new Paint(Paint.LINEAR_TEXT_FLAG);
static {
    sNumPaint.setColor(Color.BLACK);
    sNumPaint.setTextSize(25);
    sNumPaint.setTextAlign(Align.RIGHT);
}
static {
    sStrokePaint.setStrokeWidth(PHOTO_BORDER_WIDTH);
    sStrokePaint.setStyle(Paint.Style.STROKE);
    // sStrokePaint.measureText("hello");
    sStrokePaint.setColor(PHOTO_BORDER_COLOR);
}

try this Method and pass bitmap it will return bitmap with border with angle :

Bitmap rotateAndFrame(Bitmap bitmap,float angle) {
    // final boolean positive = sRandom.nextFloat() >= 0.5f;
    // final float angle = (ROTATION_ANGLE_MIN + sRandom.nextFloat() * 
    //     ROTATION_ANGLE_EXTRA) * (positive ? 1.0f : -1.0f);
    final double radAngle = Math.toRadians(angle);

    final int bitmapWidth = bitmap.getWidth();
    final int bitmapHeight = bitmap.getHeight();

    final double cosAngle = Math.abs(Math.cos(radAngle));
    final double sinAngle = Math.abs(Math.sin(radAngle));

    final int strokedWidth = (int) (bitmapWidth + 2 * PHOTO_BORDER_WIDTH);
    final int strokedHeight = (int) (bitmapHeight + 2 * PHOTO_BORDER_WIDTH);

    final int width = (int) (strokedHeight * sinAngle + strokedWidth * cosAngle);
    final int height = (int) (strokedWidth * sinAngle + strokedHeight * cosAngle);

    final float x = (width - bitmapWidth) / 2.0f;
    final float y = (height - bitmapHeight) / 2.0f;

    final Bitmap decored = Bitmap.createBitmap(width, height, 
        Bitmap.Config.ARGB_8888);
       
    final Canvas canvas = new Canvas(decored);

    canvas.rotate(angle, width / 2.0f, height / 2.0f);
    canvas.drawBitmap(bitmap, x, y, sPaint);
    canvas.drawRect(x, y, x + bitmapWidth, y + bitmapHeight, sStrokePaint);
    canvas.drawText(String.valueOf(k_value),(one_piecewidth*0.84f),
        (one_pieceheight*0.35f),sNumPaint);
    k++;
    return decored;
}

hope it helps...

edited:

private static final float PHOTO_BORDER_WIDTH2 = 3.0f;
private static final float PHOTO_BORDER_WIDTH = 1.0f;
private static final int PHOTO_BORDER_COLOR2=0x00000000;
private static final int PHOTO_BORDER_COLOR = 0xffffffff;

private static final Paint sPaint = 
    new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
private static final Paint sStrokePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
private static final Paint sNumPaint = new Paint(Paint.LINEAR_TEXT_FLAG);
static {
    sNumPaint.setColor(Color.BLACK);
    sNumPaint.setTextSize(25);
    sNumPaint.setTextAlign(Align.RIGHT);
}
static {
    sStrokePaint.setStrokeWidth(PHOTO_BORDER_WIDTH);
    sStrokePaint.setStyle(Paint.Style.STROKE);
    // sStrokePaint.measureText("hello");
    sStrokePaint.setColor(PHOTO_BORDER_COLOR);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文