android动画的用法
2016-07-07 17:18
495 查看
帧动画(Frame
Animation)
顺序显示动画文件:animation1.xml<?xml version="1.0" encoding="utf-8"?> <!-- 根标签为animation-list,其中oneshot代表着是否只展示一遍,设置为false会不停的循环播放动画 根标签下,通过item标签对动画中的每一个图片进行声明 android:duration 表示展示所用的该图片的时间长度 --> <animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="true" > <item android:drawable="@drawable/icon1" android:duration="150"></item> <item android:drawable="@drawable/icon2" android:duration="150"></item> <item android:drawable="@drawable/icon3" android:duration="150"></item> <item android:drawable="@drawable/icon4" android:duration="150"></item> <item android:drawable="@drawable/icon5" android:duration="150"></item> <item android:drawable="@drawable/icon6" android:duration="150"></item> </animation-list>
animationIV.setImageResource(R.drawable.animation1);
animationDrawable = (AnimationDrawable) animationIV.getDrawable();
animationDrawable.start(); //启动帧动画
animationDrawable.stop(); //停止帧动画
补间动画(Tween Animation)
补间动画与逐帧动画在本质上是不同的,逐帧动画通过连续播放图片来模拟动画的效果,而补间动画则是通过在两个关键帧之间补充渐变的动画效果来实现的。补间动画的优点是可以节省空间。目前Android应用框架支持的补间动画效果有以下5种。具体实现在android.view.animation类库中。AlphaAnimation:透明度(alpha)渐变效果,对应<alpha/>标签。
TranslateAnimation:位移渐变,需要指定移动点的开始和结束坐标,对应<translate/>标签。
ScaleAnimation:缩放渐变,可以指定缩放的参考点,对应<scale/>标签。
RotateAnimation:旋转渐变,可以指定旋转的参考点,对应<rotate/>标签。
AnimationSet:组合渐变,支持组合多种渐变效果,对应<set/>标签。
补间动画的效果同样可以使用XML语言来定义,这些动画模板文件通常会被放在Android项目的res/anim/目录下。
下面是具体实现源码:
MainActivity.java封装实现的方法:
[java] viewplain copy
package com.example.lesson19_tween;
import com.example.lesson19_tween.R;
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.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
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.imageView1);
}
@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 alphaImpl(View v) {
Animation animation = AnimationUtils.loadAnimation(this,
R.anim.alpha_demo);
imageView.startAnimation(animation);
}
// 旋转动画
public void rotateImpl(View v) {
Animation animation = AnimationUtils.loadAnimation(this,
R.anim.rotate_demo);
imageView.startAnimation(animation);
}
// 缩放动画
public void scaleImpl(View v) {
Animation animation = AnimationUtils.loadAnimation(this,
R.anim.scale_demo);
imageView.startAnimation(animation);
}
// 移动效果
public void translateImpl(View v) {
// XML文件
Animation animation = AnimationUtils.loadAnimation(this,
R.anim.translate_demo);
animation.setRepeatCount(Animation.INFINITE);//循环显示
imageView.startAnimation(animation);
/*
* 第一种 imageView.setAnimation(animation); animation.start();
*/
// 第二种
// Java代码
/*
* TranslateAnimation translateAnimation = new TranslateAnimation(0,
* 200, 0, 0); translateAnimation.setDuration(2000);
* imageView.startAnimation(translateAnimation);
*/
}
// 综合实现set_demo.xml中的动画
public void setAll(View v) {
Animation animation = AnimationUtils.loadAnimation(this,
R.anim.set_demo);
imageView.startAnimation(animation);
}
}
alpha_demo.xml
[html] viewplain copy
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:fromAlpha="1.0"
android:toAlpha="0.1"
android:duration="2000"/>
<!--
fromAlpha :起始透明度
toAlpha:结束透明度
1.0表示完全不透明
0.0表示完全透明
-->
rotate_demo.xml
[html] viewplain copy
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:fromDegrees="0"
android:toDegrees="360"
android:duration="1000"
android:repeatCount="1"
android:repeatMode="reverse"/>
<!--
fromDegrees:表示旋转的起始角度
toDegrees:表示旋转的结束角度
repeatCount:旋转的次数 默认值是0 代表旋转1次 如果值是repeatCount=4 旋转5次,值为-1或者infinite时,表示补间动画永不停止
repeatMode 设置重复的模式。默认是restart。当repeatCount的值大于0或者为infinite时才有效。
repeatCount=-1 或者infinite 循环了
还可以设成reverse,表示偶数次显示动画时会做与动画文件定义的方向相反的方向动行。
-->
scale_demo.xml
[html] viewplain copy
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
android:fromXScale="0.2"
android:toXScale="1.5"
android:fromYScale="0.2"
android:toYScale="1.5"
android:pivotX="50%"
android:pivotY="50%"
android:duration="2000"/>
<!--
fromXScale:表示沿着x轴缩放的起始比例
toXScale:表示沿着x轴缩放的结束比例
fromYScale:表示沿着y轴缩放的起始比例
toYScale:表示沿着y轴缩放的结束比例
图片中心点:
android:pivotX="50%"
android:pivotY="50%"
-->
translate_demo.xml
[html] viewplain copy
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:fromXDelta="0"
android:toXDelta="320"
android:fromYDelta="0"
android:toYDelta="0"
android:duration="2000"/>
<!--
android:interpolator 动画的渲染器
1、accelerate_interpolator(动画加速器) 使动画在开始的时候 最慢,然后逐渐加速
2、decelerate_interpolator(动画减速器)使动画在开始的时候 最快,然后逐渐减速
3、accelerate_decelerate_interpolator(动画加速减速器)
中间位置分层: 使动画在开始的时候 最慢,然后逐渐加速
使动画在开始的时候 最快,然后逐渐减速 结束的位置最慢
fromXDelta 动画起始位置的横坐标
toXDelta 动画起结束位置的横坐标
fromYDelta 动画起始位置的纵坐标
toYDelta 动画结束位置的纵坐标
duration 动画的持续时间
-->
set_demo.xml
[html] viewplain copy
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/decelerate_interpolator"
android:shareInterpolator="true" >
<scale
android:duration="2000"
android:fromXScale="0.2"
android:fromYScale="0.2"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="1.5"
android:toYScale="1.5" />
<rotate
android:duration="1000"
android:fromDegrees="0"
android:repeatCount="1"
android:repeatMode="reverse"
android:toDegrees="360" />
<translate
android:duration="2000"
android:fromXDelta="0"
android:fromYDelta="0"
android:toXDelta="320"
android:toYDelta="0" />
<alpha
android:duration="2000"
android:fromAlpha="1.0"
android:toAlpha="0.1" />
</set>
布局文件:
[html] viewplain copy
<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/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="23dp"
android:layout_marginTop="15dp"
android:onClick="translateImpl"
android:text="@string/text_translate" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/button1"
android:layout_marginLeft="21dp"
android:onClick="rotateImpl"
android:layout_toRightOf="@+id/imageView1"
android:text="@string/text_rotate" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/button1"
android:layout_below="@+id/button1"
android:layout_marginTop="32dp"
android:onClick="scaleImpl"
android:text="@string/text_scale" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button3"
android:layout_alignBottom="@+id/button3"
android:layout_alignLeft="@+id/button2"
android:onClick="alphaImpl"
android:text="@string/text_alpha" />
<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button3"
android:layout_centerHorizontal="true"
android:onClick="setAll"
android:text="@string/text_set" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button5"
android:layout_marginTop="48dp"
android:layout_toRightOf="@+id/button3"
android:src="@drawable/ic_launcher" />
</RelativeLayout>
上面的补间动画是通过xml文件进行动画的设置,下面使用的是代码设置动画
public class Animation1Activity extends Activity
{
private Button rotateButton = null;
private Button scaleButton = null;
private Button alphaButton = null;
private Button translateButton = null;
private ImageView image = null;
@Override
public void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
rotateButton = (Button)findViewById(R.id.rotateButton);
scaleButton = (Button)findViewById(R.id.scaleButton);
alphaButton = (Button)findViewById(R.id.alphaButton);
translateButton = (Button)findViewById(R.id.translateButton);
image = (ImageView)findViewById(R.id.image);
rotateButton.setOnClickListener(newRotateButtonListener());
scaleButton.setOnClickListener(newScaleButtonListener());
alphaButton.setOnClickListener(newAlphaButtonListener());
translateButton.setOnClickListener(
new TranslateButtonListener());
}
class AlphaButtonListener implementsOnClickListener{
public void onClick(View
v) {
//创建一个AnimationSet对象,参数为Boolean型,
//true表示使用Animation的interpolator,false则是使用自己的
AnimationSet animationSet = new AnimationSet(true);
//创建一个AlphaAnimation对象,参数从完全的透明度,到完全的不透明
AlphaAnimation alphaAnimation = new AlphaAnimation(1,
0);
//设置动画执行的时间
alphaAnimation.setDuration(500);
//将alphaAnimation对象添加到AnimationSet当中
animationSet.addAnimation(alphaAnimation);
//使用ImageView的startAnimation方法执行动画
image.startAnimation(animationSet);
}
}
class RotateButtonListener implementsOnClickListener{
public void onClick(View
v) {
AnimationSet animationSet = new AnimationSet(true);
//参数1:从哪个旋转角度开始
//参数2:转到什么角度
//后4个参数用于设置围绕着旋转的圆的圆心在哪里
//参数3:确定x轴坐标的类型,有ABSOLUT绝对坐标、RELATIVE_TO_SELF相对于自身坐标、RELATIVE_TO_PARENT相对于父控件的坐标
//参数4:x轴的值,0.5f表明是以自身这个控件的一半长度为x轴
//参数5:确定y轴坐标的类型
//参数6:y轴的值,0.5f表明是以自身这个控件的一半长度为x轴
RotateAnimation rotateAnimation = new RotateAnimation(0,
360,
Animation.RELATIVE_TO_SELF,0.5f,
Animation.RELATIVE_TO_SELF,0.5f);
rotateAnimation.setDuration(1000);
animationSet.addAnimation(rotateAnimation);
image.startAnimation(animationSet);
}
}
class ScaleButtonListener implementsOnClickListener{
public void onClick(View
v) {
AnimationSet animationSet = new AnimationSet(true);
//参数1:x轴的初始值
//参数2:x轴收缩后的值
//参数3:y轴的初始值
//参数4:y轴收缩后的值
//参数5:确定x轴坐标的类型
//参数6:x轴的值,0.5f表明是以自身这个控件的一半长度为x轴
//参数7:确定y轴坐标的类型
//参数8:y轴的值,0.5f表明是以自身这个控件的一半长度为x轴
ScaleAnimation scaleAnimation = new ScaleAnimation(
0, 0.1f,0,0.1f,
Animation.RELATIVE_TO_SELF,0.5f,
Animation.RELATIVE_TO_SELF,0.5f);
scaleAnimation.setDuration(1000);
animationSet.addAnimation(scaleAnimation);
image.startAnimation(animationSet);
}
}
class TranslateButtonListener implementsOnClickListener{
public void onClick(View
v) {
AnimationSet animationSet = new AnimationSet(true);
//参数1~2:x轴的开始位置
//参数3~4:y轴的开始位置
//参数5~6:x轴的结束位置
//参数7~8:x轴的结束位置
TranslateAnimation translateAnimation =
new TranslateAnimation(
Animation.RELATIVE_TO_SELF,0f,
Animation.RELATIVE_TO_SELF,0.5f,
Animation.RELATIVE_TO_SELF,0f,
Animation.RELATIVE_TO_SELF,0.5f);
translateAnimation.setDuration(1000);
animationSet.addAnimation(translateAnimation);
image.startAnimation(animationSet);
}
}
}
从右往左进入(配置文件)
right_to_left_in.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="500"
android:fromXDelta="100%p"
android:toXDelta="0" />
<alpha
android:duration="500"
android:fromAlpha="1.0"
android:toAlpha="1.0" />
</set>
从左往右离开(配置文件)
left_to_right_out.xml
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="500"
android:fromXDelta="0"
android:toXDelta="100%p" />
<alpha
android:duration="500"
android:fromAlpha="1.0"
android:toAlpha="1.0" />
</set>
//android:fromXDelt代表水平,把x换成Y就是垂直的动画
/ 使用AnimationUtils装载动画配置文件
Animation animation = AnimationUtils.loadAnimation(
Animation1Activity.this,
R.anim.alpha);
// 启动动画
image.startAnimation(animation);
//监听动画的结束
animation .setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
doXXXX();
}
});
}
相关文章推荐
- android客户端性能测试
- Android SDK 在线更新配置(附镜像地址)
- Android Studio常用插件整理
- Android-仿小米巨无霸字体调整控件
- Android支付宝咻咻水波纹效果的实现
- Android颜色渲染:PorterDuff及Xfermode详解
- Android 重新规划项目结构
- Android Studio 更新报错:This version of Android studio is incompatible withe gradle plugin used.
- 我在使用AndroidStudio模仿智慧北京时遇到的问题及解决办法
- Android中三种超实用的滑屏方式汇总(ViewPager、ViewFlipper、ViewFlow)
- Android 复杂的手势处理利用GestureOverView
- ListView里面有EditTextView,解决弹出键盘后EditView不能获取焦点的问题
- ListView里面有EditTextView,解决弹出键盘后EditView不能获取焦点的问题
- Android Studio 开发工具一些入门的小设置
- Android AdapterViewFlipper控件的使用
- Android获取Manifest中<meta-data>元素的值
- Android平台下使用OpenCV灰度化图片的两种方式
- Android Studio多渠道打包和代码混淆教程
- Android 自带的一些系统查询用URI
- Android短信Mms接受流程