android - 如何在android中的Canvas文本上添加透明背景?

标签 android android-layout canvas android-bitmap

我想在报价单上添加透明的canvas。确切地说,我们可以在下图中看到($74)。我试图关注 SO 问题,但对我没有任何帮助。这是我的代码:

enter image description here

@Override
public Bitmap transform(Bitmap bitmap) {
    // TODO Auto-generated method stub
    synchronized (ImageTransform.class) {
        if (bitmap == null) {
            return null;
        }
        Bitmap resultBitmap = bitmap.copy(bitmap.getConfig(), true);
        Bitmap bitmapImage = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_heart);
        bitmapImage = Bitmap.createScaledBitmap(bitmapImage, 50, 50, true);

        Canvas canvas = new Canvas(resultBitmap);
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setColor(Color.WHITE);
        paint.setStyle(Paint.Style.FILL);
        paint.setTextSize(40);
        paint.setShadowLayer(2.0f, 1.0f, 1.0f, Color.BLACK);
            canvas.drawText("$250", 20, 400, paint);
            canvas.drawBitmap(bitmapImage, 510, 55, null);
        bitmap.recycle();
        return resultBitmap;
    }

如何在价格部分添加透明颜色或图像?

编辑-1

但是我可以添加 Canvas 矩形但不是在正确的位置

 Paint paintrect = new Paint();
        paintrect.setColor(Color.BLACK);
        paintrect.setStyle(Paint.Style.FILL_AND_STROKE);
        paintrect.setStrokeWidth(10);

        float left = 20;
        float top = 80;
        float right = 50;
        float bottom = 0;

        canvas.drawRect(left, top, right, bottom, paintrect);

我明白了: 您可以在图像的左上角看到黑色矩形。 enter image description here

最佳答案

你快到了!

Paint paintrect = new Paint();
    paintrect.setColor(Color.BLACK);
    paintrect.setStyle(Paint.Style.FILL_AND_STROKE);
    paintrect.setStrokeWidth(10);
    paintrect.setAlpha(127);

    float left = 18;
    float top = 360;
    float right = 128;
    float bottom = 416;

    canvas.drawRect(left, top, right, bottom, paintrect);

您将不得不调整矩形坐标以使其位于您想要的位置;我尽可能估计了。

关于android - 如何在android中的Canvas文本上添加透明背景?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32684857/

相关文章:

java - 为什么我无法在 Windows 上运行 APK 文件?

java - 服务在应用程序关闭时停止,而以前没有

android - TextEdit 在键盘上消失

javascript - 调整和缩放 HTML5 Canvas 和内容

android - Coordinator布局和权重

android - 我的布局适用于一种屏幕尺寸,如何使其适用于 Android Studio 3.4.2 中的所有屏幕尺寸

android - 为什么layout和layout-normal-mdpi的存在不会产生编译时错误?

java - Gridview在 ScrollView 中根据实际高度显示

javascript - 在 HTML5 canvas 上绘图的最佳实践、技巧和陷阱是什么

JS 文件中的 Javascript 函数不会从 Canvas 中调用