Tween动画的使用,我试过的有移动效果、旋转效果、透明效果和缩放效果以及混合动画
下面我给大家完整的代码和详细的解释:
MainActivity.java源码:
package com.example.lesson19_tween; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.View; import android.view.animation.Animation; import android.view.animation.AnimationUtils; import android.widget.ImageView; /** * 2013-6-28 上午9:09:49 * * @author 乔晓松 */ 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.iv_01); } @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; } public void translateImpl(View v) { // xml文件的Tween动画 移动的效果 Animation animation = AnimationUtils.loadAnimation(this, R.anim.translate); // 循环动画,动画一直循环的移动 animation.setRepeatCount(Animation.INFINITE); // imageView.setAnimation(animation); // animation.start(); imageView.startAnimation(animation); // Java代码实现的Tween动画效果 // TranslateAnimation translateAnimation = new TranslateAnimation(0, // 320, // 0, 0); // translateAnimation.setDuration(2000); // imageView.startAnimation(translateAnimation); } // 缩放动画 public void scaleImpl(View v) { Animation animation = AnimationUtils.loadAnimation(this, R.anim.scale); imageView.startAnimation(animation); } // 旋转动画 public void rotateImpl(View v) { Animation animation = AnimationUtils.loadAnimation(this, R.anim.rotate); imageView.startAnimation(animation); } // 透明动画 public void alphaImpl(View v) { Animation animation = AnimationUtils.loadAnimation(this, R.anim.alpha); imageView.startAnimation(animation); } public void setImpl(View v) { Animation animation = AnimationUtils.loadAnimation(this, R.anim.set); imageView.startAnimation(animation); } }
<translate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="2000" android:fromXDelta="0" android:fromYDelta="0" android:interpolator="@android:anim/accelerate_decelerate_interpolator" android:toXDelta="320" android:toYDelta="0" />
<scale xmlns:android="http://schemas.android.com/apk/res/android" android:fromXScale="0.2" android:fromYScale="0.2" android:interpolator="@android:anim/accelerate_decelerate_interpolator" android:pivotX="50%" android:pivotY="50%" android:toXScale="1.0" android:toYScale="1.0" android:duration="2000" />
<rotate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="2000" android:fromDegrees="0" android:pivotY="1" android:repeatCount="infinite" android:repeatMode="reverse" android:toDegrees="360" />透明效果的代码如下:代码中的1代表开始不透明,0.1是最后的透明效果是0.1接近完全透明
<alpha xmlns:android="http://schemas.android.com/apk/res/android" android:duration="2000" android:fromAlpha="1" android:interpolator="@android:anim/accelerate_decelerate_interpolator" android:toAlpha="0.1" />所谓的混合效果,就是上面的几种效果叠加的效果,用set集合就可以实现,代码如下:
<set xmlns:android="http://schemas.android.com/apk/res/android" android:shareInterpolator="false" > <scale android:duration="700" android:fillAfter="false" android:fromXScale="1.0" android:fromYScale="1.0" android:interpolator="@android:anim/accelerate_decelerate_interpolator" android:pivotX="50%" android:pivotY="50%" android:toXScale="1.4" android:toYScale="0.6" /> <alpha android:duration="2000" android:fromAlpha="1" android:interpolator="@android:anim/accelerate_decelerate_interpolator" android:toAlpha="0.1" /> <set android:interpolator="@android:anim/decelerate_interpolator" > <scale android:duration="400" android:fillBefore="false" android:fromXScale="1.4" android:fromYScale="0.6" android:pivotX="50%" android:pivotY="50%" android:startOffset="700" android:toXScale="0.0" android:toYScale="0.0" /> <rotate android:duration="400" android:fromDegrees="0" android:pivotX="50%" android:pivotY="50%" android:startOffset="700" android:toDegrees="-45" android:toYScale="0.0" /> </set> </set>