I am trying to rotate an imagebutton to 360 by clicking itself but it doesn’t work
I am using xml file like this..
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="72dp"
android:layout_height="72dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="83dp"
android:layout_marginTop="103dp"
android:clickable="true"
android:src="@drawable/cute" />
</RelativeLayout>
and java code like this…
package com.example.testapplication;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.RotateAnimation;
import android.widget.ImageButton;
public class MainActivity extends Activity implements OnClickListener {
ImageButton img;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img = (ImageButton) findViewById(R.id.imageView1);
}
public void onClick(View v) {
// TODO Auto-generated method stub
RotateAnimation ra =new RotateAnimation(0, 360);
ra.setFillAfter(true);
ra.setDuration(0);
img.startAnimation(ra);
}
}
my aim of application is using 9 img btn develop and puzzle find game….
Your code works just fine. the problem is : you set duration to 0. You can’t see it happen because if you rotate 360 degrees its ending position and starting position will be identical . so set the duration 1000(a second) or more to see the animation actualy happening.
just try:
And you also did not set the onClick listener of img.
just try: