Geki Geki - 5 months ago 33
Android Question

How to distort an image to any quadrangle?

Do any of you have an idea, how to distort an image in any quadrangle?
I want to implement an image, which you can pull any corner in any direction, distorting the image. Anyone has an idea how to do that? I use and write stuff in android for a while now, but it does not seem like android has a function for that. I don't really feel like writing a new math library :).



Looks like you need Canvas.drawBitmapMesh . There is a sample in Android SDK showing how to use it.

You need to use Matrix for drawing your bitmap on Canvas. You can easily create such transformation which will fit your bitmap image into any quadrangle with Matrix.polyToPoly method. It will look like this:

        new float[] { 
            0, 0, 
            bitmap.getWidth(), 0
            0, bitmap.getHeight(),
            bitmap.getWidth(), bitmap.getHeight() 
        }, 0, 
        new float[] { 
            x0, y0, 
            x1, y1, 
            x2, y2,
            x3, y3
        }, 0, 

canvas.drawBitmap(bitmap, matrix, paint);

Where x0-x3, y0-y3 are your quadrangle vertex coordinates.