/*
 * Copyright (C) 2016 Gil Vegliach
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package it.gilvegliach.android.transparenttexttextview;

import android.annotation.SuppressLint;

import android.content.Context;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;

import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;

import android.util.AttributeSet;

import android.widget.TextView;

public class TransparentTextTextView extends TextView {
    private Bitmap mMaskBitmap;
    private Canvas mMaskCanvas;
    private Paint mPaint;
    private Drawable mBackground;
    private Bitmap mBackgroundBitmap;
    private Canvas mBackgroundCanvas;
    private boolean mSetBoundsOnSizeAvailable;

    public TransparentTextTextView(final Context context) {
        super(context);
        init();
    }

    public TransparentTextTextView(final Context context, final AttributeSet attrs) {
        super(context, attrs);
        init();
    }

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

    @Override
    @Deprecated
    public void setBackgroundDrawable(final Drawable bg) {
        mBackground = bg;

        int w = getWidth();
        int 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(final int color) {
        setBackgroundDrawable(new ColorDrawable(color));
    }

    @Override
    protected void onSizeChanged(final int w, final int h, final int oldw, final 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(final Canvas canvas) {
        drawMask();
        drawBackground();
        canvas.drawBitmap(mBackgroundBitmap, 0.f, 0.f, null);
    }

    private void drawBackground() {
        clear(mBackgroundCanvas);
        mBackground.draw(mBackgroundCanvas);
        mBackgroundCanvas.drawBitmap(mMaskBitmap, 0.f, 0.f, mPaint);
    }

    // draw() calls onDraw() leading to stack overflow
    @SuppressLint("WrongCall")
    private void drawMask() {
        clear(mMaskCanvas);
        super.onDraw(mMaskCanvas);
    }

    private static void clear(Canvas canvas) {
        canvas.drawColor(Color.BLACK, PorterDuff.Mode.CLEAR);
    }
}

@@ -1352,55 +1352,8 @@
 vas;
-%0A    private boolean mSetBoundsOnSizeAvailable;
 %0A%0A  
@@ -1942,32 +1942,36 @@
 e bg) %7B%0A        
+if (
 mBackground = bg
@@ -1971,13 +1971,15 @@
 nd =
+=
  bg
-;%0A
+) %7B
 %0A   
@@ -1987,197 +1987,431 @@
     
-int w = getWidth();%0A        int h = getHeight();%0A%0A        // Layout has not run%0A        if (w == 0 %7C%7C h == 0) %7B%0A            mSetBoundsOnSizeAvailable = true;%0A            return;%0A        %7D%0A%0A
+    return;%0A        %7D%0A%0A        mBackground = bg;%0A%0A        // Will always draw drawable using view bounds. This might be a%0A        // problem if the drawable should force the view to be bigger, e.g.%0A        // the view sets its dimensions to wrap_content and the drawable%0A        // is larger than the text.%0A        int w = getWidth();%0A        int h = getHeight();%0A        if (mBackground != null && w != 0 && h != 0) %7B%0A    
     
@@ -2441,32 +2441,67 @@
 ds(0, 0, w, h);%0A
+        %7D%0A        requestLayout();%0A
         invalida
@@ -3059,17 +3059,16 @@
 itmap);%0A
-%0A
         
@@ -3076,32 +3076,26 @@
 f (m
-SetBoundsOnSizeAvailable
+Background != null
 ) %7B%0A
@@ -3145,55 +3145,8 @@
 h);%0A
-            mSetBoundsOnSizeAvailable = false;%0A
     
@@ -3213,32 +3213,101 @@
 anvas canvas) %7B%0A
+        // Nothing to draw%0A        if (mBackground == null) return;%0A%0A
         drawMask
@@ -3413,371 +3413,371 @@
     
-private void drawBackground() %7B%0A        clear(mBackgroundCanvas);%0A        mBackground.draw(mBackgroundCanvas);%0A        mBackgroundCanvas.drawBitmap(mMaskBitmap, 0.f, 0.f, mPaint);%0A    %7D%0A%0A    // draw() calls onDraw() leading to stack overflow%0A    @SuppressLint(%22WrongCall%22)%0A    private void drawMask() %7B%0A        clear(mMaskCanvas);%0A        super.onDraw(mMaskCanvas
+// draw() calls onDraw() leading to stack overflow%0A    @SuppressLint(%22WrongCall%22)%0A    private void drawMask() %7B%0A        clear(mMaskCanvas);%0A        super.onDraw(mMaskCanvas);%0A    %7D%0A%0A    private void drawBackground() %7B%0A        clear(mBackgroundCanvas);%0A        mBackground.draw(mBackgroundCanvas);%0A        mBackgroundCanvas.drawBitmap(mMaskBitmap, 0.f, 0.f, mPaint
 );%0A 

Fix issue from pr #1