如何在Android的每个花瓣中画一朵花

时间:2018-04-12 11:17:49

标签: java android

在android中我想通过从中心点添加每个花瓣来绘制花朵。我在图像视图上使用了setRotation,但每个花瓣的中心点都不同。 (我的意思是花的中心点)任何人都可以查看我的代码并建议我更正吗?感谢。

   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);
   }

我得到的图像是

enter image description here

2 个答案:

答案 0 :(得分:4)

旋转图像时,旋转是在图像的左上角完成的,而不是旋转图像的中心。

下图可能会说明这一点。黑色方块代表您的形象。左手网站显示了您现在的情况。右侧显示了您想要的情况。

enter image description here

在旋转之前,你应该从x位置减去一半的宽度,并从y位置加上一半的高度。然后你应该得到想要的图像。

正如用户Ralf Renz在他们的comment中指出的那样,你也可以简单地以-36的角度开始。这是一个有用的解决方法。

答案 1 :(得分:1)

我使用以下策略完成了此操作。

image.setPivotY(-1);

现在花朵的设计不同,因为花瓣被倾斜了。所以我使用inkscape将我的实际图像倾斜到相反方向,现在我得到了所需的输出。

enter image description here