前面我已经说了变换动画,并且变换动画中分为4种情况:透明度动画、旋转动画、缩放动画、位移动画。
今天我来说说关于使用变换动画中的4种类型来实现它们的糅合。
我在这里主要使用了一个Animation对象中的一个监听方法--setAnimationListener。这个方法里面只有一个参数,安卓api给出的这个方法的完整形态。
void android.view.animation.Animation.setAnimationListener(AnimationListener listener)。从中我们可以知道,这个参数需要传递一个AnimationListener 类型的参数,我们只需要使用匿名内部类来即可(这里我的要求简单,所以使用的是匿名内部类)。当我们使用了匿名内部类后,会出现三个方法。代码如下:
animation1.setAnimationListener(new AnimationListener() {
//当动画开始时调用
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
//当动画重复时调用
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
//当动画开始时调用
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
imageview.startAnimation(animation2);
}
});
其中方法animation1和animation2都是Animation对象。
看到这里我们应该怎么实现了吧,话不多说,直接贴完整的代码
1.alpha.xml代码(animation1加载的)
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:duration="1000"
android:fromAlpha="0.1"
android:toAlpha="1.0"
/>
</set>
2.translate.xml代码
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="1000"
android:fromXDelta="10"
android:fromYDelta="10"
android:toXDelta="100"
android:toYDelta="100" />
</set>
3. 布局文件代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.Tween_Animation.Alpha_MainActivity" >
<Button
android:id="@+id/button_scale"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/button_stringScaleAnimation" />
<LinearLayout
android:gravity="center"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageview_scale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
</LinearLayout>
</LinearLayout>
4. activity代码
package com.example.Demo1;
import com.example.androidanimation.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
/*
* 组合动画
* 先播放一个动画然后再播放一个动画
*/
public class MainActivity extends Activity implements OnClickListener{
private Button button = null;
private ImageView imageview = null;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button_scale);
imageview = (ImageView) findViewById(R.id.imageview_scale);
button.setText("组合动画1");
button.setOnClickListener(this);
}
public void onClick(View v) {
Animation animation1 = AnimationUtils.loadAnimation(this, R.anim.alpha);
imageview.startAnimation(animation1);
final Animation animation2 = AnimationUtils.loadAnimation(this, R.anim.translate);
animation1.setAnimationListener(new AnimationListener() {
//当动画开始时调用
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
//当动画重复时调用
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
//当动画开始时调用
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
imageview.startAnimation(animation2);
}
});
}
}
这里只是实现了两种类型的动画来完成效果,其实我们可以多种多样,看自己的需要了。