Animations

一、Animations介绍

Animations是一个实现android UI界面动画效果的API,Animations提供了一系列的动画效果,可以进行旋转、缩放、淡入淡出等,这些效果可以应用在绝大多数的控件中。

二、Animations的分类

Animations从总体上可以分为两大类:

1.Tweened Animations:该类Animations提供了旋转、移动、伸展和淡出等效果。Alpha——淡入淡出,Scale——缩放效果,Rotate——旋转,Translate——移动效果。

2.Frame-by-frame Animations:这一类Animations可以创建一个Drawable序列,这些Drawable可以按照指定的时间间歇一个一个的显示。

三、Animations的使用方法(代码中使用)

Animations extends Object implements Cloneable

使用TweenedAnimations的步骤:

1.创建一个AnimationSet对象(Animation子类);

2.增加需要创建相应的Animation对象;

3.更加项目的需求,为Animation对象设置相应的数据;

4.将Animatin对象添加到AnimationSet对象当中;

5.使用控件对象开始执行AnimationSet。

  Tweened Animations的分类
  1、Alpha:淡入淡出效果
  2、Scale:缩放效果
  3、Rotate:旋转效果
  4、Translate:移动效果

Animation的四个子类:
  AlphaAnimation、TranslateAnimation、ScaleAnimation、RotateAnimation

Tween Animations的通用方法

  1、setDuration(long durationMills)
  设置动画持续时间(单位:毫秒)
  2、setFillAfter(Boolean fillAfter)
  如果fillAfter的值为true,则动画执行后,控件将停留在执行结束的状态
  3、setFillBefore(Boolean fillBefore)
  如果fillBefore的值为true,则动画执行后,控件将回到动画执行之前的状态
  4、setStartOffSet(long startOffSet)
  设置动画执行之前的等待时间
  5、setRepeatCount(int repeatCount)
  设置动画重复执行的次数

AnimationSet的具体使用方法

1.AnimationSet是Animation的子类;

2.一个AnimationSet包含了一系列的Animation;

3.针对AnimationSet设置一些Animation的常见属性(如startOffset,duration等),可以被包含在AnimationSet当中的Animation集成;

Interpolator的具体使用方法

Interpolator定义了动画变化的速率,在Animations框架当中定义了一下几种Interpolator

   AccelerateDecelerateInterpolator:在动画开始与结束的地方速率改变比较慢,在中间的时候速率快。

   AccelerateInterpolator:在动画开始的地方速率改变比较慢,然后开始加速

   CycleInterpolator:动画循环播放特定的次数,速率改变沿着正弦曲线

   DecelerateInterpolator:在动画开始的地方速率改变比较慢,然后开始减速

  LinearInterpolator:动画以均匀的速率改变

分为以下几种情况:

1、在set标签中

<set xmlns:android="http://schemas.android.com/apk/res/android"

android:interpolator="@android:anim/accelerate_interpolator"/>

2、如果在一个set标签中包含多个动画效果,如果想让这些动画效果共享一个Interpolator。

android:shareInterpolator="true"

3、如果不想共享一个interpolator,则设置android:shareInterpolator="true"并且需要在每一个动画效果处添加interpolator。

<alpha

android:interpolator="@android:anim/accelerate_decelerate_interpolator"

android:fromAlpha="1.0"

android:toAlpha="0.0"

android:startOffset="500"

android:duration="500"/>

4、如果是在代码上设置共享一个interpolator,则可以在AnimationSet设置interpolator。

AnimationSet animationSet = newAnimationSet(true);

animationSet.setInterpolator(new AccelerateInterpolator());

5、如果不设置共享一个interpolator则可以在每一个Animation对象上面设置interpolator。

AnimationSet animationSet = newAnimationSet(false);

alphaAnimation.setInterpolator(new AccelerateInterpolator());

rotateAnimation.setInterpolator(new DecelerateInterpolator());

相关实现代码

package com.example.thestudyanimationview;

import android.app.Activity;
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.view.animation.Animation.AnimationListener;
import android.widget.Button;
import android.widget.ImageView; public class MainActivity extends Activity implements OnClickListener{
private Button rotateButton = null;
private Button scaleButton = null;
private Button alphaButton = null;
private Button translateButton = null;
private ImageView iv_rotateanimation = null;
private ImageView iv_scaleanimation = null;
private ImageView iv_alphaanimation = null;
private ImageView iv_translateanimation = null; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_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); iv_rotateanimation = (ImageView) findViewById(R.id.iv_rotateanimation);
iv_scaleanimation = (ImageView) findViewById(R.id.iv_scaleanimation);
iv_alphaanimation = (ImageView) findViewById(R.id.iv_alphaanimation);
iv_translateanimation = (ImageView) findViewById(R.id.iv_translateanimation); rotateButton.setOnClickListener(this);
scaleButton.setOnClickListener(this);
alphaButton.setOnClickListener(this);
translateButton.setOnClickListener(this);
} public void onClick(View v) {
switch (v.getId()) {
case R.id.rotateButton:
rotateAnimation();
break;
case R.id.scaleButton:
scaleAnimation();
break;
case R.id.alphaButton:
alphaAnimation();
break;
case R.id.translateButton:
translateAnimation();
break;
}
}
public void rotateAnimation(){
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);
iv_rotateanimation.startAnimation(animationSet);
rotateAnimation.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation arg0) {
} @Override
public void onAnimationRepeat(Animation arg0) {
} @Override
public void onAnimationEnd(Animation arg0) {
iv_rotateanimation.setVisibility(View.INVISIBLE);
}
});
}
public void scaleAnimation(){
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);
iv_scaleanimation.startAnimation(animationSet);
scaleAnimation.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation arg0) {
} @Override
public void onAnimationRepeat(Animation arg0) {
} @Override
public void onAnimationEnd(Animation arg0) {
iv_scaleanimation.setVisibility(View.INVISIBLE);
}
}); }
public void alphaAnimation(){
// 创建一个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方法执行动画
iv_alphaanimation.startAnimation(animationSet);
alphaAnimation.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation arg0) {
} @Override
public void onAnimationRepeat(Animation arg0) {
} @Override
public void onAnimationEnd(Animation arg0) {
iv_alphaanimation.setVisibility(View.INVISIBLE);
}
});
}
public void translateAnimation(){
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);
iv_translateanimation.startAnimation(animationSet);
translateAnimation.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation arg0) {
} @Override
public void onAnimationRepeat(Animation arg0) {
} @Override
public void onAnimationEnd(Animation arg0) {
iv_translateanimation.setVisibility(View.INVISIBLE);
}
});
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
} @Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}

布局文件

<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.thestudyanimationview.MainActivity" > <LinearLayout
android:id="@+id/ll_bts"
android:layout_width="match_parent"
android:layout_height="60dp"
android:gravity="center"
android:orientation="horizontal" > <Button
android:id="@+id/rotateButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="旋转" /> <Button
android:id="@+id/scaleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="缩放" /> <Button
android:id="@+id/alphaButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="淡入淡出" /> <Button
android:id="@+id/translateButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="移动" />
</LinearLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" > <ImageView
android:id="@+id/iv_rotateanimation"
android:layout_width="90dp"
android:layout_height="90dp"
android:src="@drawable/icon_pengyouquan" /> <ImageView
android:id="@+id/iv_scaleanimation"
android:layout_width="90dp"
android:layout_height="90dp"
android:src="@drawable/icon_qq" /> <ImageView
android:id="@+id/iv_alphaanimation"
android:layout_width="90dp"
android:layout_height="90dp"
android:src="@drawable/icon_qqkongjian" /> <ImageView
android:id="@+id/iv_translateanimation"
android:layout_width="90dp"
android:layout_height="90dp"
android:src="@drawable/weibologon" />
</LinearLayout> </LinearLayout>

Drawable Animation

是逐帧动画,那么使用它之前必须先定义好各个帧。我们可以通过代码定义,也可以使用xml文件定义,一般使用后者。如下:

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item android:drawable="@drawable/icon_aboutus" android:duration="200" />
<item android:drawable="@drawable/icon_join_menu" android:duration="200" />
<item android:drawable="@drawable/icon_join_opinion" android:duration="200" />
</animation-list>

其中android:oneshot=“true”表示该动画只播放一次,等于false时则循环播放。<item/>标签定义各个帧显示的图片。显示顺序依照<item/>定义顺序。

再看主界面的activity:

public class MainActivity extends Activity {
public ImageView iv_start;
public ImageView iv_stop;
public ImageView iv_animation;
public AnimationDrawable drawableAnimation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); iv_start = (ImageView) findViewById(R.id.iv_start);
iv_stop = (ImageView) findViewById(R.id.iv_stop);
iv_animation = (ImageView) findViewById(R.id.iv_animation);
iv_start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startAnimation();
}
});
iv_stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
stopAnimation();
}
});
iv_animation.setBackgroundResource(R.drawable.animations);
drawableAnimation = (AnimationDrawable) iv_animation.getBackground();
} public void startAnimation(){
drawableAnimation.start();
} public void stopAnimation(){
drawableAnimation.stop();
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
} @Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}

Android动画Drawable Animation的更多相关文章

  1. Android动画View Animation与Drawable Animation

    Animations 一.Animations介绍 Animations是一个实现android UI界面动画效果的API,Animations提供了一系列的动画效果,可以进行旋转.缩放.淡入淡出等, ...

  2. Android动画View Animation

    Animations 一.Animations介绍 Animations是一个实现android UI界面动画效果的API,Animations提供了一系列的动画效果,可以进行旋转.缩放.淡入淡出等, ...

  3. Android 动画——Frame Animation与Tween Animation

    很多手机应用的引导页都是动画的,添加动画后的应用画面会更加生动灵活,今天博主也学习了Android中Animation的使用,下面来总结下.  android中的Animation分为两种,一种是Fr ...

  4. android动画介绍--Animation 实现loading动画效果

    Animation的使用方法并不难.这里简单的介绍一下使用方法. 先看效果图: 效果还是不错的吧. 下面来看看使用方法. 动画效果是通过Animation来实现的,一共有四种,分别为: AlphaAn ...

  5. Android动画效果animation

    1.Tween 根据指定动画开始和结束时的对象属性(位置.Alpha值(透明度).大小.角度等)以及动画播放的时间长度生成动画: 2.Frame 指定每一帧所播放的图片和时间长度.   建立动画的方法 ...

  6. Android 动画具体解释Frame动画 (Drawable Animation)

    Frame动画像gif画画,通过一些静态的图片,以实现动画效果. Android sdk该AnimationDrawable就是专门针对Frame动画,当然Frame动画也可在java代码或者xml中 ...

  7. 【Android 基础】Animation 动画介绍和实现

    在前面PopupWindow 实现显示仿腾讯新闻底部弹出菜单有用到Animation动画效果来实现菜单的显示和隐藏,本文就来介绍下吧. 1.Animation 动画类型 Android的animati ...

  8. Android 动画——属性动画Property Animation

    Android在3.0之前只提供了两种动画:View Animation .Drawable Animation .也就是我们在<Android 动画——Frame Animation与Twee ...

  9. android动画效果编程基础--Android Animation

    动画效果编程基础--Android Animation 动画类型 Android的animation由四种类型组成 XML中 alpha 渐变透明度动画效果 scale 渐变尺寸伸缩动画效果 tran ...

随机推荐

  1. javaBean Request生命周期

    request: 在request范围内,JavaBean对象的有效范围为: ① 客户请求访问的当前JSP页面② 和当前JSP页面共享同一个请求的页面,包括<%@ include%>指令以 ...

  2. Struts2的Action(二)

    Struts2的Action可以是一个POJO(即简单的javaBean),也实现Action接口,或者继承ActionSupport类. 1.Action接口: public interface A ...

  3. Android课程---视图组件的总结

  4. 将公网IP自动发到Twitter上

    [Twitter] 1. 在https://apps.twitter.com/创建新的应用 2. 在https://dev.twitter.com/rest/reference/post/status ...

  5. ubuntu下wine安装软件

    安装wine 1. sudo apt-get install playonlinux playonlinux就是wine.或者在软件中心,搜索wine. 2. 在dash搜索playonlinux 安 ...

  6. copy module

    需求,当有一个实例a,我们需要一个新的实例b,b同a拥有相同的属性. 当我们使用a=b的模式的时候是一个赋值的过程.a和b指向同一个实例.b的任何操作都同a一样. 在这个使用需要使用copy模块.根据 ...

  7. ExtJS笔记 Grids

    参考:http://blog.csdn.net/zhangxin09/article/details/6885175 The Grid Panel is one of the centerpieces ...

  8. Java虚拟机内存管理机制

    自动内存管理机制 Java虚拟机(JVM)在执行Java程序过程中会把它所管理的内存划分为若干个不同的数据区域.这些区域都有各自的用途,以及创建和销毁的时间,有的区域随着虚拟机进程的启动而存在,有的区 ...

  9. ASP.NET MVC中从后台控制器(Controller)传递数据到前台页面视图(View)方式

    方式一: 数据存储模型Model: public class CalendarEvent { public string id { get; set; } public DateTime start ...

  10. win7 安装 memcached

    1. 下载 memcached-win32-1.4.4-14.zip,里面包含6个文件,将解压后的文件夹随便放在什么位置.如果需要win64版,下载 memcached-win64-1.4.4-14. ...