0

I want to inflate a layout, draw it onto a 96x96 bitmap, and send it to MetaWatch, whose 96x96 black-and-white screen has no gradations of gray. In fact, I have managed to do it, but I am not content with the result: the characters are either too large or too thin and unreadable.

If I render the view onto a 96x96 bitmap, I get very large letters on a tiny screen:

public void sendView(View v) {
    Bitmap bm = createViewBitmap(v, 1, 1);
    sendLcdBitmap(bm);
    bm.recycle();
}

If I render the view onto a bigger bitmap and scale it back by calling sendViewScaled(R.layout.test_layout, 1, 4), the scaled text does not look good. That is, it could be smaller, but it will get too thin if I make it smaller.

public void sendViewScaled(View v, int num, int denom) {
    Bitmap bm = createViewBitmap(v, num, denom);
    Bitmap bm2 = Bitmap.createScaledBitmap(bm, SCREEN_SIZE, SCREEN_SIZE, false);
    sendLcdBitmap(bm2);
    bm2.recycle();
    bm.recycle();
}
public void sendViewScaled(int id, int num, int denom) {
    sendViewScaled(inflateView(id), num, denom);
}

The function to convert a view into a bitmap is:

protected Bitmap createViewBitmap(View v, int num, int denom) {
    v.measure(MeasureSpec.makeMeasureSpec(SCREEN_SIZE*num/denom, MeasureSpec.EXACTLY)
             ,MeasureSpec.makeMeasureSpec(SCREEN_SIZE*num/denom, MeasureSpec.EXACTLY)
             );
    Bitmap b = Bitmap.createBitmap(v.getMeasuredWidth(), v.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(b);
    c.setDensity(Bitmap.DENSITY_NONE);//DisplayMetrics.DENSITY_xxx;
    v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
    v.draw(c);
    return b;

}

The function sendLcdBitmap() converts the ARGB_8888 into a single-bit bitmap using the function:

protected boolean isRatherWhite(int color) {
    return Color.red(color)+Color.green(color)+Color.blue(color) > 127*3;
    //return color == Color.WHITE;
}

So, the question is:

I want to render a layout like R.layout.test_layout onto a 96x96 bitmap, B&W, no gradations of gray. How do I get a good quality?

Relevant questions:
Converting a view to Bitmap without displaying it in Android?
getMeasuredWidth returns totally wrong value
How to Resize a Bitmap in Android?
How to convert Views to bitmaps?
http://www.skoumal.net/en/android-how-draw-text-bitmap

UPDATE: playing with the value used in isRatherWhite() helped a bit.

1 Answer 1

0

I think you problem is with the down scaling. You can try different algorithms like the ones mentioned here: What is the best image downscaling algorithm (quality-wise)?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.