package com.example.test;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
public class MainActivity extends Activity {
private ImageView iv = null;
private boolean show = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv = (ImageView) findViewById(R.id.iv);
iv.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Log.d("huang", "show===" + show);
if (!show) {
startAnimationsIn(iv, 500);
} else {
startAnimationsOut(iv, 500, 0);
}
show = !show;//交替替换上面的动作
}
});
}
protected void startAnimationsIn(ImageView iv2, int i) {
iv2.setVisibility(0);
iv2.setClickable(true);
iv2.setFocusable(true);
Animation animation;
animation = new RotateAnimation(-180, 0, Animation.RELATIVE_TO_SELF,
0.5f, Animation.RELATIVE_TO_SELF, 1.0f);
animation.setFillAfter(true);
animation.setDuration(i);
iv2.startAnimation(animation);
}
protected void startAnimationsOut(final ImageView iv2, int i, int j) {
Animation animation;
animation = new RotateAnimation(0, -180, Animation.RELATIVE_TO_SELF,
0.5f, Animation.RELATIVE_TO_SELF, 1.0f);
animation.setFillAfter(true);
animation.setDuration(i);
animation.setStartOffset(j);// startOffset开始动画的时间
animation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation arg0) {
}
@Override
public void onAnimationRepeat(Animation arg0) {
}
@Override
public void onAnimationEnd(Animation arg0) {
iv2.setVisibility(8);
iv2.setVisibility(8);
iv2.setClickable(true);
iv2.setFocusable(true);
}
});
iv2.startAnimation(animation);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
public class MainActivity extends Activity {
private ImageView iv = null;
private boolean show = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv = (ImageView) findViewById(R.id.iv);
iv.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Log.d("huang", "show===" + show);
if (!show) {
startAnimationsIn(iv, 500);
} else {
startAnimationsOut(iv, 500, 0);
}
show = !show;//交替替换上面的动作
}
});
}
protected void startAnimationsIn(ImageView iv2, int i) {
iv2.setVisibility(0);
iv2.setClickable(true);
iv2.setFocusable(true);
Animation animation;
animation = new RotateAnimation(-180, 0, Animation.RELATIVE_TO_SELF,
0.5f, Animation.RELATIVE_TO_SELF, 1.0f);
animation.setFillAfter(true);
animation.setDuration(i);
iv2.startAnimation(animation);
}
protected void startAnimationsOut(final ImageView iv2, int i, int j) {
Animation animation;
animation = new RotateAnimation(0, -180, Animation.RELATIVE_TO_SELF,
0.5f, Animation.RELATIVE_TO_SELF, 1.0f);
animation.setFillAfter(true);
animation.setDuration(i);
animation.setStartOffset(j);// startOffset开始动画的时间
animation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation arg0) {
}
@Override
public void onAnimationRepeat(Animation arg0) {
}
@Override
public void onAnimationEnd(Animation arg0) {
iv2.setVisibility(8);
iv2.setVisibility(8);
iv2.setClickable(true);
iv2.setFocusable(true);
}
});
iv2.startAnimation(animation);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
注意:show的使用,而不是将一个true或者false赋给它。