Extract clear text using Canvas.drawText in Android

I am making an AppWidget, and in its settings I allow the user to enable / disable the text shadow. Since I cannot call the shadow method through the RemoteViews class, I make a "draw" method that dynamically draws the widget and its container.

When drawing text, it becomes blurry and not as crispy as when using TextView. The only code I used to draw text:

Paint p = new Paint();
p.setAntiAlias(true);
p.setColor(Color.WHITE);

Is there any other magic to make it clearer?

+5
source share
3 answers
Paint paint = new Paint(Paint.LINEAR_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);

did the trick for me

+11
source

These are my text paint settings:

    textPaint = new Paint();
    textPaint.setStyle(Paint.Style.FILL);
    textPaint.setAntiAlias(true);
    textPaint.setARGB(255, 255, 255, 255);
    textPaint.setFakeBoldText(true);
    textPaint.setTextSize(textSize);

This seems to work well for me.

+1

text setAntiAlias(true) (in hardware mode) only works with API 18 and above, so use this code to set the type of your layer.

int currentapiVersion = android.os.Build.VERSION.SDK_INT;
    if (currentapiVersion >= android.os.Build.VERSION_CODES.HONEYCOMB && currentapiVersion < android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) {
        this.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
    }
0
source

All Articles