在res下创建一个目录 anim目录
public class MainActivity extends Activity { private ImageView iv; @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) { Toast.makeText(getApplicationContext(), "哈哈 你点不到我", 1).show(); } }); } //点击按钮 实现iv 透明的效果 动画 public void click1(View v) { Animation aa = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.alpha); //iv开始执行动画 iv.startAnimation(aa); } //点击按钮 实现iv 执行一个旋转 动画 public void click2(View v) { Animation ra = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.rotate); //iv开始执行动画 iv.startAnimation(ra); } //点击按钮进行一个缩放动画 public void click3(View v) { Animation sa= AnimationUtils.loadAnimation(getApplicationContext(), R.anim.scale); //iv开始执行动画 iv.startAnimation(sa); } //位移动画 public void click4(View v){ Animation ta = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.translate); //开始动画 iv.startAnimation(ta); } //动画一起飞 public void click5(View v){ Animation set = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.set); //最后一步 要记得 执行动画 iv.startAnimation(set); } }
alpha.xml
<?xml version="1.0" encoding="utf-8"?> <alpha android:fromAlpha="1.0" android:toAlpha="0.0" android:duration="2000" android:repeatMode="reverse" android:repeatCount="1" xmlns:android="http://schemas.android.com/apk/res/android"> </alpha>
rotate.xml
<?xml version="1.0" encoding="utf-8"?> <rotate android:fromDegrees="0" android:toDegrees="360" android:pivotX="50%" android:pivotY="50%" android:repeatCount="1" android:repeatMode="reverse" android:duration="2000" xmlns:android="http://schemas.android.com/apk/res/android"> </rotate>
scale.xml
<?xml version="1.0" encoding="utf-8"?> <scale android:fromXScale="1.0" android:toXScale="2.0" android:fromYScale="1.0" android:toYScale="2.0" android:pivotX="50%" android:pivotY="50%" android:repeatMode="reverse" android:repeatCount="1" android:duration="2000" xmlns:android="http://schemas.android.com/apk/res/android"> </scale>
translate.xml
<?xml version="1.0" encoding="utf-8"?> <translate android:fromXDelta="0%p" android:toXDelta="0%p" android:fromYDelta="0%p" android:toYDelta="20%p" android:fillAfter="true" android:duration="2000" xmlns:android="http://schemas.android.com/apk/res/android"> </translate>
set.xml
<?xml version="1.0" encoding="utf-8"?> <set> <alpha xmlns:android="http://schemas.android.com/apk/res/android" android:duration="2000" android:fromAlpha="1.0" android:repeatCount="1" android:repeatMode="reverse" android:toAlpha="0.0" > </alpha> <rotate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="2000" android:fromDegrees="0" android:pivotX="50%" android:pivotY="50%" android:repeatCount="1" android:repeatMode="reverse" android:toDegrees="360" > </rotate> <scale xmlns:android="http://schemas.android.com/apk/res/android" android:duration="2000" android:fromXScale="1.0" android:fromYScale="1.0" android:pivotX="50%" android:pivotY="50%" android:repeatCount="1" android:repeatMode="reverse" android:toXScale="2.0" android:toYScale="2.0" > </scale> <translate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="2000" android:fillAfter="true" android:fromXDelta="0%p" android:fromYDelta="0%p" android:toXDelta="0%p" android:toYDelta="20%p" > </translate> </set>