Android subpixel rendering
Asked Answered
E

1

0

I have a line that should get thinner the longer it gets. The problem is, that you can clearly see a jump when it gets a pixel thinner. Is there a way to do subpixel rendering/antialiasing on Android?

canvas.drawRect() takes float values, but it's ignoring those. Here's the code:

@Override
protected void onDraw(Canvas canvas) {
    float width = getMeasuredWidth() / (float) getMeasuredHeight()  * getMinimumHeight();
    float left = (getMeasuredWidth() - width) / 2.0f;
    canvas.drawRect(left, 0, left + width, getMeasuredHeight(), paint);
    super.onDraw(canvas);
}

The paint object has ANTI_ALIAS_FLAG enabled and contains a solid color.

This is the default line:

Default line

This is when it gets longer and thinner. It should have some anti aliasing on the sides, though to make the whole transition seems smoother.

Thinner line

Eade answered 18/1, 2013 at 2:7 Comment(5)
Have you tried setAntiAlias( true )? It's easy to accidentally override a flag using setFlags().Ratan
No, it's not working either. I'm not setting any other flags :/Eade
Could you post a screenshot?Pinch
I've edited my post and added screenshots, thanks!Eade
Actually drawLine with the strokeWidth set seems to do the job!Eade
E
0

This seems to do a better job:

@Override
protected void onDraw(Canvas canvas) {
    float width = getMeasuredWidth() / (float) getMeasuredHeight()  * getMinimumHeight();
    float left = (getMeasuredWidth() - width) / 2.0f;
    paint.setStrokeWidth(width * getResources().getDisplayMetrics().density);
    canvas.drawLine(left, 0, left, getMeasuredHeight(), paint);
    super.onDraw(canvas);
}
Eade answered 18/1, 2013 at 18:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.