补间动画
在xml文件下创建图片imageview
在res下创建文件夹选Tween animation类型文件夹名称anim以下文件
透明动画
<?xml version="1.0" encoding="utf-8"?> <alpha xmlns:android="http://schemas.android.com/apk/res/android" android:fromAlpha="1.0" android:toAlpha="0.5" android:duration="2000" android:repeatCount="3" android:repeatMode="restart" android:fillAfter="true" > </alpha>
放缩动画
<?xml version="1.0" encoding="utf-8"?> <scale xmlns:android="http://schemas.android.com/apk/res/android" android:fromXScale="0" android:toXScale="2.0" android:fromYScale="0" android:toYScale="2.0" android:pivotX="50%" android:pivotY="50%" android:repeatCount="3" android:duration="2000" > </scale>
位移动画
<?xml version="1.0" encoding="utf-8"?> <translate xmlns:android="http://schemas.android.com/apk/res/android" android:fromXDelta="0" android:fromYDelta="0" android:repeatCount="2" android:duration="2000" android:toXDelta="200" android:toYDelta="0" > </translate>
旋转动画
<?xml version="1.0" encoding="utf-8"?> <rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="0"
android:toDegrees="-361" android:pivotX="0%" android:pivotY="0%" android:duration="2000" android:repeatCount="5"> </rotate>
动画集合,所用动画效果同时进行
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" > <alpha android:duration="2000" android:fillAfter="true" android:fromAlpha="1.0" android:repeatCount="2" android:repeatMode="restart" android:toAlpha="0.5" /> <rotate android:fromDegrees="0" android:toDegrees="-361" android:pivotX="50%" android:pivotY="50%" android:duration="2000" android:repeatCount="2" /> <scale android:fromXScale="0" android:toXScale="1.0" android:fromYScale="0" android:toYScale="1.0" android:pivotX="50%" android:pivotY="50%" android:repeatCount="2" android:duration="2000" /> <translate android:fromXDelta="0" android:fromYDelta="0" android:repeatCount="2" android:duration="2000" android:toXDelta="80" android:toYDelta="0" /> </set>
MainActivity
package com.bwei.day_06; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.view.animation.AlphaAnimation; import android.view.animation.AnimationSet; import android.view.animation.AnimationUtils; import android.view.animation.RotateAnimation; import android.view.animation.ScaleAnimation; import android.view.animation.TranslateAnimation; import android.widget.ImageView; public class MainActivity extends Activity { private ImageView imageView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); imageView = (ImageView) findViewById(R.id.imageView); } /** * 透明动画 * * @param v */ public void alpha(View v) { // 获得AlphaAnimation对象 AlphaAnimation alphaAnimation = (AlphaAnimation) AnimationUtils .loadAnimation(MainActivity.this, R.anim.alpha_anim); // 开启动画 imageView.startAnimation(alphaAnimation); } /** * 放缩动画 * @param v */ public void scale(View v) { // 获得ScaleAnimation对象 ScaleAnimation scaleAnimation = (ScaleAnimation) AnimationUtils .loadAnimation(MainActivity.this, R.anim.scale_anim); // 开启动画 imageView.startAnimation(scaleAnimation); } /** * 位移动画 * @param v */ public void translate(View v) { // 获得TranslateAnimation对象 TranslateAnimation translateAnimation = (TranslateAnimation) AnimationUtils .loadAnimation(MainActivity.this, R.anim.translate_anim); // 开启动画 imageView.startAnimation(translateAnimation); } /** * 旋转动画 * @param v */ public void rotate(View v) { // 获得RotateAnimation对象 RotateAnimation rotateAnimation = (RotateAnimation) AnimationUtils .loadAnimation(MainActivity.this, R.anim.rotate_anim); // 开启动画 imageView.startAnimation(rotateAnimation); } /** * 动画集合,所用动画效果同时进行 * @param v */ public void set(View v) { // 获得AnimationSet对象 AnimationSet animationSet = (AnimationSet) AnimationUtils .loadAnimation(MainActivity.this, R.anim.set_anim); // 开启动画 imageView.startAnimation(animationSet); } }
动画结束后监听
translateAnimation.setAnimationListener(new AnimationListener() { @Override public void onAnimationStart(Animation animation) { // TODO Auto-generated method stub } @Override public void onAnimationRepeat(Animation animation) { // TODO Auto-generated method stub } @Override public void onAnimationEnd(Animation animation) { // TODO Auto-generated method stub 动画结束后想运行的功能 } });
帧动画
在xml文件下创建图片imageview
在res下创建文件夹选drawable类型文件夹名称animation.xml以下文件
<?xml version="1.0" encoding="utf-8"?> <animation-list xmlns:android="http://schemas.android.com/apk/res/android" > <item android:drawable="@drawable/a1" android:duration="200"/> <item android:drawable="@drawable/a2" android:duration="200"/> <item android:drawable="@drawable/a3" android:duration="200"/> <item android:drawable="@drawable/a4" android:duration="200"/> <item android:drawable="@drawable/a5" android:duration="200"/> <item android:drawable="@drawable/a6" android:duration="200"/> <item android:drawable="@drawable/a7" android:duration="200"/> <item android:drawable="@drawable/a8" android:duration="200"/> </animation-list>
MainActivity
package com.bawei.d5_2; import android.os.Bundle; import android.app.Activity; import android.graphics.drawable.AnimationDrawable; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageView; public class MainActivity extends Activity { private AnimationDrawable animationDrawable; private Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = (Button) findViewById(R.id.button);
ImageView imageView=(ImageView) findViewById(R.id.imageview); imageView.setBackgroundResource(R.drawable.animation); animationDrawable = (AnimationDrawable) imageView.getBackground(); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub String str=button.getText().toString(); if(str.equals("开始")){ animationDrawable.start(); button.setText("暂停"); } if(str.equals("暂停")){ animationDrawable.stop(); button.setText("继续"); } if(str.equals("继续")){ animationDrawable.start(); button.setText("暂停"); } } }); } }