How drawBitmapMesh works in android canvas












4















I want to draw a bitmap on a rectangle. I use the values below:



    this.meshWidth = 1;
this.meshHeight = 1;
this.verts = new float[8];
this.points[0].x = (float)(this.getWidth()/4);
this.points[0].y = (float)(this.getHeight()/4);
this.points[1].x = (float)(this.points[0].x+this.getWidth()/2);
this.points[1].y = (float)(this.points[0].y);
this.points[2].x = (float)(this.points[0].x);
this.points[2].y = (float)(this.points[0].y+this.getHeight()/2);
this.points[3].x = (float)(this.points[1].x);
this.points[3].y = (float)(this.points[2].y);


points array is my vertex array.



my onDraw method



public void onDraw(Canvas canvas){
//canvas.drawBitmap(bitmap, 20,20, null);
Paint paint = new Paint();
paint.setColor(Color.BLUE);
canvas.drawLine(this.points[0].x, this.points[0].y, this.points[1].x, this.points[1].y, paint);
canvas.drawLine(this.points[1].x, this.points[1].y, this.points[3].x, this.points[3].y, paint);
canvas.drawLine(this.points[3].x, this.points[3].y, this.points[2].x, this.points[2].y, paint);
canvas.drawLine(this.points[2].x, this.points[2].y, this.points[0].x, this.points[0].y, paint);
canvas.drawBitmapMesh(this.bitmap, meshWidth, meshHeight, verts, 0, null, 0, null);
}


The output is this
alt text



I want to draw bitmap on rectangle surrounded with blue lines..










share|improve this question

























  • This answer is related: stackoverflow.com/a/42962558/897007

    – Dale
    Dec 6 '18 at 16:29
















4















I want to draw a bitmap on a rectangle. I use the values below:



    this.meshWidth = 1;
this.meshHeight = 1;
this.verts = new float[8];
this.points[0].x = (float)(this.getWidth()/4);
this.points[0].y = (float)(this.getHeight()/4);
this.points[1].x = (float)(this.points[0].x+this.getWidth()/2);
this.points[1].y = (float)(this.points[0].y);
this.points[2].x = (float)(this.points[0].x);
this.points[2].y = (float)(this.points[0].y+this.getHeight()/2);
this.points[3].x = (float)(this.points[1].x);
this.points[3].y = (float)(this.points[2].y);


points array is my vertex array.



my onDraw method



public void onDraw(Canvas canvas){
//canvas.drawBitmap(bitmap, 20,20, null);
Paint paint = new Paint();
paint.setColor(Color.BLUE);
canvas.drawLine(this.points[0].x, this.points[0].y, this.points[1].x, this.points[1].y, paint);
canvas.drawLine(this.points[1].x, this.points[1].y, this.points[3].x, this.points[3].y, paint);
canvas.drawLine(this.points[3].x, this.points[3].y, this.points[2].x, this.points[2].y, paint);
canvas.drawLine(this.points[2].x, this.points[2].y, this.points[0].x, this.points[0].y, paint);
canvas.drawBitmapMesh(this.bitmap, meshWidth, meshHeight, verts, 0, null, 0, null);
}


The output is this
alt text



I want to draw bitmap on rectangle surrounded with blue lines..










share|improve this question

























  • This answer is related: stackoverflow.com/a/42962558/897007

    – Dale
    Dec 6 '18 at 16:29














4












4








4








I want to draw a bitmap on a rectangle. I use the values below:



    this.meshWidth = 1;
this.meshHeight = 1;
this.verts = new float[8];
this.points[0].x = (float)(this.getWidth()/4);
this.points[0].y = (float)(this.getHeight()/4);
this.points[1].x = (float)(this.points[0].x+this.getWidth()/2);
this.points[1].y = (float)(this.points[0].y);
this.points[2].x = (float)(this.points[0].x);
this.points[2].y = (float)(this.points[0].y+this.getHeight()/2);
this.points[3].x = (float)(this.points[1].x);
this.points[3].y = (float)(this.points[2].y);


points array is my vertex array.



my onDraw method



public void onDraw(Canvas canvas){
//canvas.drawBitmap(bitmap, 20,20, null);
Paint paint = new Paint();
paint.setColor(Color.BLUE);
canvas.drawLine(this.points[0].x, this.points[0].y, this.points[1].x, this.points[1].y, paint);
canvas.drawLine(this.points[1].x, this.points[1].y, this.points[3].x, this.points[3].y, paint);
canvas.drawLine(this.points[3].x, this.points[3].y, this.points[2].x, this.points[2].y, paint);
canvas.drawLine(this.points[2].x, this.points[2].y, this.points[0].x, this.points[0].y, paint);
canvas.drawBitmapMesh(this.bitmap, meshWidth, meshHeight, verts, 0, null, 0, null);
}


The output is this
alt text



I want to draw bitmap on rectangle surrounded with blue lines..










share|improve this question
















I want to draw a bitmap on a rectangle. I use the values below:



    this.meshWidth = 1;
this.meshHeight = 1;
this.verts = new float[8];
this.points[0].x = (float)(this.getWidth()/4);
this.points[0].y = (float)(this.getHeight()/4);
this.points[1].x = (float)(this.points[0].x+this.getWidth()/2);
this.points[1].y = (float)(this.points[0].y);
this.points[2].x = (float)(this.points[0].x);
this.points[2].y = (float)(this.points[0].y+this.getHeight()/2);
this.points[3].x = (float)(this.points[1].x);
this.points[3].y = (float)(this.points[2].y);


points array is my vertex array.



my onDraw method



public void onDraw(Canvas canvas){
//canvas.drawBitmap(bitmap, 20,20, null);
Paint paint = new Paint();
paint.setColor(Color.BLUE);
canvas.drawLine(this.points[0].x, this.points[0].y, this.points[1].x, this.points[1].y, paint);
canvas.drawLine(this.points[1].x, this.points[1].y, this.points[3].x, this.points[3].y, paint);
canvas.drawLine(this.points[3].x, this.points[3].y, this.points[2].x, this.points[2].y, paint);
canvas.drawLine(this.points[2].x, this.points[2].y, this.points[0].x, this.points[0].y, paint);
canvas.drawBitmapMesh(this.bitmap, meshWidth, meshHeight, verts, 0, null, 0, null);
}


The output is this
alt text



I want to draw bitmap on rectangle surrounded with blue lines..







android canvas






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 6 '12 at 16:16









Antinous

90821533




90821533










asked Jan 5 '11 at 22:23









kml_ckrkml_ckr

1,14521530




1,14521530













  • This answer is related: stackoverflow.com/a/42962558/897007

    – Dale
    Dec 6 '18 at 16:29



















  • This answer is related: stackoverflow.com/a/42962558/897007

    – Dale
    Dec 6 '18 at 16:29

















This answer is related: stackoverflow.com/a/42962558/897007

– Dale
Dec 6 '18 at 16:29





This answer is related: stackoverflow.com/a/42962558/897007

– Dale
Dec 6 '18 at 16:29












4 Answers
4






active

oldest

votes


















9














Everything you need to understand the DrawBitmapMesh function can be found in the Android developer documentation. See Android developer documentation.



However, I will explain it here in my own words. The syntax for the function is:



public void drawBitmapMesh (Bitmap bitmap, int meshWidth, int meshHeight, float verts, int vertOffset, int colors, int colorOffset, Paint paint)


The bitmap is clearly the bitmap you want to use. Now imagine a grid over the bitmap with meshWidth+1 points along the rows of the image and meshHeight+1 points down the columns of the bitmap. You specify these points, or vertices, in the verts variable. These are entered in row-major format, meaning that you enter the vertices in vert from left to right for row 1, then left to right for row 2, and so on, i.e. if we have 4 x 4 points, then we have something like this:



*01 *02 *03 *04



*05 *06 *07 *08



*09 *10 *11 *12



*13 *14 *15 *16



where *n is a vertex (x, y) with coordinates laid appropriately over the bitmap image. You would define your vert array as:



vert[0] = (*01).x;
vert[1] = (*01).y;
vert[2] = (*02).x;
vert[3] = (*02).y;
...


If you were to distribute these points uniformly across the bitmap then the drawBitmapMesh would in theory give the same output as the drawBitmap function. However, if you displace these vertices away from their "natural" position, then the drawBitmapMesh will begin to stretch the bitmap as per specification.



You can set the remaining function arguments to 0, null, 0, null respectively as you have done in your example.






share|improve this answer

































    2














    If all you are doing is draw a rectangle, you don't need to use a mesh at all. It will be more expensive to use the mesh.






    share|improve this answer
























    • i know but i want to understand how the logic behind the method.

      – kml_ckr
      Jan 5 '11 at 22:47



















    1














    You're not initializing verts in the code above. It should do what you want if you actually assign values to the verts elements (though, as Romain said, it's not the best way to accomplish the task).






    share|improve this answer































      0














      public class MainActivity extends Activity
      {
      private Bitmap bitmap;
      @Override
      public void onCreate(Bundle savedInstanceState)
      {
      super.onCreate(savedInstanceState);
      setContentView(new MyView(this, R.drawable.jinta));
      }
      private class MyView extends View
      {
      private final int WIDTH = 20;
      private final int HEIGHT = 20;

      private final int COUNT = (WIDTH + 1) * (HEIGHT + 1);

      private final float verts = new float[COUNT * 2];

      private final float orig = new float[COUNT * 2];
      public MyView(Context context, int drawableId)
      {
      super(context);
      setFocusable(true);
      bitmap = BitmapFactory.decodeResource(getResources()
      , drawableId);
      float bitmapWidth = bitmap.getWidth();
      float bitmapHeight = bitmap.getHeight();
      int index = 0;
      for (int y = 0; y <= HEIGHT; y++)
      {
      float fy = bitmapHeight * y / HEIGHT;
      for (int x = 0; x <= WIDTH; x++)
      {
      float fx = bitmapWidth * x / WIDTH;

      orig[index * 2 + 0] = verts[index * 2 + 0] = fx;
      orig[index * 2 + 1] = verts[index * 2 + 1] = fy;
      index += 1;
      }
      }

      setBackgroundColor(Color.WHITE);
      }
      @Override
      protected void onDraw(Canvas canvas)
      {
      canvas.drawBitmapMesh(bitmap, WIDTH, HEIGHT, verts
      , 0, null, 0,null);
      }

      private void warp(float cx, float cy)
      {
      for (int i = 0; i < COUNT * 2; i += 2)
      {
      float dx = cx - orig[i + 0];
      float dy = cy - orig[i + 1];
      float dd = dx * dx + dy * dy;
      float d = (float) Math.sqrt(dd);

      float pull = 100000 / ((float) (dd * d));

      if (pull >= 1)
      {
      verts[i + 0] = cx;
      verts[i + 1] = cy;
      }
      else
      {

      verts[i + 0] = orig[i + 0] + dx * pull;
      verts[i + 1] = orig[i + 1] + dy * pull;
      }
      }
      invalidate();
      }
      @Override
      public boolean onTouchEvent(MotionEvent event)
      {
      warp(event.getX(), event.getY());
      return true;
      }
      }
      }





      share|improve this answer

























        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f4609825%2fhow-drawbitmapmesh-works-in-android-canvas%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        4 Answers
        4






        active

        oldest

        votes








        4 Answers
        4






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        9














        Everything you need to understand the DrawBitmapMesh function can be found in the Android developer documentation. See Android developer documentation.



        However, I will explain it here in my own words. The syntax for the function is:



        public void drawBitmapMesh (Bitmap bitmap, int meshWidth, int meshHeight, float verts, int vertOffset, int colors, int colorOffset, Paint paint)


        The bitmap is clearly the bitmap you want to use. Now imagine a grid over the bitmap with meshWidth+1 points along the rows of the image and meshHeight+1 points down the columns of the bitmap. You specify these points, or vertices, in the verts variable. These are entered in row-major format, meaning that you enter the vertices in vert from left to right for row 1, then left to right for row 2, and so on, i.e. if we have 4 x 4 points, then we have something like this:



        *01 *02 *03 *04



        *05 *06 *07 *08



        *09 *10 *11 *12



        *13 *14 *15 *16



        where *n is a vertex (x, y) with coordinates laid appropriately over the bitmap image. You would define your vert array as:



        vert[0] = (*01).x;
        vert[1] = (*01).y;
        vert[2] = (*02).x;
        vert[3] = (*02).y;
        ...


        If you were to distribute these points uniformly across the bitmap then the drawBitmapMesh would in theory give the same output as the drawBitmap function. However, if you displace these vertices away from their "natural" position, then the drawBitmapMesh will begin to stretch the bitmap as per specification.



        You can set the remaining function arguments to 0, null, 0, null respectively as you have done in your example.






        share|improve this answer






























          9














          Everything you need to understand the DrawBitmapMesh function can be found in the Android developer documentation. See Android developer documentation.



          However, I will explain it here in my own words. The syntax for the function is:



          public void drawBitmapMesh (Bitmap bitmap, int meshWidth, int meshHeight, float verts, int vertOffset, int colors, int colorOffset, Paint paint)


          The bitmap is clearly the bitmap you want to use. Now imagine a grid over the bitmap with meshWidth+1 points along the rows of the image and meshHeight+1 points down the columns of the bitmap. You specify these points, or vertices, in the verts variable. These are entered in row-major format, meaning that you enter the vertices in vert from left to right for row 1, then left to right for row 2, and so on, i.e. if we have 4 x 4 points, then we have something like this:



          *01 *02 *03 *04



          *05 *06 *07 *08



          *09 *10 *11 *12



          *13 *14 *15 *16



          where *n is a vertex (x, y) with coordinates laid appropriately over the bitmap image. You would define your vert array as:



          vert[0] = (*01).x;
          vert[1] = (*01).y;
          vert[2] = (*02).x;
          vert[3] = (*02).y;
          ...


          If you were to distribute these points uniformly across the bitmap then the drawBitmapMesh would in theory give the same output as the drawBitmap function. However, if you displace these vertices away from their "natural" position, then the drawBitmapMesh will begin to stretch the bitmap as per specification.



          You can set the remaining function arguments to 0, null, 0, null respectively as you have done in your example.






          share|improve this answer




























            9












            9








            9







            Everything you need to understand the DrawBitmapMesh function can be found in the Android developer documentation. See Android developer documentation.



            However, I will explain it here in my own words. The syntax for the function is:



            public void drawBitmapMesh (Bitmap bitmap, int meshWidth, int meshHeight, float verts, int vertOffset, int colors, int colorOffset, Paint paint)


            The bitmap is clearly the bitmap you want to use. Now imagine a grid over the bitmap with meshWidth+1 points along the rows of the image and meshHeight+1 points down the columns of the bitmap. You specify these points, or vertices, in the verts variable. These are entered in row-major format, meaning that you enter the vertices in vert from left to right for row 1, then left to right for row 2, and so on, i.e. if we have 4 x 4 points, then we have something like this:



            *01 *02 *03 *04



            *05 *06 *07 *08



            *09 *10 *11 *12



            *13 *14 *15 *16



            where *n is a vertex (x, y) with coordinates laid appropriately over the bitmap image. You would define your vert array as:



            vert[0] = (*01).x;
            vert[1] = (*01).y;
            vert[2] = (*02).x;
            vert[3] = (*02).y;
            ...


            If you were to distribute these points uniformly across the bitmap then the drawBitmapMesh would in theory give the same output as the drawBitmap function. However, if you displace these vertices away from their "natural" position, then the drawBitmapMesh will begin to stretch the bitmap as per specification.



            You can set the remaining function arguments to 0, null, 0, null respectively as you have done in your example.






            share|improve this answer















            Everything you need to understand the DrawBitmapMesh function can be found in the Android developer documentation. See Android developer documentation.



            However, I will explain it here in my own words. The syntax for the function is:



            public void drawBitmapMesh (Bitmap bitmap, int meshWidth, int meshHeight, float verts, int vertOffset, int colors, int colorOffset, Paint paint)


            The bitmap is clearly the bitmap you want to use. Now imagine a grid over the bitmap with meshWidth+1 points along the rows of the image and meshHeight+1 points down the columns of the bitmap. You specify these points, or vertices, in the verts variable. These are entered in row-major format, meaning that you enter the vertices in vert from left to right for row 1, then left to right for row 2, and so on, i.e. if we have 4 x 4 points, then we have something like this:



            *01 *02 *03 *04



            *05 *06 *07 *08



            *09 *10 *11 *12



            *13 *14 *15 *16



            where *n is a vertex (x, y) with coordinates laid appropriately over the bitmap image. You would define your vert array as:



            vert[0] = (*01).x;
            vert[1] = (*01).y;
            vert[2] = (*02).x;
            vert[3] = (*02).y;
            ...


            If you were to distribute these points uniformly across the bitmap then the drawBitmapMesh would in theory give the same output as the drawBitmap function. However, if you displace these vertices away from their "natural" position, then the drawBitmapMesh will begin to stretch the bitmap as per specification.



            You can set the remaining function arguments to 0, null, 0, null respectively as you have done in your example.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Sep 10 '15 at 7:34

























            answered Nov 6 '12 at 16:25









            AntinousAntinous

            90821533




            90821533

























                2














                If all you are doing is draw a rectangle, you don't need to use a mesh at all. It will be more expensive to use the mesh.






                share|improve this answer
























                • i know but i want to understand how the logic behind the method.

                  – kml_ckr
                  Jan 5 '11 at 22:47
















                2














                If all you are doing is draw a rectangle, you don't need to use a mesh at all. It will be more expensive to use the mesh.






                share|improve this answer
























                • i know but i want to understand how the logic behind the method.

                  – kml_ckr
                  Jan 5 '11 at 22:47














                2












                2








                2







                If all you are doing is draw a rectangle, you don't need to use a mesh at all. It will be more expensive to use the mesh.






                share|improve this answer













                If all you are doing is draw a rectangle, you don't need to use a mesh at all. It will be more expensive to use the mesh.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jan 5 '11 at 22:40









                Romain GuyRomain Guy

                89.2k16203190




                89.2k16203190













                • i know but i want to understand how the logic behind the method.

                  – kml_ckr
                  Jan 5 '11 at 22:47



















                • i know but i want to understand how the logic behind the method.

                  – kml_ckr
                  Jan 5 '11 at 22:47

















                i know but i want to understand how the logic behind the method.

                – kml_ckr
                Jan 5 '11 at 22:47





                i know but i want to understand how the logic behind the method.

                – kml_ckr
                Jan 5 '11 at 22:47











                1














                You're not initializing verts in the code above. It should do what you want if you actually assign values to the verts elements (though, as Romain said, it's not the best way to accomplish the task).






                share|improve this answer




























                  1














                  You're not initializing verts in the code above. It should do what you want if you actually assign values to the verts elements (though, as Romain said, it's not the best way to accomplish the task).






                  share|improve this answer


























                    1












                    1








                    1







                    You're not initializing verts in the code above. It should do what you want if you actually assign values to the verts elements (though, as Romain said, it's not the best way to accomplish the task).






                    share|improve this answer













                    You're not initializing verts in the code above. It should do what you want if you actually assign values to the verts elements (though, as Romain said, it's not the best way to accomplish the task).







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Oct 16 '12 at 14:31









                    Chet HaaseChet Haase

                    1,09177




                    1,09177























                        0














                        public class MainActivity extends Activity
                        {
                        private Bitmap bitmap;
                        @Override
                        public void onCreate(Bundle savedInstanceState)
                        {
                        super.onCreate(savedInstanceState);
                        setContentView(new MyView(this, R.drawable.jinta));
                        }
                        private class MyView extends View
                        {
                        private final int WIDTH = 20;
                        private final int HEIGHT = 20;

                        private final int COUNT = (WIDTH + 1) * (HEIGHT + 1);

                        private final float verts = new float[COUNT * 2];

                        private final float orig = new float[COUNT * 2];
                        public MyView(Context context, int drawableId)
                        {
                        super(context);
                        setFocusable(true);
                        bitmap = BitmapFactory.decodeResource(getResources()
                        , drawableId);
                        float bitmapWidth = bitmap.getWidth();
                        float bitmapHeight = bitmap.getHeight();
                        int index = 0;
                        for (int y = 0; y <= HEIGHT; y++)
                        {
                        float fy = bitmapHeight * y / HEIGHT;
                        for (int x = 0; x <= WIDTH; x++)
                        {
                        float fx = bitmapWidth * x / WIDTH;

                        orig[index * 2 + 0] = verts[index * 2 + 0] = fx;
                        orig[index * 2 + 1] = verts[index * 2 + 1] = fy;
                        index += 1;
                        }
                        }

                        setBackgroundColor(Color.WHITE);
                        }
                        @Override
                        protected void onDraw(Canvas canvas)
                        {
                        canvas.drawBitmapMesh(bitmap, WIDTH, HEIGHT, verts
                        , 0, null, 0,null);
                        }

                        private void warp(float cx, float cy)
                        {
                        for (int i = 0; i < COUNT * 2; i += 2)
                        {
                        float dx = cx - orig[i + 0];
                        float dy = cy - orig[i + 1];
                        float dd = dx * dx + dy * dy;
                        float d = (float) Math.sqrt(dd);

                        float pull = 100000 / ((float) (dd * d));

                        if (pull >= 1)
                        {
                        verts[i + 0] = cx;
                        verts[i + 1] = cy;
                        }
                        else
                        {

                        verts[i + 0] = orig[i + 0] + dx * pull;
                        verts[i + 1] = orig[i + 1] + dy * pull;
                        }
                        }
                        invalidate();
                        }
                        @Override
                        public boolean onTouchEvent(MotionEvent event)
                        {
                        warp(event.getX(), event.getY());
                        return true;
                        }
                        }
                        }





                        share|improve this answer






























                          0














                          public class MainActivity extends Activity
                          {
                          private Bitmap bitmap;
                          @Override
                          public void onCreate(Bundle savedInstanceState)
                          {
                          super.onCreate(savedInstanceState);
                          setContentView(new MyView(this, R.drawable.jinta));
                          }
                          private class MyView extends View
                          {
                          private final int WIDTH = 20;
                          private final int HEIGHT = 20;

                          private final int COUNT = (WIDTH + 1) * (HEIGHT + 1);

                          private final float verts = new float[COUNT * 2];

                          private final float orig = new float[COUNT * 2];
                          public MyView(Context context, int drawableId)
                          {
                          super(context);
                          setFocusable(true);
                          bitmap = BitmapFactory.decodeResource(getResources()
                          , drawableId);
                          float bitmapWidth = bitmap.getWidth();
                          float bitmapHeight = bitmap.getHeight();
                          int index = 0;
                          for (int y = 0; y <= HEIGHT; y++)
                          {
                          float fy = bitmapHeight * y / HEIGHT;
                          for (int x = 0; x <= WIDTH; x++)
                          {
                          float fx = bitmapWidth * x / WIDTH;

                          orig[index * 2 + 0] = verts[index * 2 + 0] = fx;
                          orig[index * 2 + 1] = verts[index * 2 + 1] = fy;
                          index += 1;
                          }
                          }

                          setBackgroundColor(Color.WHITE);
                          }
                          @Override
                          protected void onDraw(Canvas canvas)
                          {
                          canvas.drawBitmapMesh(bitmap, WIDTH, HEIGHT, verts
                          , 0, null, 0,null);
                          }

                          private void warp(float cx, float cy)
                          {
                          for (int i = 0; i < COUNT * 2; i += 2)
                          {
                          float dx = cx - orig[i + 0];
                          float dy = cy - orig[i + 1];
                          float dd = dx * dx + dy * dy;
                          float d = (float) Math.sqrt(dd);

                          float pull = 100000 / ((float) (dd * d));

                          if (pull >= 1)
                          {
                          verts[i + 0] = cx;
                          verts[i + 1] = cy;
                          }
                          else
                          {

                          verts[i + 0] = orig[i + 0] + dx * pull;
                          verts[i + 1] = orig[i + 1] + dy * pull;
                          }
                          }
                          invalidate();
                          }
                          @Override
                          public boolean onTouchEvent(MotionEvent event)
                          {
                          warp(event.getX(), event.getY());
                          return true;
                          }
                          }
                          }





                          share|improve this answer




























                            0












                            0








                            0







                            public class MainActivity extends Activity
                            {
                            private Bitmap bitmap;
                            @Override
                            public void onCreate(Bundle savedInstanceState)
                            {
                            super.onCreate(savedInstanceState);
                            setContentView(new MyView(this, R.drawable.jinta));
                            }
                            private class MyView extends View
                            {
                            private final int WIDTH = 20;
                            private final int HEIGHT = 20;

                            private final int COUNT = (WIDTH + 1) * (HEIGHT + 1);

                            private final float verts = new float[COUNT * 2];

                            private final float orig = new float[COUNT * 2];
                            public MyView(Context context, int drawableId)
                            {
                            super(context);
                            setFocusable(true);
                            bitmap = BitmapFactory.decodeResource(getResources()
                            , drawableId);
                            float bitmapWidth = bitmap.getWidth();
                            float bitmapHeight = bitmap.getHeight();
                            int index = 0;
                            for (int y = 0; y <= HEIGHT; y++)
                            {
                            float fy = bitmapHeight * y / HEIGHT;
                            for (int x = 0; x <= WIDTH; x++)
                            {
                            float fx = bitmapWidth * x / WIDTH;

                            orig[index * 2 + 0] = verts[index * 2 + 0] = fx;
                            orig[index * 2 + 1] = verts[index * 2 + 1] = fy;
                            index += 1;
                            }
                            }

                            setBackgroundColor(Color.WHITE);
                            }
                            @Override
                            protected void onDraw(Canvas canvas)
                            {
                            canvas.drawBitmapMesh(bitmap, WIDTH, HEIGHT, verts
                            , 0, null, 0,null);
                            }

                            private void warp(float cx, float cy)
                            {
                            for (int i = 0; i < COUNT * 2; i += 2)
                            {
                            float dx = cx - orig[i + 0];
                            float dy = cy - orig[i + 1];
                            float dd = dx * dx + dy * dy;
                            float d = (float) Math.sqrt(dd);

                            float pull = 100000 / ((float) (dd * d));

                            if (pull >= 1)
                            {
                            verts[i + 0] = cx;
                            verts[i + 1] = cy;
                            }
                            else
                            {

                            verts[i + 0] = orig[i + 0] + dx * pull;
                            verts[i + 1] = orig[i + 1] + dy * pull;
                            }
                            }
                            invalidate();
                            }
                            @Override
                            public boolean onTouchEvent(MotionEvent event)
                            {
                            warp(event.getX(), event.getY());
                            return true;
                            }
                            }
                            }





                            share|improve this answer















                            public class MainActivity extends Activity
                            {
                            private Bitmap bitmap;
                            @Override
                            public void onCreate(Bundle savedInstanceState)
                            {
                            super.onCreate(savedInstanceState);
                            setContentView(new MyView(this, R.drawable.jinta));
                            }
                            private class MyView extends View
                            {
                            private final int WIDTH = 20;
                            private final int HEIGHT = 20;

                            private final int COUNT = (WIDTH + 1) * (HEIGHT + 1);

                            private final float verts = new float[COUNT * 2];

                            private final float orig = new float[COUNT * 2];
                            public MyView(Context context, int drawableId)
                            {
                            super(context);
                            setFocusable(true);
                            bitmap = BitmapFactory.decodeResource(getResources()
                            , drawableId);
                            float bitmapWidth = bitmap.getWidth();
                            float bitmapHeight = bitmap.getHeight();
                            int index = 0;
                            for (int y = 0; y <= HEIGHT; y++)
                            {
                            float fy = bitmapHeight * y / HEIGHT;
                            for (int x = 0; x <= WIDTH; x++)
                            {
                            float fx = bitmapWidth * x / WIDTH;

                            orig[index * 2 + 0] = verts[index * 2 + 0] = fx;
                            orig[index * 2 + 1] = verts[index * 2 + 1] = fy;
                            index += 1;
                            }
                            }

                            setBackgroundColor(Color.WHITE);
                            }
                            @Override
                            protected void onDraw(Canvas canvas)
                            {
                            canvas.drawBitmapMesh(bitmap, WIDTH, HEIGHT, verts
                            , 0, null, 0,null);
                            }

                            private void warp(float cx, float cy)
                            {
                            for (int i = 0; i < COUNT * 2; i += 2)
                            {
                            float dx = cx - orig[i + 0];
                            float dy = cy - orig[i + 1];
                            float dd = dx * dx + dy * dy;
                            float d = (float) Math.sqrt(dd);

                            float pull = 100000 / ((float) (dd * d));

                            if (pull >= 1)
                            {
                            verts[i + 0] = cx;
                            verts[i + 1] = cy;
                            }
                            else
                            {

                            verts[i + 0] = orig[i + 0] + dx * pull;
                            verts[i + 1] = orig[i + 1] + dy * pull;
                            }
                            }
                            invalidate();
                            }
                            @Override
                            public boolean onTouchEvent(MotionEvent event)
                            {
                            warp(event.getX(), event.getY());
                            return true;
                            }
                            }
                            }






                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Dec 30 '18 at 5:57

























                            answered Dec 30 '18 at 4:34









                            TienanhTienanh

                            63




                            63






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f4609825%2fhow-drawbitmapmesh-works-in-android-canvas%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                Monofisismo

                                Angular Downloading a file using contenturl with Basic Authentication

                                Olmecas