4

To place a custom font on my widget I am creating a bitmap with the font inside of it, and then placing that bitmap into my RemoteViews. However the text on the bitmap is pretty fuzzy, and looks really pixelated compared to how crystal clear it is within an Activity.

There are quite a few apps already that use custom fonts on a widget. I haven't found a solid way to do this yet though. If anyone has a better solution to this problem I would love to hear it!

Right now this is what I am using to create the bitmap and place it on the widget:

RemoteViews widgetView = new RemoteViews(this.getPackageName(), R.layout.widget);
widgetView.setImageViewBitmap(R.id.widgetImage, buildBitmap());

with

    public Bitmap buildBitmap() {
        Bitmap bitmap = Bitmap.createBitmap(160, 84, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);

        TextPaint textPaint = new TextPaint(TextPaint.LINEAR_TEXT_FLAG | TextPaint.ANTI_ALIAS_FLAG);
        textPaint.setTypeface(getFont());
        textPaint.setStyle(Style.FILL);
        textPaint.setColor(fontColor);
        textPaint.setTextSize(fontSize);

        StaticLayout staticLayout = new StaticLayout(textString, textPaint, bitmap.getWidth(), Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
        staticLayout.draw(canvas);

        return bitmap;
    }

The culprit Widgets! The one on the top is with the custom font, the middle image is with the default Sans font on a bitmap, to show that the text is fuzzy no matter what font is used.

The Last image is what the text looks like when using the default remoteView.setTextViewText("Text"); instead of a bitmap.

Any assistance on this problem would be greatly appreciated!

enter image description here enter image description here enter image description here

vggonz
  • 1,986
  • 1
  • 14
  • 14
Gatekeeper
  • 6,708
  • 6
  • 27
  • 37

2 Answers2

2

You can't assume 160x84. That's a very small size. When scaled it will likely be fuzzy as you can see. Instead, you'll want to actually measure out the bitmap size based on measurements of your text at a given sp. Paint.measureText is useful sometimes for these kinds of things to get the width, but you'll need height as well to do it right, so getTextBounds might be more useful. You'll probably need to add some padding too, but this should get you started. As you can see in your pictures, the clarity is not the only problem you have to deal with. You'll have to figure out word wrapping too (perhaps here is where Paint.measureText will come in handy).

kabuko
  • 36,028
  • 10
  • 80
  • 93
  • Changing the 160x84 to something like 300x400 definitely made a difference! However it isn't the actual size of the widget yet. I have tried both measureText and getTextBounds. They return really large numbers, such as Paint.measureText() returns 7375. – Gatekeeper Jul 25 '13 at 03:28
  • Did you call 'setTextSize' and 'setTextScale' on your paint object with appropriate values? – kabuko Jul 25 '13 at 05:10
  • Right now I'm passing 18 into `setTextSize`, but not doing anything with `setTextScaleX`. Changing `setTextScaleX` just stretches the text left or right, making it skewed. – Gatekeeper Jul 26 '13 at 04:56
0

The ImageView you are using requires android:scaleType="matrix" attribute to be added. Edit your widget layout xml and add that attribute where needed. The ImageView will then not try and scale your image to fit the view.

As previous posts have said your pixel dimensions are wrong. You should be maintaining a record of the size of each of your widgets so you can later use it for creating the bitmap.

If you want help working this bit out I can post some code. Accept this answer and post another about that question specifically and I will answer there with the code.

Simon
  • 10,932
  • 50
  • 49
  • 3
    Please don't extort users to accept answers or post new, kinda related, questions. If you can help, help. If not, don't. – Delyan Jul 29 '13 at 21:49
  • I'm not extorting anyone.. I have actually posted the answer to his question. After re-reading my reply I can see how it may have come across in a different light and how my wording could have been better. I was just saying that the other issue is essentially unrelated and if he wants that question answered he should post another question. – Simon Aug 02 '13 at 10:56