I have been created the animation arrow. When I press the button then it rotate -45 degree and then goes back arrow.
My Question is: I want to stop the arrow at -45 degree. When I click the button again then it should move the arrow -45 to -90 and so on.
Please help me regarding this problem. thanks
I am placing code here.
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<rotate
android:fromDegrees="0"
android:toDegrees="-45"
android:toYScale="0.0"
android:pivotX="50%"
android:pivotY="50%"
android:duration="400"
/>
</set>
hyperspace_jump.xml
package kh.qasim;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
public class CustomAnimationActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ImageView image = (ImageView) findViewById(R.id.imageView1);
final Animation hyperspaceJump = AnimationUtils.loadAnimation(this, R.anim.hyperspace_jump);
Button btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
image.startAnimation(hyperspaceJump);
}
});
}
}
CustomAnimationActivity.java
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button android:text="Button" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<ImageView android:id="@+id/imageView1" android:src="@drawable/up" android:layout_width="203dp" android:layout_height="148dp"></ImageView>
</LinearLayout>
First, if you want to prevent you image to revert to previous state afterb the animation, you need to use :
Also, in order to be able to change orientation dynamically, you should create your animation programmatically, instead of doing it from an XML file :
Use fromDegress and toDegrees variable to control the start and end orientation of your object. For instance, you could put save the current orientation in a variable that you would call currentOrientation. So, you just need to call :
Then start the animation like you did before.
At the end of the animation, just store the new orientation in currentOrientation.