带有背景和透明文本的 TextView

Rei*_*erd 6 android android-layout

我正在尝试执行以下操作:

在此处输入图片说明

现在,我正在尝试使用 NativeAndroid。

但是,我可以使用HTML 和 css来做到这一点

如您所见,它类似于:

  • 蓝色(可能是任何其他颜色甚至图像)背景的持有人
  • 带有(透明??)颜色的 TextView
  • 带灰色背景的 TextView

但是,如果我将背景设置为灰色并将文本设置为透明,则文本“消失”并且全部显示为灰色。

这是我的尝试(原生 Android):

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ff000000" >
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#ffcecece"
    android:padding="5dp"
    android:text="@string/hello_world"
    android:textColor="#00ffffff" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

但是,它显示以下内容:

在此处输入图片说明

有文字,因为它是透明的,所以没有显示。

所以我的问题是:

  • 我怎样才能做到这一点?
  • 帆布?跨度?我不习惯这些方法,所以一些方向将不胜感激。

谢谢!!

编辑: 那个蓝色背景必须是动态的,它可能是一个图像!

小智 3

你可以使用 Canvas 来实现:

Bitmap bmOverlay = Bitmap.createBitmap(1200, 1200, Bitmap.Config.ARGB_8888);//bMap.getConfig());

Canvas canvas = new Canvas(bmOverlay);
canvas.drawColor(Color.LTGRAY);
Paint paint = new Paint();

Paint mPaintText = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaintText.setStrokeWidth(3);
mPaintText.setStyle(Paint.Style.FILL_AND_STROKE);
mPaintText.setColor(Color.TRANSPARENT);
mPaintText.setTextSize(50f);
mPaintText.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC));    
canvas.drawText("Your TExt", x, y, mPaintText);
Run Code Online (Sandbox Code Playgroud)