动画Animation
动画分类:Animation 单一动画
AnimationSet 复合动画
AnimationSet是Animation的实现子类,Animation是一个抽象类,他的实现子类主要有如下几种:

主要有缩放 ScaleAnimation ,平移TranslateAnimation,透明(不清楚)AlphaAnimation,旋转 RotateAnimation。
Animation作为父类的公用方法:
SetDuration(..);设置的是动画持续的时间,单位是毫秒:1000=1秒
SetFillAfter(..);设置动画结束后是否保持状态,false则返回最初状态;
SetFillBefore(..);
SetFillEnable(..); //这两个搞不懂
SetStartOffSet(..);这是动画开始延时时间;
还有几个通用属性:
pivotXType: 指定x轴中心的类型有三种参数:Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF,Animation.RELATIVE_TO_PARENT
pivotYtype:指定Y轴中心的类型同x轴
动画移动选择或者缩放都有一个中心点,这个中心点默认原点是视图左上角的点

pivotXTyp的作用就是指定中心点。如果设置成绝对路径Animation.ABSOLUTE,那么就需要计算距离这个原点的距离,把x轴中心设置到x的中间,那么就是view.getWidth()/2
如果设置成相对自身Animation.RELATIVE_TO_SELF,设置成0.5f就可以了,这个要注意理解。
最后一个是相对父视图的绝对距离这个计算相对麻烦。
以下是主程序:
package com.skymaster.hs.myapplication; import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.animation.Animation;
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.Toast; public class AnimationTest extends AppCompatActivity {
private ImageView iv_animation;
private Button btn_start1,btn_start2,btn_start3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_animation_test);
iv_animation = (ImageView) findViewById(R.id.iv_animation);
btn_start1 = (Button) findViewById(R.id.btn_start1);
btn_start2 = (Button) findViewById(R.id.btn_start2);
btn_start3 = (Button) findViewById(R.id.btn_start3); btn_start1.setOnClickListener(new KeyPress());
btn_start2.setOnClickListener(new KeyPress());
btn_start3.setOnClickListener(new KeyPress());
iv_animation.setOnClickListener(new View.OnClickListener() {
//这里设置ImageView点击监听是为了测试当动画移走之后,点击ImageView
//原来的位置是否有效果,实测是有的,也就是说即使ImageView移走了但是它的
//按键有效位置还是xml布局的那个位置。
@Override
public void onClick(View v) {
Toast.makeText(AnimationTest.this,"ImageClick",Toast.LENGTH_SHORT)
.show();
}
});
} private class KeyPress implements View.OnClickListener{
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btn_start1:
//设定x轴坐标中心点(0,2f),2f是移动自身宽度的两倍
//设定y轴坐标中心点(0,1.5f),同理x轴
TranslateAnimation animation = new TranslateAnimation(Animation.ABSOLUTE,0,Animation.RELATIVE_TO_SELF,2.0f
,Animation.ABSOLUTE,0,Animation.RELATIVE_TO_SELF,1.5f);
animation.setDuration(2000);//持续时间
animation.setStartOffset(1000);
animation.setFillAfter(true);//动画完成保持不会原位
//设定动画监听
animation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
Toast.makeText(AnimationTest.this,"start!",Toast.LENGTH_SHORT)
.show();
} @Override
public void onAnimationEnd(Animation animation) {
Toast.makeText(AnimationTest.this,"End!",Toast.LENGTH_SHORT)
.show();
} @Override
public void onAnimationRepeat(Animation animation) {
Toast.makeText(AnimationTest.this,"Repeat!",Toast.LENGTH_SHORT)
.show();
}
});
iv_animation.startAnimation(animation);
break;
case R.id.btn_start2:
//设定缩放中心点与缩放比例x轴是0.2f放大到2f,这里都是相对距离
//y轴缩放比例同理,从0.2到3f是放大如果反过来就是缩小
//后面4个参数是设置缩放中心点,用的是绝对距离
ScaleAnimation scaleAnimation = new ScaleAnimation(0.2f,2f,0.2f,3f,Animation.ABSOLUTE
,iv_animation.getWidth()/2,Animation.ABSOLUTE,0);
scaleAnimation.setDuration(2000);
scaleAnimation.setFillAfter(false);
iv_animation.startAnimation(scaleAnimation);
break;
case R.id.btn_start3:
//旋转动画
RotateAnimation rotateAnimation = new RotateAnimation(0f,180f,Animation.RELATIVE_TO_SELF,0.5f,
Animation.RELATIVE_TO_SELF,0.5f);
//rotateAnimation.setFillAfter(true);
rotateAnimation.setFillEnabled(true);
rotateAnimation.setFillBefore(true);
rotateAnimation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
Toast.makeText(AnimationTest.this, "rotateStart!", Toast.LENGTH_SHORT)
.show();
} @Override
public void onAnimationEnd(Animation animation) {
Toast.makeText(AnimationTest.this, "rotateEnd!", Toast.LENGTH_SHORT)
.show();
//iv_animation.startAnimation(animation);
} @Override
public void onAnimationRepeat(Animation animation) {
Toast.makeText(AnimationTest.this, "rotateRepeat!", Toast.LENGTH_SHORT)
.show();
}
});
rotateAnimation.setDuration(2000);
rotateAnimation.setStartOffset(1000);
iv_animation.startAnimation(rotateAnimation);
break;
default:break;
}
}
}
}
xml文件:
<?xml version="1.0" encoding="utf-8"?>
<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="com.skymaster.hs.myapplication.AnimationTest"> <ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="100dp"
android:background="@drawable/ic_launcher"
android:id="@+id/iv_animation"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="start1"
android:layout_alignParentBottom="true"
android:id="@+id/btn_start1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="start2"
android:layout_alignParentBottom="true"
android:layout_toRightOf="@id/btn_start1"
android:id="@+id/btn_start2"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="start3"
android:layout_toRightOf="@id/btn_start2"
android:layout_alignParentBottom="true"
android:id="@+id/btn_start3"/>
</RelativeLayout>
AnimationSet可以配置多个动画
除了Animation之外还有xml利用多幅图片来实现动画。
利用Animation实现动画,属性并并没有随着动画移动。上面例子中ImageView的点击事件还是在最初的位置,要实现属性动画就要利用ObjectAnimation类
动画Animation的更多相关文章
- 动画animation的三个应用(漂浮的白云、旋转的星球、正方体合成)
× 目录 [1]漂浮的白云 [2]旋转的星球 [3]正方体合成 前面的话 前面介绍过动画animation的详细用法,本文主要介绍动画animation的三个效果 漂浮的白云 [效果演示] [简要介绍 ...
- CSS3的变形transform、过渡transition、动画animation学习
学习CSS3动画animation得先了解一些关于变形transform.过渡transition的知识 这些新属性大多在新版浏览器得到了支持,有些需要添加浏览器前缀(-webkit-.-moz-.- ...
- 精灵动画Animation对话框组成Idle动画的各精灵
精灵动画Animation对话框组成Idle动画的各精灵 1.3 精灵动画 场景中已经添加了精灵,现在是时候让让它动起来了.读者也许已经从精灵图集中,各精灵的命名中看出来了,这个精灵一共有两种动画状 ...
- Qt-4.6动画Animation快速入门三字决
Qt-4.6动画Animation快速入门三字决 Qt-4.6新增了Animation Framework(动画框架),让我们能够方便的写一些生动的程序.不必像以前的版本一样,所有的控件都枯燥的呆在伟 ...
- css3 动画(animation)-简单入门
css3之动画(animation) css3中我们可以使用动画,由于取代以前的gif图片,flash动画,以及部分javascript代码(相信有很多同学都用过jquery中的animate方法来做 ...
- Android 动画animation 深入分析
转载请注明出处:http://blog.csdn.net/farmer_cc/article/details/18259117 Android 动画animation 深入分析 前言:本文试图通过分析 ...
- View 动画 Animation 运行原理解析
这次想来梳理一下 View 动画也就是补间动画(ScaleAnimation, AlphaAnimation, TranslationAnimation...)这些动画运行的流程解析.内容并不会去分析 ...
- CSS3动画属性:动画(animation)
一:动画(animation)的参数详解 由于上面用到了animation动画,这里详细介绍下这个animation的参数. 简介 CSS动画(Animations)简单说就是在一段固定的动画时间内暗 ...
- 《The Cg Tutorial》阅读笔记——动画 Animation
这段时间阅读了英文版的NVidia官方的<The Cg Tutorial>,借此来学习基本的图形学知识和着色器编程. 在此做一个阅读笔记. 本文为大便一箩筐的原创内容,转载请注明出处,谢谢 ...
- amazeui学习笔记--css(常用组件15)--CSS动画Animation
amazeui学习笔记--css(常用组件15)--CSS动画Animation 一.总结 1.css3动画封装:CSS3 动画封装,浏览器需支持 CSS3 动画. Class 描述 .am-anim ...
随机推荐
- reactor模式学习
一.介绍reactor模式 二.使用reactor模式 三.参考 http://blog.csdn.net/swordmanwk/article/details/6170995 该文章,简单介绍了r ...
- Windows手动添加开机启动项
@方法1. 添加程序完整路径到注册表HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run下 或者添加到HKEY_CURREN ...
- 2016年11月23日 星期三 --出埃及记 Exodus 20:14
2016年11月23日 星期三 --出埃及记 Exodus 20:14 "You shall not commit adultery.不可奸淫.
- AutoIt实现selenium上传文件
1,安装autoIt 2,工程中建文件夹,例如:script,将autoit 复制到该文件夹,并且编辑视频所在的文件夹的路径,编辑"au3"格式的文件 3,编辑完成后生成EXE文件 ...
- HDU 5615 Jam's math problem
Jam's math problem Problem Description Jam has a math problem. He just learned factorization.He is t ...
- 如何通过Button获取UITableViewCell
发现一个奇怪的问题: 手机(ios7) 2015-06-17 15:11:29.323 ***[1412:60b] [btn superview] = UITableViewCellContent ...
- BZOJ 3589 动态树(子树操作,链查询)
题目链接:http://www.lydsy.com:808/JudgeOnline/problem.php?id=3589 题意:给出一棵有根树,两种操作:(1)以u为根的子树所有节点权值加上一个数字 ...
- [UVA11464]Even Parity(状压,枚举)
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- Codeforces Round #379 (Div. 2) C. Anton and Making Potions 二分
C. Anton and Making Potions time limit per test 4 seconds memory limit per test 256 megabytes input ...
- bzoj 2242: [SDOI2011]计算器 BSGS+快速幂+扩展欧几里德
2242: [SDOI2011]计算器 Time Limit: 10 Sec Memory Limit: 512 MB[Submit][Status][Discuss] Description 你被 ...