Android动画View Animation与Drawable Animation
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动画View Animation与Drawable Animation的更多相关文章
- Android动画主要包含补间动画(Tween)View Animation、帧动画(Frame)Drawable Animation、以及属性动画Property Animation
		
程序运行效果图: Android动画主要包含补间动画(Tween)View Animation.帧动画(Frame)Drawable Animation.以及属性动画Property Animatio ...
 - Android动画效果之初识Property Animation(属性动画)
		
前言: 前面两篇介绍了Android的Tween Animation(补间动画) Android动画效果之Tween Animation(补间动画).Frame Animation(逐帧动画)Andr ...
 - Android动画View Animation
		
Animations 一.Animations介绍 Animations是一个实现android UI界面动画效果的API,Animations提供了一系列的动画效果,可以进行旋转.缩放.淡入淡出等, ...
 - android动画介绍之 自己定义Animation动画实现qq抖一抖效果
		
昨天我们介绍了Animation的基本使用方法.小伙伴们了解的怎么样了?假设还没有了解过Animation的小伙伴能够看看这篇博客 android动画介绍--Animation 实现loading动画 ...
 - Android动画-View动画
		
View动画 Android动画分为三类:View动画,帧动画,和属性动画.帧动画也是View动画的一种. View动画的作用对象是View,之所以强调这一点是因为其作用对象有别于Android的另一 ...
 - Android动画总结#补间动画(Tween Animation/View Animation) #帧动画(Frame Animation/Drawable Animation)#属性动画(PropertyAnimation)
		
1.共有三种动画,英文名字多种叫法如下 第一种动画:补间动画(Tween Animation/View Animation) 四个:RotateAnimation旋转. AlphaAnimation透 ...
 - Android animation学习笔记之view/drawable animation
		
前一章中总结了android animation中property animation的知识和用法,这一章总结View animation和Drawable animation的有关知识: View ...
 - Android动画效果之Frame Animation(逐帧动画)
		
前言: 上一篇介绍了Android的Tween Animation(补间动画) Android动画效果之Tween Animation(补间动画),今天来总结下Android的另外一种动画Frame ...
 - Android动画学习(二)——Tween Animation
		
前两天写过一篇Android动画学习的概述,大致的划分了下Android Animation的主要分类,没有看过的同学请移步:Android动画学习(一)——Android动画系统框架简介.今天接着来 ...
 
随机推荐
- Type Trait 和 Type Utility
			
所谓Type trait,提供了一种用来处理type 属性的办法,它是个template,可在编译期根据一个或多个template实参(通常也是type)产出一个type或者value. templa ...
 - centos7安装kong和kong-dashboard
			
1.安装Kong yum install -y https://kong.bintray.com/kong-community-edition-rpm/centos/7/kong-community- ...
 - LogHelper
			
原文链接 public class LogHelper { static string strLogCOMPath = Directory.GetCurrentDirectory() + " ...
 - HDU - 6704 K-th occurrence (后缀数组+主席树/后缀自动机+线段树合并+倍增)
			
题意:给你一个长度为n的字符串和m组询问,每组询问给出l,r,k,求s[l,r]的第k次出现的左端点. 解法一: 求出后缀数组,按照排名建主席树,对于每组询问二分或倍增找出主席树上所对应的的左右端点, ...
 - 再提供一种解决Nginx文件类型错误解析漏洞的方法
			
[文章作者:张宴 本文版本:v1.2 最后修改:2010.05.24 转载请注明原文链接:http://blog.zyan.cc/nginx_0day/] 注:2010年5月23日14:00前阅读本文 ...
 - 【GitHub】命令行操作
			
提交文件 本地修改之后:git add . 提交到暂存区 commit一下:git commit -m "xxx" -m表示信息,不填无法commit 提交到远程仓库: ...
 - 【LOJ#536】「LibreOJ Round #6」花札
			
题目链接 题目描述 「UniversalNO」的规则如下:每张牌有一种颜色和一个点数.两个人轮流出牌,由 Alice 先手,最开始牌堆为空,出的人可以出任意牌(放到牌堆顶),之后出的牌必须和当时牌堆顶 ...
 - 04-01 Django之模板层
			
Django之模板层 一 模板简介 在刚刚介绍完的视图层中我们提到,浏览器发送的请求信息会转发给视图函数进行处理,而视图函数在经过一系列处理后必须要有返回信息给浏览器.如果我们要返回html标签.cs ...
 - Python---函数2---王伟
			
#### 作用域 ```python#1.作用域:变量生效的区域#2.作用域的分类a.全局作用域(全局变量)- 全局作用域在程序执行时创建,在程序执行结束时销毁- 所有函数以外的区域都是全局作用域- ...
 - 关于enter事件的触发
			
如果您使用了antd的Button组件,那么恭喜已经封装好了,只要加上htmlType='submit', 如果没有使用其他框架,使用onPress或者onKeydown事件,判断e.keycode ...