Java乔晓松-android中的Tween动画的使用

来源:互联网 发布:电脑便签软件 编辑:程序博客网 时间:2024/05/14 05:14

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;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);imageView = (ImageView) findViewById(R.id.iv_01);}@Overridepublic 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);}}

移动效果的代码如下:2000表示2000毫秒移动一次

<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" />

缩放效果的代码如下:50%都表示缩放的比例

<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" />

旋转效果的代码如下:0代表开始旋转的°数,360代表360°
<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>