单击按钮时以一定角度旋转

时间:2015-04-09 09:24:45

标签: android rotation textview

我正在经历一个问题,即使我搜索了很多博客和教程,但没有什么对我有用。

enter image description here

我有上面的视图,其中粗体边框形状是没有任何文字的文本视图,我有两个按钮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;

}

但不适合我

2 个答案:

答案 0 :(得分:1)

  1. 在您的活动类中创建一个角度变量。

    private int angle = 0;

  2. 在您的活动中创建方法getAngle:

  3. public int getRotateAngle(){

    angle = angle+10;
    return angle*(-1);
    }
    
    1. on up按钮单击监听器:
    2. upBtn.setOnClickListener(new 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);
          }
      });
      

      它的出局是: enter image description here

答案 1 :(得分:0)

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

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

移动边缘(XY)的坐标是

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

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

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