Android子像素渲染

时间:2013-01-18 02:07:07

标签: android android-canvas subpixel

我有一条线应该越长越好。问题是,当像素变薄时,你可以清楚地看到跳跃。有没有办法在Android上进行子像素渲染/抗锯齿?

canvas.drawRect()获取浮动值,但忽略了这些值。这是代码:

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

绘制对象启用了ANTI_ALIAS_FLAG并包含纯色。

这是默认行:

Default line

这是它变得更长更薄的时候。它应该在侧面有一些抗锯齿,但要使整个过渡看起来更平滑。

Thinner line

1 个答案:

答案 0 :(得分:0)

这似乎做得更好:

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