Problems with Android setPolyToPoly (2)

Problems with Android setPolyToPoly (2)

Question:

The question is still the last one ( Problems with Android setPolyToPoly (1) ) Problem, which was solved with hardware acceleration in the previous article, but he may have black edges around the drawn quadrilateral on some mobile phones. It is suspected that the transparency problem was solved after matrix acted on canvas.

Solution:

Matx has problems working on canvas on some systems. So we customize a canvas, first we draw the graphics on our canvas, then we draw the custom canvas on the system canvas.

Encapsulate a CanvasWrapper implementation

public class CanvasWrapper {

    private int mWidth;
    private int mHeight;
    private final Canvas mCanvas = new Canvas();
    private final Rect mDstRect = new Rect();
    private Bitmap mDrawBitmap;

    public CanvasWrapper() {}

    public void setCanvasSize(int width, int height) {
        if (width <= 0 || height <= 0) {
            return;
        }

        mWidth = width;
        mHeight = height;
        mDstRect.set(0, 0, width, height);
        createDrawBitmap();
    }

    public void drawBitmap(Bitmap bitmap, Matrix matrix, Paint mPaint) {
        if (null != bitmap && null != matrix) {
            mCanvas.drawBitmap(bitmap, matrix, mPaint);
        }
    }

    public void draw(Canvas canvas) {
        if (mDrawBitmap != null) {
            canvas.drawBitmap(mDrawBitmap, null, mDstRect, null);
        }
    }

    public Canvas getCanvas() {
        return mCanvas;
    }

    private void createDrawBitmap() {
        final int width = mWidth;
        final int height = mHeight;
        if (null != mDrawBitmap) {
            if (mDrawBitmap.getWidth() != width || mDrawBitmap.getHeight() != height) {
                mDrawBitmap = null;
            }
        }

        if (mDrawBitmap == null) {
            try {
                mDrawBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
            } catch (Exception e) {
                e.printStackTrace();
            } catch (OutOfMemoryError e) {
                e.printStackTrace();
            }
        }

        if (mDrawBitmap != null) {
            reset();
            mCanvas.setBitmap(mDrawBitmap);
        }
    }

    private void reset() {
        if (mDrawBitmap != null) {
            mDrawBitmap.eraseColor(Color.TRANSPARENT);
        }
    }

Then onDraw() changes to this

    @Override
    protected void onDraw(Canvas canvas) {
        mCanvasWrapper.setCanvasSize(canvas.getWidth(), canvas.getHeight());
        mCanvasWrapper.drawBitmap(mBitmap, mPolyMatrix, pointPaint);
        super.onDraw(mCanvasWrapper.getCanvas());
        mCanvasWrapper.draw(canvas);
    }

This can also be solved by customizing canvas Matrix.setPolyToPoly The interface draws a polygon and encounters the problem of picture distortion and misalignment on the Huawei mobile phone.

Record drips and drips, point out if you have problems

Tags: Android Mobile

Posted on Mon, 20 Jul 2020 11:47:46 -0400 by anon