Are Android View transformations applied after ras

2019-07-07 03:46发布

问题:

The lines (A) and the lines (B) in the code below should produce the same image.

Yet lines (A) produce instead the image:

What is happening? Are View/Canvas transformations applied after rasterization?

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

Update: If line (C) is uncommented, the problem does not arise (Thanks, Henry). Is it possible to get the first image while using hardware acceleration?

回答1:

I could reproduce the problem only with Android 4.1.2 when hardware acceleration is enabled. Could it be a bug?