单击按钮时旋转一定角度

Rotate with some angle when click on button

即使我搜索了很多博客和教程,我也遇到了一个问题,但没有任何效果。

我有上面的视图,其中粗体边框形状是没有任何文本的文本视图,并且我有两个按钮 UP 和 DOWN(图中未显示)。现在我想以某个角度移动这个粗体边框视图。假设我点击向上按钮然后它应该以 10 度向上的角度移动但另一端应该保持不变。有些东西像时钟的指针。每次点击 UP 按钮时,一端的 XY 坐标常量和另一端的 XY 坐标会发生一定角度的变化。我试过这个

 float x1=txtv_legside.getLeft();  
 float y1=txtv_legside.getTop();
 float x2=txtv_legside.getRight();
 float y2=txtv_legside.getBottom();
            System.out.println("starting co-ordinates  ("+x1+","+y1+")");
            System.out.println("end  co-ordinates  ("+x2+","+y2+")");

float lenght =   lengthOfLine(x1, y1, x2, y2);
txtv_legside.setRight((int) (x1+lenght* Math.cos(10*3.14/180)));
txtv_legside.setBottom((int) (y1+lenght* Math.sin(40*3.14/180)));

public float lengthOfLine(float x1, float y1, float x2 , float y2){
float length = (float) Math.sqrt(((x2-x1)*(x2-x1))+((y2-y1)*(y2-y1)));
System.out.println("length of line "+length);
return length;

}

但对我不起作用

如果你想知道的是移动边的新坐标,就这么简单:

给定形状 L 的恒定长度(以像素为单位)、旋转 D(以度为单位,逆时针方向,绘制的水平位置为 0)和固定边的坐标 (X0,Y0),

移动边(XY)坐标为

X=X0+L* cos(D*PI/180)

Y=Y0+L* sin(D*PI/180)

(D*PI/180)是用弧度表示角度

  1. 在 activity class 中创建一个角度变量。

    私有 int 角度 = 0;

  2. 在您的 activity 中创建一个方法 getAngle:

public int getRotateAngle(){

angle = angle+10;
return angle*(-1);
}
  1. 向上按钮点击侦听器:

upBtn.setOnClickListener(新的 OnClickListener() {

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
         Animation an = new RotateAnimation(angle*(-1), getRotateAngle(), 0, 25);
            an.setDuration(1000);               // duration in ms
            an.setRepeatCount(0);                // -1 = infinite repeated
            an.setRepeatMode(Animation.REVERSE); // reverses each repeat
            an.setFillAfter(true);
            an.setFillEnabled(true);
            tvView.startAnimation(an);
    }
});

它的输出是: