如果提供旋转矩阵,canvas.drawBitmap不会绘制任何内容

时间:2015-07-21 15:11:09

标签: android android-camera android-canvas

我的应用程序中的一个活动拍摄照片,然后调用另一个打开图片进行编辑的活动。问题是,我无法将我拍摄的照片以正确的方式显示在我的画布上。

结构是这样的:

布局

<RelativeLayout>
    <SurfaceView id="the_surface_view" />
    <Other_Stuff />
</RelativeLayout>

活动

class EditorActivity extends Activity implements SurfaceHolder.Callback {
    private Bitmap image;
    private Matrix rotationMatrix;
    ...
    public void onCreate(){
        ...
        SurfaceView surface = (SurfaceView) findViewById(R.id.the_surface_view);
        SurfaceHolder holder = surface.getHolder();
        holder.addCallback(this);
        editor.setWillNotDraw(false);

        String imageFile = getIntent.getStringExtra("file");
        image = BitmapFactory.decodeFile(imageFile);

        rotationMatrix = new Matrix();
        rotationMatrix.postRotate(90); 
        // same angle as the camera preview, will be dynamic later
        ...
    }

    protected void onDraw(canvas canvas){
        canvas.drawRGB(255,0,255); // just to make sure that it works
        canvas.drawBitmap(image, rotationMatrix, null);
    }

    @Override
        public void surfaceCreated(SurfaceHolder holder) {
        Canvas canvas = null;
        try {
            canvas = holder.lockCanvas();
            synchronized (holder){
                onDraw(canvas);
            }
        } catch (Exception e){
            Log.e(TAG, "failed canvas lock.",e);
        } finally {
            if (canvas != null){
                holder.unlockCanvasAndPost(canvas);
            }
        }
    }
    ...
}

所有这一切都是画布变成洋红色 奇怪的是,当我注释掉canvas.postRotation(90)时,图像会显示出来(虽然旋转错误)。

如何让我的图像正确显示?

0 个答案:

没有答案