3

下面代码中的行 (A) 和行 (B) 应生成相同的图像。

正确的图像

然而,行 (A) 却产生了图像:

在此处输入图像描述

怎么了?光栅化后是否应用视图/画布转换?

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.view.View;
import android.graphics.Matrix;

public class MyView extends View {
    Paint paint = new Paint();

    public MyView(Context c) {
        super(c);
        // this.setLayerType(View.LAYER_TYPE_SOFTWARE, null); // (C)
    }

    Rect canvasRect   = new Rect();
    RectF canvasRectf = new RectF();
    RectF scene = new RectF();
    Matrix M = new Matrix();

    @Override
    protected void onDraw(Canvas canvas) {
        getDrawingRect(canvasRect);
        canvasRectf.set(canvasRect);

        scene.set(0,0, 100,100); // (A)
        // scene.set(0,0, 500,500); // (B)

        M.setRectToRect(scene, canvasRectf, Matrix.ScaleToFit.CENTER);
        canvas.setMatrix(M);

        canvas.drawCircle( 50, 50, 1, paint); // (A)
        // canvas.drawCircle(250,250, 5, paint); // (B)
    }
}

更新:如果 (C) 行未注释,则不会出现问题(谢谢,Henry)。使用硬件加速时是否可以获得第一张图像?

4

1 回答 1

0

启用硬件加速后,我只能使用 Android 4.1.2 重现该问题。这可能是一个错误吗?

于 2012-12-26T20:02:25.203 回答