Android透明文字

Mar*_*say 10 transparency android textview

我需要TextView在渐变背景上显示.在TextView本身应该有一个纯白色的背景,文字应该是透明的.

但是,为文本设置透明色(#00000000)不起作用:它只显示白色矩形,背景不会显示文本的位置(文本与TextView背景颜色相同).

如何在我的背景颜色上显示透明文字TextView

Gil*_*ach 15

更新,2016年1月30日

我在这个答案中创建了一个小型并编写了博客文章,因此您无需复制和粘贴代码,我会为您进行维护.:)

使用xml中的视图:

<it.gilvegliach.android.transparenttexttextview.TransparentTextTextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/view_bg"
    android:text="Hello World" />
Run Code Online (Sandbox Code Playgroud)

Gradle依赖:

 compile 'it.gilvegliach.android:transparent-text-textview:1.0.3'
Run Code Online (Sandbox Code Playgroud)

原始答案

这是你如何实现这种效果:

  1. 您在位图上的透明背景上呈现文本
  2. 您使用该位图剪切纯白色背景中的文本形状

这是一个简单的子类TextView.

final public class SeeThroughTextView extends TextView
{
    Bitmap mMaskBitmap;
    Canvas mMaskCanvas;
    Paint mPaint;

    Drawable mBackground;
    Bitmap mBackgroundBitmap;
    Canvas mBackgroundCanvas;
    boolean mSetBoundsOnSizeAvailable = false;

    public SeeThroughTextView(Context context)
    {
        super(context);

        mPaint = new Paint();
        mPaint.setXfermode(new PorterDuffXfermode(Mode.DST_OUT));
        super.setTextColor(Color.BLACK);
        super.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    }

    @Override
    @Deprecated
    public void setBackgroundDrawable(Drawable bg)
    {
        mBackground = bg;
        int w = bg.getIntrinsicWidth();
        int h = bg.getIntrinsicHeight();

        // Drawable has no dimensions, retrieve View's dimensions
        if (w == -1 || h == -1)
        {
            w = getWidth();
            h = getHeight();
        }

        // Layout has not run
        if (w == 0 || h == 0)
        {
            mSetBoundsOnSizeAvailable = true;
            return;
        }

        mBackground.setBounds(0, 0, w, h);
        invalidate();
    }

    @Override
    public void setBackgroundColor(int color)
    {
        setBackgroundDrawable(new ColorDrawable(color));
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh)
    {
        super.onSizeChanged(w, h, oldw, oldh);
        mBackgroundBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        mBackgroundCanvas = new Canvas(mBackgroundBitmap);
        mMaskBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        mMaskCanvas = new Canvas(mMaskBitmap);

        if (mSetBoundsOnSizeAvailable)
        {
            mBackground.setBounds(0, 0, w, h);
            mSetBoundsOnSizeAvailable = false;
        }
    }

    @Override
    protected void onDraw(Canvas canvas)
    {
        // Draw background
        mBackground.draw(mBackgroundCanvas);

        // Draw mask
        mMaskCanvas.drawColor(Color.BLACK, PorterDuff.Mode.CLEAR);
        super.onDraw(mMaskCanvas);

        mBackgroundCanvas.drawBitmap(mMaskBitmap, 0.f, 0.f, mPaint);
        canvas.drawBitmap(mBackgroundBitmap, 0.f, 0.f, null);
    }
}
Run Code Online (Sandbox Code Playgroud)

示例屏幕截图:活动背景的靛蓝图案,TextView背景的粉红色实心填充.

这适用于纯色背景和普通绘图.无论如何,这只是一个BASIC实现,不支持某些功能,如平铺.