23

我用下面的方法制作了一个圆角的位图。现在我想在位图周围画一条线。

private BitmapDrawable roundCornered(BitmapDrawable scaledBitmap, int i) {

        Bitmap bitmap = scaledBitmap.getBitmap();

        result = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(),
                Bitmap.Config.ARGB_8888);
        canvas = new Canvas(result);

        color = 0xff424242;
        paint = new Paint();
        rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
        rectF = new RectF(rect);
        roundPx = i;
        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(Color.BLUE);
        canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);
        BitmapDrawable finalresult = new BitmapDrawable(result);
        return finalresult;
    }

我得到了下面的图像,但我的实际需要是我必须在图像周围画一个边框。

4

6 回答 6

49

我为自己整理了以下内容。

public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int color, int cornerDips, int borderDips, Context context) {
    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(),
            Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    final int borderSizePx = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, (float) borderDips,
            context.getResources().getDisplayMetrics());
    final int cornerSizePx = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, (float) cornerDips,
            context.getResources().getDisplayMetrics());
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
    final RectF rectF = new RectF(rect);

    // prepare canvas for transfer
    paint.setAntiAlias(true);
    paint.setColor(0xFFFFFFFF);
    paint.setStyle(Paint.Style.FILL);
    canvas.drawARGB(0, 0, 0, 0);
    canvas.drawRoundRect(rectF, cornerSizePx, cornerSizePx, paint);

    // draw bitmap
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);

    // draw border
    paint.setColor(color);
    paint.setStyle(Paint.Style.STROKE);
    paint.setStrokeWidth((float) borderSizePx);
    canvas.drawRoundRect(rectF, cornerSizePx, cornerSizePx, paint);

    return output;
}

当然,感谢http://ruibm.com/?p=184

于 2012-09-22T12:32:46.790 回答
3

如何准备像下面这样的 9-patch 图像并将其设置为背景使用android:background

在此处输入图像描述

于 2012-06-13T10:14:09.023 回答
3

我使用BitmapShaderdrawRoundRect做它,它对我有用,看看截图

在此处输入图像描述

RectF roundRect; // the Rect you have to draw into
Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);

// draw the border at bottom
mPaint.setStyle(Paint.Style.FILL);
mPaint.setColor(mBorderColor);
canvas.drawRoundRect(roundRect, mRadius, mRadius, mPaint);

// ------------------ draw scheme bitmap
roundRect.set(itemRect.left + mBorderSize, itemRect.top + mBorderSize, itemRect.right - mBorderSize, itemRect.bottom - mBorderSize);
Shader shader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
mPaint.setShader(shader);
canvas.drawRoundRect(roundRect, mRadius, mRadius, mPaint);
mPaint.setShader(null);
于 2013-11-08T11:46:11.260 回答
1

我做了很多搜索以通过代码实现该效果,在我找到另一种方法之前,它还不够完美,你可以看到每个角落的锯齿,我设置了 Paint.setAntiAlias(true), Paint.setDither(true), Paint.setFilterBitmap (真的),但它不起作用,所以我希望有人能帮助我。 在此处输入图像描述

RectF roundRect = new RectF(itemRect);

Bitmap bitmap = scheme.getSchemeBitmap(getResources());

mPaint.setStyle(Paint.Style.FILL);
mPaint.setColor(mSchemeSelectedColor);
canvas.drawRoundRect(roundRect, mSchemeCornerRadius, mSchemeCornerRadius, mPaint);

roundRect.set(itemRect.left + mBorderSize, itemRect.top + mBorderSize, itemRect.right - mBorderSize, itemRect.bottom - mBorderSize);
Path clipPath = new Path();
clipPath.addRoundRect(roundRect, mSchemeCornerRadius, mSchemeCornerRadius, Path.Direction.CW);
canvas.save(Canvas.CLIP_SAVE_FLAG);
canvas.clipPath(clipPath);
canvas.drawBitmap(bitmap, null, roundRect, mPaint);
canvas.restore();
于 2013-11-08T12:08:22.207 回答
0

不幸的是,Android 中没有漂亮而简洁的“边框”参数,但最简单的方法是将其封装在另一个布局中,将父布局的背景设置为边框颜色/可绘制,然后在其上设置填充。填充将出现在您的 BitmapDrawable 周围。

于 2012-06-13T10:07:57.770 回答
0

我的方式:

 public static Bitmap getRoundedCornerBitmap1(Bitmap bitmap, int color, int cornerDips, int borderDips) {


            Bitmap output = Bitmap.createBitmap(bitmap.getWidth()+2*borderDips,
                    bitmap.getHeight()+2*borderDips,
                    Bitmap.Config.ARGB_8888);

            Canvas canvas = new Canvas(output);

            canvas.drawColor(Color.TRANSPARENT);

            final RectF rectF = new RectF(0, 0, output.getWidth(), output.getHeight());
            final Paint paint = new Paint();
            // prepare canvas for transfer
            paint.setAntiAlias(true);
            paint.setStrokeWidth((float) borderDips);
            paint.setColor(Color.WHITE);
            paint.setStyle(Paint.Style.FILL);

            canvas.drawRoundRect(rectF, borderDips, borderDips, paint);

            canvas.drawBitmap(bitmap, borderDips, borderDips, null);
            bitmap.recycle();
            return output;
        }

结果

在此处输入图像描述

于 2017-04-26T07:29:45.510 回答