【问题标题】:How to add transparent background on Canvas text in android?如何在 android 中的 Canvas 文本上添加透明背景?
【发布时间】:2015-09-21 22:38:23
【问题描述】:

我想在报价单上添加透明的canvas。正是我们可以在下图中看到(74 美元)。我试图关注 SO question,但对我没有任何帮助。这是我的code

@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

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

 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);

我得到了这个: 您可以在图像的左上角看到黑色矩形。

【问题讨论】:

  • 我也面临同样的问题:(

标签: android android-layout canvas android-bitmap


【解决方案1】:

你快到了!

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);

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

【讨论】:

  • 我认为您误解了这个问题。 paint.setAlpha() 使 250 美元透明或不透明。我要求在其上添加一个背景矩形框(有点透明)。如上图所示
  • 请查看编辑部分
  • 我确实误解了你的问题。请查看我的更新答案。
猜你喜欢
  • 1970-01-01
  • 2013-03-13
  • 2016-12-02
  • 1970-01-01
  • 2017-02-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-02
  • 1970-01-01
相关资源
最近更新 更多