Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To Draw A Flower In Android Each Petal By Petal

Tags:

java

android

In android I would like to draw flower by adding each petal from a center point. I used setRotation on the image view but the center point is different for each petal. (I mean the center point from the flower) Can anybody look at my code and suggest me correction? Thanks.

   int angle=0;
   int ypos=500;
   int xpos=500;

   RelativeLayout layout = (RelativeLayout)findViewById(R.id.ln1);

   for(int i=0;i<10;i++)
   {

      ImageView image = new ImageView(this);
      image.setLayoutParams(new android.view.ViewGroup.LayoutParams(150,400));
      image.setX(xpos);
      image.setY(ypos);
      image.setPadding(-7,-30,-10,0);
      image.setPivotX(1.0f);
      image.setScaleX(1.5f);
      image.setScaleY(1.5f);

      image.setImageResource(R.drawable.petal);
      image.setRotation(image.getRotation() + angle);
      angle=angle+36;
      layout.addView(image);
   }

Image I get is this

enter image description here

like image 440
MACMAN Avatar asked Apr 12 '18 11:04

MACMAN


1 Answers

When you rotate the image, the rotation is done with the top left corner of the image, not the center of the rotated image.

Below image might illustrate this. The black square represents your image. The lefthand site shows the situation you have now. The righthand side shows the situation you want.

enter image description here

Before rotating, you should subtract half the width from the x position, and add half the height from the y position. Then you should get the desired image.

As user Ralf Renz pointed out in their comment, you could also simply start with an angle of -36. This is a useful workaround.

like image 131
S.L. Barth Avatar answered Nov 09 '22 23:11

S.L. Barth