您的位置:首页 > 移动开发 > Android开发

Android开发之Tween(补间动画)

2017-09-28 16:03 435 查看
Tween动画的分类

首先来了解下补间动画的分类,在android中补间动画可以分为四类:alpha(渐变)、scale(缩放)、translate(位移)、rotate(旋转)。在这四种动画里每种动画拥有它的独有的属性的同时又拥有相同的属性,其中

alpha:渐变透明度动画效果

scale:渐变缩放动画效果

translate:渐变位置移动动画效果

rotate:渐变旋转动画效果,这四种动画能够分别带来不同的效果体验,又能混合在一起完成酷炫的动画效果。

接下来我们就逐一详细的来学习这四种Tween动画

首先来说这几个的共性吧

从Animation类继承的属性

android:duration:动画执行的时间,以毫秒为单位

android:fillEnabled:true|false,true:动画结束时还原到开始动画前的状态

android:fillBefore:如果fillEnabled的值为true,它的值才有意义,否则没有意义默认值是true,视图会停留在动画开始的状态

android:fillAfter:设置的是在这个动画结束后是否保留这个动画的最后一帧的效果填充后面的动画,它的设置不受fillEnabled的影响

android:repeatMode:reverse|restart,重复类型,reverse:表示倒序回访,restart:表示重新放一遍,这个属性必须与repeatCount联合使用,因为它的前提是重复,即重复播放时的播放类型。

android:repeatCount:动画重复的次数(注意是重复的次数),可以是你想循环播放的次数,也是可以是infinite:表示无限循环

android:interpolator:设定的插值器,它主要用来为动画设置一些特殊的效果,比方说:加速运动、减速运动、动画结束的时候弹 起等等

<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<Button
android:id="@+id/alpha"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="淡入淡出视线动画效果"
android:layout_alignParentBottom="true"
/>
<Button
android:id="@+id/scale"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text=" 比例动画效果"
android:layout_above="@+id/alpha"
/>
<Button
android:id="@+id/translate"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="移动动画效果"
android:layout_above="@+id/scale"
/>
<Button
android:id="@+id/rotate"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="旋转动画效果"
android:layout_above="@+id/translate"
/>
<ImageView
android:id="@+id/image
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="@drawable/ic_launcher"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
/>

</RelativeLayout>


package com.example.newtween;

import android.app.Activity;
import android.os.Bundle;
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;

public class MainActivity extends Activity {
private Button buttonalpha;
private Button buttonscale;
private Button buttontranslate;
private Button buttonrotate;
private ImageView imageview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonalpha=(Button) findViewById(R.id.alpha);
buttonscale=(Button) findViewById(R.id.scale);
buttontranslate=(Button) findViewById(R.id.translate);
buttonrotate=(Button) findViewById(R.id.rotate);
//buttonalpha.setOnClickListener(new AlphaButtonListener());
imageview=(ImageView) findViewById(R.id.image);
buttonalpha.setOnClickListener(new AlphaButtonListener());
buttonscale.setOnClickListener(new ScaleButtonLiatener());
buttontranslate.setOnClickListener(new translateButtonLiatener());
buttonrotate.setOnClickListener(new rotateButtonLiatener());
}
private class AlphaButtonListener implements OnClickListener{

@Override
public void onClick(View v) {
//1.创建一个AnimationSet对象
AnimationSet animationset=new AnimationSet(true);
//2.创建Animation对象
AlphaAnimation alphaAnimation=new AlphaAnimation(1.0f,0f);

4000
//3.设置相应的属相
alphaAnimation.setDuration(1000);
//为Animationset添加Animation
animationset.addAnimation(alphaAnimation);
//为缪怡对象添加Animation
imageview.startAnimation(animationset);

}

}

private class ScaleButtonLiatener implements OnClickListener{

@Override
public void onClick(View v) {
//1.创建一个AnimationSet对象
AnimationSet animationset=new AnimationSet(true);
//2.创建Animation对象
ScaleAnimation alphaAnimation=new ScaleAnimation(1.0f,2.0f,1.0f,2.0f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
//3.设置相应的属相
alphaAnimation.setDuration(1000);
//为Animationset添加Animation
animationset.addAnimation(alphaAnimation);
//为缪怡对象添加Animation
imageview.startAnimation(animationset);
}

}

private class translateButtonLiatener implements OnClickListener{

@Override
public void onClick(View v) {
//1.创建一个AnimationSet对象
AnimationSet animationset=new AnimationSet(true);
//2.创建Animation对象
TranslateAnimation alphaAnimation=new TranslateAnimation(0,100,0,100);
//3.设置相应的属相
alphaAnimation.setDuration(1000);
//为Animationset添加Animation
animationset.addAnimation(alphaAnimation);
//为缪怡对象添加Animation
imageview.startAnimation(animationset);
}

}
private class rotateButtonLiatener implements OnClickListener{

@Override
public void onClick(View v) {
//1.创建一个AnimationSet对象
AnimationSet animationset=new AnimationSet(true);
//2.创建Animation对象
RotateAnimation alphaAnimation=new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
//3.设置相应的属相
alphaAnimation.setDuration(1000);
//为Animationset添加Animation
animationset.addAnimation(alphaAnimation);
//为缪怡对象添加Animation
imageview.startAnimation(animationset);
}

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: