Search code examples
androidopenglcanvasbitmaptextview

Unwanted textview's bitmap outline


I am trying to get bitmap from TextView like this:

private Bitmap getBitmapFromView(View view) {
        Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(returnedBitmap);
        view.draw(canvas);
        return returnedBitmap;
}

But returnedBitmap contains bitmap of TextView with unwanted outline, see picture from Android Studio Debugger. Also I tried using the same function but applied returnedBitmap on ImageView, and it doesnt have such outline. I don't understand how this can be fixed.

UPD. My TextView layout:

<TextView
      android:id="@+id/bitmaptext"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_margin="4dp"
      android:background="@android:color/transparent"
      android:textColor="@color/white"
      android:textSize="18sp"
      tools:text="test"
      tools:textColor="@color/white" />

UPD2. Here is sample project: https://github.com/khoben/test-android, bitmap itself has an outline, but in ImageView doesnt. I need an image without this outline.

UPD3. I want to draw bitmaps from texview on video, but outlining ruins all plans (pic).

UPD4. I should say that problem appeared when image from getBitmapFromView() drawn on video thru OpenGL and the cause was incorrect blending mode. Link to detailed answer.


Solution

  • Actually, the black border of the image in the Android debugger came about because the semi-transparent pixels around the letters and default blending option give black color in result (glClearColor was set to black color). Also OpenGL doing the same.

    Here are some tips with OpenGL:

    1. Adjust the blending parameters (in my case works):

      GLES20.glEnable(GL_BLEND);
      GLES20.glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
      
    2. Make sure that overlay was drawing on your background, e.g. not on a cleared FBO's background.