0

我在已缩放的画布上绘制文本时遇到问题。我想缩放画布,使绘图尺寸和坐标始终在 0.0 到 1.0 的范围内,即独立于画布的宽度和高度。(如果这是不好的做法,请随时发表评论,说明为什么会这样。)虽然我可以在缩放的画布上正确绘制线条和弧线,但我在绘制文本时遇到了很大的困难,而且这个问题似乎只发生在 Android 上4.2

例如,我在这里创建了一些基于此页面的代码。然而,为了清楚起见,我删除了很多代码。

首先,此代码可以正常工作,并且与上面的链接一样(尽管已精简):

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.View;

public class DrawDemo extends Activity {
DemoView demoview;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    demoview = new DemoView(this);
    setContentView(demoview);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

private class DemoView extends View{
    private int width = 0;
    private int height = 0;

    public DemoView(Context context){
        super(context);
    }

    @Override
    protected void onSizeChanged (int w, int h, int oldw, int oldh){
           width = w;
           height = h;
    }
    @Override protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        // custom drawing code here
        // remember: y increases from top to bottom
        // x increases from left to right
        int x = 0;
        int y = 0;
        Paint paint = new Paint();
        paint.setStyle(Paint.Style.FILL);

        // make the entire canvas white
        paint.setColor(Color.WHITE);
        canvas.drawPaint(paint);
                // draw some text using STROKE style
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(1);
        paint.setColor(Color.MAGENTA);
        paint.setTextSize(30);
        canvas.drawText("Style.STROKE", 75, 75, paint);
    }
  }
}

接下来,我将绝对像素大小替换为标准化(0 到 1.0)值并缩放画布:

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.View;

public class DrawDemo extends Activity {
DemoView demoview;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    demoview = new DemoView(this);
    setContentView(demoview);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

private class DemoView extends View{
    private int width = 0;
    private int height = 0;

    public DemoView(Context context){
        super(context);
    }

    @Override
    protected void onSizeChanged (int w, int h, int oldw, int oldh){
           width = w;
           height = h;
    }
    @Override protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        // custom drawing code here
        // remember: y increases from top to bottom
        // x increases from left to right
        int x = 0;
        int y = 0;
        Paint paint = new Paint();
        paint.setStyle(Paint.Style.FILL);

        // make the entire canvas white
        paint.setColor(Color.WHITE);
        canvas.drawPaint(paint);
                // draw some text using STROKE style
        canvas.scale(width, height);
        paint.setStyle(Paint.Style.STROKE);
        //paint.setStrokeWidth(1);
        paint.setColor(Color.MAGENTA);
        paint.setTextSize(0.2f);
        canvas.drawText("Style.STROKE", 0.5f, 0.5f, paint);
    }
  }
}

结果是屏幕上什么也没有出现。有人可以建议我做错了什么吗?请记住,后者在 Android < 4.2 上对我有用。为代码示例的格式道歉,我仍然无法掌握 Stackoverflows 代码格式。

4

1 回答 1

2

启用硬件加速时,文本会以您在绘图上指定的字体大小呈现为纹理。这意味着在您的情况下,文本以 0.2f 像素进行光栅化。然后在绘制时放大该纹理,这就是为什么你什么都看不到的原因。

虽然此问题已在未来版本的 Android 中得到解决,但我强烈建议您不要使用 0..1 值。您可能不仅会遇到精度问题,还会强制渲染管道(软件和硬件)绕过非常有用的优化。例如,在软件中使用比例变换渲染的文本是使用路径而不是位图块来渲染的。

于 2013-07-22T23:57:37.460 回答