开发者

Render crisp text using Canvas.drawText in Android

开发者 https://www.devze.com 2022-12-30 19:43 出处:网络
I\'m doing an AppWidget and in its settings I\'m letting the user enable/disable text shadow. Since I can\'t invoke the shadow method through the RemoteViews class, I\'m doing a \"draw\" method that d

I'm doing an AppWidget and in its settings I'm letting the user enable/disable text shadow. Since I can't invoke the shadow method through the RemoteViews class, I'm doing a "draw" method that dynamica开发者_高级运维lly paints the widget and its container.

When drawing the text though, it gets kinda blurred and not that crisp like when using a TextView. The only code I've used for the text painting is:

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

Are there any other magic I need to do for it to become more crisp?


Paint paint = new Paint(Paint.LINEAR_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);

did the trick for me


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);

Seems to be working well for me.


The text setAntiAlias(true) (In Hardware mode) only works from API 18 and above so use this code to set your layer type.

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

精彩评论

暂无评论...
验证码 换一张
取 消