How to distort an image to any quadrangle?

前端 未结 5 1282
执念已碎
执念已碎 2020-12-16 05:19

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. A

5条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-16 06:07

    There is an issue with your code. Although it is the correct method, you have inverted the float[] parameters, as appears in Android documentation:

    public boolean setPolyToPoly (float[] src, int srcIndex, float[] dst, int dstIndex, int pointCount)
    WHERE
    src: The array of src [x,y] pairs (points)
    ...
    dst: The array of dst [x,y] pairs (points)
    

    So, according with your code, the matrix should be created as:

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

    In this way, the app works fine, as shown in the image:

    By the other hand, regarding of what user2498079 says in his comment about the computational problem in low end devices, you can use some easy-to-do techniques to reduce the source image size (and color depth, for example) before matrix conversion calculation. It'd make it easier for the low end phone to realize this task.

提交回复
热议问题