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 に答える