标签:
在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 动画结束后想运行的功能 } });
标签:
原文地址:http://www.cnblogs.com/1426837364qqcom/p/5150709.html