DJhon
DJhon

Reputation: 1518

Rotate with some angle when click on button

I am going through one problem even i searched lot of blogs and tutorial but nothing is working for me.

enter image description here

I have this above view where bold border shape is text-view without any text and i have two button UP and DOWN(not shown in image). Now i want to move this bold border view with some angle. Suppose i click UP button then it should be move with angle 10 degree UP but other end should remain constant. Some thing like hands of clock. One end XY coordinate constant and other end XY coordinate will change with some angle every time when we click on UP button. I have tried this

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

and

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;

}

But not working for me

Upvotes: 1

Views: 364

Answers (2)

Yogendra
Yogendra

Reputation: 5258

  1. Create a angle variable in your activity class.

    private int angle = 0;

  2. create a method getAngle in your activity:

public int getRotateAngle(){

angle = angle+10;
return angle*(-1);
}
  1. on up button click listener :

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

its out put is : enter image description here

Upvotes: 1

Javier Herrero
Javier Herrero

Reputation: 1

If what you want to know are the new coordinates of the moving edge, it is as simple as this:

given the constant length of the shape L (in pixels), a rotation D (in degrees, counterclockwise and 0 in the horizontal position of your draw), and the coordinates of the fixed edge (X0,Y0),

the coordinates of the moving edge (XY) are

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

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

(D*PI/180) is to express the angle in radians

Upvotes: 0

Related Questions