質問

Something weird is happening on android 4.4 with one of my apps. I am using a custom view in which I set a custom typeface with Typeface.createFromAsset. The rendering works fine on the device/emulator as you can see in the image above. The problem appears when I save the content of the custom View as a bitmap:

Bitmap currentBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(currentBitmap);
view.draw(canvas);

Everything works fine on android < 4.4. The saved image looks the same as the one visible on the screen. However, on 4.4 I only see the font borders and the inside of text is transparent. How can I fix this ?

enter image description here

役に立ちましたか?

解決

When drawing text on a canvas in API 19 and above, you have to be mindful of the stroke and fill for the Paint object that you're using. In prior versions of Android, text was always drawn filled, even when using the stroke style. In KitKat, setting the style to stroke will do exactly that; stroke the text rather than fill it. You will need to manually set the paint style to fill when rendering text and then re-set it to stroke for drawing other things (if that's what you're trying to do).

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    mTextPaint.setStyle(Style.STROKE);
    // do other setup on Paint object
    // Draw non-text stuff

    mTextPaint.setStyle(Style.FILL);
    // do other setup on Paint object
    // Draw all the text stuff
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top