动画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 ...
随机推荐
- C# this.invoke()作用 多线程操作UI
Invoke()的作用是:在应用程序的主线程上执行指定的委托.一般应用:在辅助线程中修改UI线程( 主线程 )中对象的属性时,调用this.Invoke(); //测试的窗体 public class ...
- 我的android学习经历23
学习fragment时遇到的问题 这几天学习fragment静态加载时遇到这样的问题: java.lang.RuntimeException: Unable to start activity Com ...
- 【leetcode❤python】24. Swap Nodes in Pairs
#-*- coding: UTF-8 -*- # Definition for singly-linked list.# class ListNode(object):# def __init ...
- DrawIndexedPrimitive参数详解
HRESULT DrawIndexedPrimitive( [in] D3DPRIMITIVETYPE Type, [in] INT BaseVertexIndex, [in] UINT Min ...
- Highlighting Text Item On Entry In Oracle Forms
Highlight a Text Item in Oracle Forms With Visual Attribute It is very necessary to highlight the cu ...
- Django中的分页
直接看代码吧,还算比较简单: 先确认数据量有多少 根据页面显示数据的多少来分割数据,得到页面的开始数据和结束数据 根据开始和截止数据去切片数据,并且得到总共的页码数 根据一页显示多少页码和当前页码数, ...
- [POJ1753]Flip Game(异或方程组,高斯消元,枚举自由变量)
题目链接:http://poj.org/problem?id=1753 题意:同上. 这回翻来翻去要考虑自由变元了,假设返回了自由变元数量,则需要枚举自由变元. /* ━━━━━┒ギリギリ♂ eye! ...
- 【CC评网】2013.第39周 漂亮的作息表
作息表 网上看到一份夏令时的作息表,让人羡慕不已: 5:00 起床——迷糊5分钟,喝500ML白开水,坐马桶看Google reader 5:20 小区6KM(大约25min—30min)+100个俯 ...
- python_way day16 JQuary
python_way day16 JQuery 封装dom js代码 jQuery(1.10,1.12-兼容性好,2.0.以后放弃了ie9以下) - 封装了Dom & JavaScript 查 ...
- XAF应用开发教程(三)业务对象模型之引用类型与关联关系
本节介绍信息系统开发中最常见的问题,引用关系,一对多关系,多对多关系. 以客户信息为例,客户通常需要客户分类,如VIP客户,普通客户,潜在客户.当然,我们可以定义枚举类型进行定义出这个类型,并在客户类 ...