What code should I use with 'drawMatrix.setPol

2019-08-07 02:29发布

问题:

When i want to use just part of a bitmap without skewing or rotating it, like the top left fourth, the following works well:

src_rect.set(0, 0, bmp_test.getWidth()/2,bmp_test.getHeight()/2);

canvas.drawBitmap(src_rect, dst_rectF, paint);

This draws just one fourth of the bitmap.

However, if i want to rotate or skew just part of a bitmap using a matrix, the following does not work as expected:

srcpairs[0] = 0;
srcpairs[1] = 0;
srcpairs[2] = (float)bmp_test.getWidth()/2;
srcpairs[3] = 0;
srcpairs[4] = (float)bmp_test.getWidth()/2;
srcpairs[5] = (float)bmp_test.getHeight()/2;
srcpairs[6] = 0;
srcpairs[7] = (float)bmp_test.getHeight()/2;

drawMatrix.setPolyToPoly(srcpairs, 0, dstpairs, 0, 4);
canvas.drawBitmap(bmp_test, drawMatrix, paint);

This does not just draw the top left forth of the bitmap, like

canvas.drawBitmap(src_rect, dst_rectF, paint) 

does. Instead it draws the entire bitmap, 4 times bigger than the dstpairs area, aligning the top left fourth of the bitmap (the srcpairs points) to the destpairs points.

What are the simplest changes to be made to this matrix code to manipulate and draw just a rectangular part of a bitmap?

I want to avoid creating separate bitmaps of parts of the original image. That is what I'm having to do now.