安卓开发过程中,动画会经常遇到,虽然有时候已经被封装好(如各种开源框架),不需要我们再去处理,但学好动画也是非常重要的,有助于我们实现各种效果,使体验更加友好。安卓动画部分分为:补间动画,帧动画和属性动画,我们也将分三篇来学习,这是第一篇的学习:补间动画的各种效果的实现。不多说了,直接上代码!!!
这里是布局文件,说明一下,补间动画,帧动画和属性动画在一个项目里,所以最下面有三个按钮,进行页面切换。
<RelativeLayout 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" >
<RelativeLayout
android:id="@+id/frame_RelativeLayout"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:orientation="horizontal" >
<Button
android:id="@+id/button_alpha"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="透明动画" />
<Button
android:id="@+id/button_rotate"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="旋转动画" />
<Button
android:id="@+id/button_scale"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="缩放动画" />
<Button
android:id="@+id/button_translate"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="位移动画" />
</LinearLayout>
<ImageView
android:id="@+id/imageView1"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:src="@drawable/ic_launcher" />
<Button
android:id="@+id/button_animationSet"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/linearLayout1"
android:text="动画集合" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true" >
<Button
android:id="@+id/button_tweenAnimation"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="补间动画" />
<Button
android:id="@+id/button_framAnimation"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="帧动画" />
<Button
android:id="@+id/button_propertyAnimation "
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="属性动画" />
</LinearLayout>
</RelativeLayout>
下面是java代码部分:
package com.example.animationexercise;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;
public class TweenAnimationActivity extends Activity implements OnClickListener{
/**
* 本文主要以代码的形式实现动画效果,如果要用XML文件,其中重要的一条语句如下:
* alphaAnimation=AnimationUtils.loadAnimation(this, R.anim.alpha);
* 其中 R.anim.alpha 文件为动画文件。
*
* 如要了解,可搜索:“Android开发之动画效果浅析 ”,写的非常详细
* 网址:http://www.2cto.com/kf/201410/342375.html。
*
* 对TweenAnimation参数进行详细介绍的网址:
* http://www.360doc.com/content/13/0102/22/6541311_257754535.shtml。
* */
//底部按钮
private Button button_tweenAnimation;
private Button button_framAnimation;
private Button button_propertyAnimation;
//补间动画所用控件
//private RelativeLayout frame_RelativeLayout;
private Button button_alpha;
private Button button_rotate;
private Button button_scale;
private Button button_translate;
private Button button_animationSet;
private ImageView mImageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tween_animation_layout);
initView();
}
private void initView() {
//补间动画所用控件
//frame_RelativeLayout = (RelativeLayout) findViewById(R.id.frame_RelativeLayout);
button_alpha = (Button) findViewById(R.id.button_alpha);
button_rotate = (Button) findViewById(R.id.button_rotate);
button_scale = (Button) findViewById(R.id.button_scale);
button_translate = (Button) findViewById(R.id.button_translate);
button_animationSet = (Button) findViewById(R.id.button_animationSet);
mImageView = (ImageView) findViewById(R.id.imageView1);
button_alpha.setOnClickListener(this);
button_rotate.setOnClickListener(this);
button_scale.setOnClickListener(this);
button_translate.setOnClickListener(this);
button_animationSet.setOnClickListener(this);
//底部按钮
button_tweenAnimation = (Button) findViewById(R.id.button_tweenAnimation);
button_framAnimation = (Button) findViewById(R.id.button_framAnimation);
button_propertyAnimation = (Button) findViewById(R.id.button_propertyAnimation);
button_tweenAnimation.setOnClickListener(this);
button_framAnimation.setOnClickListener(this);
button_propertyAnimation.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.button_tweenAnimation://补间动画
break;
case R.id.button_framAnimation://帧动画
startActivity(new Intent(this,FramAnimationActivity.class));
break;
case R.id.button_propertyAnimation://属性动画
startActivity(new Intent(this,PropertyAnimationActivity.class));
break;
case R.id.button_alpha://透明度动画
mImageView.startAnimation(getAlphaAnimation());
break;
case R.id.button_scale://缩放动画
mImageView.startAnimation(getScaleAnimation());
break;
case R.id.button_rotate://旋转动画
mImageView.startAnimation(getRotateAnimation());
break;
case R.id.button_translate://位移动画
// TranslateAnimation translate = new TranslateAnimation(
// fromXType, fromXValue, toXType, toXValue,
// fromYType, fromYValue, toYType, toYValue);
mImageView.startAnimation(getTranslateAnimation());
break;
case R.id.button_animationSet://动画集
//动画集合,true:使用Animation的Animation; false:使用自己的
AnimationSet set = new AnimationSet(false);
set.addAnimation(getAlphaAnimation());
set.addAnimation(getScaleAnimation());
set.addAnimation(getRotateAnimation());
set.addAnimation(getTranslateAnimation());
set.setFillAfter(true);
mImageView.startAnimation(set);
break;
default:
break;
}
}
/**
* 透明度动画
* */
private AlphaAnimation getAlphaAnimation(){
AlphaAnimation alpha = new AlphaAnimation(0, 1);//透明度从0到1
alpha.setDuration(5000);//时间5秒
alpha.setFillAfter(true);//保持结束之后的状态
return alpha;
}
/**
* 缩放动画
* */
private ScaleAnimation getScaleAnimation(){
ScaleAnimation scale = new ScaleAnimation(0, 2, 0, 2, //放大倍数0到2
Animation.RELATIVE_TO_SELF,0.5f,
Animation.RELATIVE_TO_SELF,0.5f);//以自己中心为旋转中心
scale.setDuration(5000);//时间5秒
scale.setFillAfter(true);//保持结束之后的状态
return scale;
}
/**
* 旋转动画
* */
private RotateAnimation getRotateAnimation(){
RotateAnimation rotate = new RotateAnimation(0, 360,
Animation.RELATIVE_TO_SELF,0.5f,
Animation.RELATIVE_TO_SELF,0.5f);
rotate.setDuration(5000);//时间5秒
rotate.setFillAfter(true);//保持结束之后的状态
return rotate;
}
/**
* 位移动画
* */
private TranslateAnimation getTranslateAnimation(){
TranslateAnimation translate = new TranslateAnimation(-200, 200, 0, 0);
//translate.setInterpolator(this, android.R.anim.accelerate_interpolator);//加速度插值器
translate.setDuration(5000);//时间5秒
translate.setFillAfter(true);//保持结束之后的状态
return translate;
}
}
最后一个动画集是后加上的,觉得有必要学习下,因为实际开发中,我们经常要使多种动画一起运行。此外,这里没有列举用XML文件的方式实现动画的方法,其实也蛮简单,这里就不写了!
注释里面提供了两个网址,有兴趣的同学可以看看,讲解的非常详细!