原来一直对动画一知半解,仅仅知道依照网上的方法会用即可了,可是自己写起来感觉确实有点费劲,今天最终研究了代码实现,一下子感觉清晰多了。先把总结例如以下,代码中有具体的凝视。

动画分类

1.Peoperty Animation

这个动画是Android3.0之后推出的眼下用处不大。

2.View Animation

这类动画也叫tween animation 主要分为 渐变动画(AlphaAnimation)旋转动画(RotateAnimation)

缩放动画(ScaleAnimation)位移动画(TranslateAnimation)

3.Drawable Animation

这类动画也叫帧动画 FrameAnimation

先上tween animation

MainActivity.java

package com.example.testanimation;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
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.widget.Button;
import android.widget.ImageView; public class MainActivity extends Activity { private ImageView imgView; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); imgView = (ImageView)this.findViewById(R.id.imgView) ;
Button btnAlpha = (Button)this.findViewById(R.id.btnAlpha) ;
btnAlpha.setOnClickListener(new AnimationOnClickListener(AnimationType.alpha));
Button btnRotate = (Button)this.findViewById(R.id.btnRotate) ;
btnRotate.setOnClickListener(new AnimationOnClickListener(AnimationType.rotate));
Button btnTraslate = (Button)this.findViewById(R.id.btnTraslate) ;
btnTraslate.setOnClickListener(new AnimationOnClickListener(AnimationType.traslate));
Button btnScale = (Button)this.findViewById(R.id.btnScale) ;
btnScale.setOnClickListener(new AnimationOnClickListener(AnimationType.scale));
Button btnComplex = (Button)this.findViewById(R.id.btnComplex) ;
btnComplex.setOnClickListener(new AnimationOnClickListener(AnimationType.complex)); } enum AnimationType {
alpha,
rotate,
traslate,
scale,
complex
} class AnimationOnClickListener implements OnClickListener { private AnimationType mAnimationType; public AnimationOnClickListener (AnimationType animationType) {
this.mAnimationType = animationType;
} @Override
public void onClick(View v) {
switch (mAnimationType) {
case alpha:
/**
* 透明度从不透明变为0.2透明度
*/
AlphaAnimation _alphaAnimation = new AlphaAnimation(1.0f, 0.2f);
_alphaAnimation.setDuration(200);
_alphaAnimation.setFillAfter(true);//动画运行完的状态显示
imgView.startAnimation(_alphaAnimation);
break;
case rotate:
/**
* RotateAnimation 以图片中点为圆心旋转360度
* params:
* pivotXType 中心点x坐标类型 RELATIVE_TO_SELF相对于自己,RELATIVE_TO_PARENT相对于父view
* pivotYType 同上
*
* pivotXValue,pivotYValue(圆心)
*
*/
RotateAnimation _rotateAnimation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
_rotateAnimation.setDuration(3000);
// _rotateAnimation.setRepeatMode(Animation.REVERSE);
imgView.startAnimation(_rotateAnimation);
break;
case traslate:
/**
* 依照图片的宽高2倍的位移移动
*/
TranslateAnimation _translateAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 2f, Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 2f);
_translateAnimation.setDuration(3000);
_translateAnimation.setFillAfter(true);
imgView.startAnimation(_translateAnimation);
break;
case scale:
/**
* ScaleAnimation 以图片左上角为精巧点,依照1.5倍尺寸放大
* params:
* pivotXType 中心点x坐标类型 RELATIVE_TO_SELF相对于自己,RELATIVE_TO_PARENT相对于父view
* pivotYType 同上
* pivotXValue,pivotYValue(精巧点)
*/
ScaleAnimation _scaleAnimation = new ScaleAnimation(1.0f, 1.5f, 1.0f, 1.5f, Animation.RELATIVE_TO_PARENT, 0f, Animation.RELATIVE_TO_SELF, 0f);
_scaleAnimation.setDuration(300);
_scaleAnimation.setZAdjustment(Animation.ZORDER_TOP);
_scaleAnimation.setRepeatCount(1);
_scaleAnimation.setRepeatMode(Animation.REVERSE);//必须设置setRepeatCount此设置才生效,动画运行完毕之后依照逆方式动画返回
imgView.startAnimation(_scaleAnimation);
break;
case complex:
AnimationSet _animationSet = new AnimationSet(false);
AlphaAnimation _alphaAnimation2 = new AlphaAnimation(1.0f, 0.2f);
_alphaAnimation2.setDuration(1000);
_alphaAnimation2.setRepeatCount(1);
_alphaAnimation2.setRepeatMode(Animation.REVERSE);
// _alphaAnimation2.setFillAfter(true);//设此地方不好使,必须设置到AnimationSet中 TranslateAnimation _translateAnimation2 = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 2f, Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 2f);
_translateAnimation2.setDuration(1000);
_translateAnimation2.setRepeatCount(1);
_translateAnimation2.setRepeatMode(Animation.REVERSE);
// _translateAnimation2.setFillAfter(true); _animationSet.addAnimation(_alphaAnimation2);
_animationSet.addAnimation(_translateAnimation2);
_animationSet.setFillAfter(true);
// _animationSet.setRepeatCount(1);
// _animationSet.setRepeatMode(Animation.REVERSE);//这两个属性设此地不好使,必须单个设置 imgView.startAnimation(_animationSet);
break; default:
break;
} }
} @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;
} }

activity_main.xml

<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"
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" > <ImageView
android:id="@+id/imgView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" /> <Button
android:id="@+id/btnAlpha"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="alpha" /> <Button
android:id="@+id/btnRotate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="rotate" /> <Button
android:id="@+id/btnTraslate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="traslate" /> <Button
android:id="@+id/btnScale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="scale" /> <Button
android:id="@+id/btnComplex"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="animationSet" /> </LinearLayout>

以下为frame animation

public class FrameAnimationAcitvity extends Activity {
private ImageView imageView;
private AnimationDrawable ad;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.animation_list);
Button button=(Button)findViewById(R.id.button1);
imageView=(ImageView)findViewById(R.id.imageView1);
imageView.setBackgroundResource(R.drawable.framedrawable);
ad=(AnimationDrawable)imageView.getBackground();
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ad.start();
}
});
}
}

framedrawable.xml

<animation-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/grid_liushui" android:duration="200"/>
<item android:drawable="@drawable/grid_payout" android:duration="200"/>
<item android:drawable="@drawable/grid_report" android:duration="200"/>
</animation-list></span>

附上tween动画的源代码下载链接

http://download.csdn.net/detail/duanyu218/7449471

android动画-动画分类及代码演示样例的更多相关文章

  1. java 覆盖hashCode()深入探讨 代码演示样例

    java 翻盖hashCode()深入探讨 代码演示样例 package org.rui.collection2.hashcode; /** * 覆盖hashcode * 设计HashCode时最重要 ...

  2. Python Web框架Tornado的异步处理代码演示样例

    1. What is Tornado Tornado是一个轻量级但高性能的Python web框架,与还有一个流行的Python web框架Django相比.tornado不提供操作数据库的ORM接口 ...

  3. Android通过startService播放背景音乐简单演示样例

    关于startService的基本使用概述及其生命周期可參见博客<Android中startService的使用及Service生命周期>. 本文通过播放背景音乐的简单演示样例,演示sta ...

  4. Swift语言 简明基础 代码演示样例

    开发环境: Mac.Xcode6.0 下面内容均可创建ios common line项目来測试 1.Hello World演示样例 使用xcode创建新的common line项目,查看主文件main ...

  5. 使用Android编写录制视频小程序演示样例

    主要实现录制功能的类:Camera类和MediaRecorder类.功能描写叙述:首先进入视频录制界面,点击录像button进入录像功能界面,点击录制開始录制视频, 点击停止button,将录制的视频 ...

  6. Java中普通代码块,构造代码块,静态代码块的代码演示样例及区分

    //运行顺序:(优先级从高到低.)静态代码块>mian方法>构造代码块>构造方法. 当中静态代码块仅仅运行一次.构造代码块在每次创建对象是都会运行. 1 普通代码块 <span ...

  7. 【甘道夫】Eclipse+Maven搭建HBase开发环境及HBaseDAO代码演示样例

    环境: Win764bit Eclipse Version: Kepler Service Release 1 java version "1.7.0_40" 第一步:Eclips ...

  8. SSL通关之代码演示样例(四)

    实际开发过程中,server端是不须要多加代码处理的,由于ssl验证过程是由server(tomcat.nginx等)完毕的. 这段代码也是參考了网上的: 新建一个web项目,项目结构和须要引入的ja ...

  9. c语言学习之结构篇代码演示样例-输入n个同学的姓名,数学英语成绩,依照平均分从低到高排序并输出

    #include<stdio.h> void main(){ const int count = 5;//定义数量 struct student{ char name[80]; float ...

随机推荐

  1. 6.MIL采集和实时显示

    前面讲到的都是离线的图像获取方法,实际中我们做机器视觉都是在线采集图像和处理,处理结果决定了计算机要给出的控制信号如电机运动等,这样就实现了实时视觉反馈运动.MIL中的采集需要Matrox采集板卡的支 ...

  2. Android NDK 简单介绍、工具安装、环境配置

    NDK全称:Native Development Kit. 1.NDK是一系列工具的集合. * NDK提供了一系列的工具,帮助开发人员高速开发C(或C++)的动态库,并能自己主动将so和java应用一 ...

  3. wift - 使用UIScreen类获取屏幕大小尺寸

    UISreen类代表了屏幕,开发中一般用来获取屏幕相关的属性,例如获取屏幕的大小. 1 2 3 4 5 6 7 //获取屏幕大小 var screenBounds:CGRect = UIScreen. ...

  4. EF具体用在什么类型的项目上

    一般来说,使用EF框架,肯定会比直接使用ADO.NET,消耗的时间多一些. 因为使用ADO.NET直接把SQL语句传回数据库执行. 而使用EF框架的话,会把所用到的尸体,转换成相对应得SQL,然后再传 ...

  5. 一个上传EXCEL导入示例

    REPORT ZTEST_UPEXCEL. data: gt_table type TABLE OF SFLIGHT, gs_table like line of gt_table. DATA:lt_ ...

  6. 测试framebuffer

    static GGLContext *gr_context = 0; static GGLSurface gr_framebuffer[2]; static unsigned gr_active_fb ...

  7. TensorFlow实现与优化深度神经网络

    TensorFlow实现与优化深度神经网络 转载请注明作者:梦里风林Github工程地址:https://github.com/ahangchen/GDLnotes欢迎star,有问题可以到Issue ...

  8. [置顶] ubuntu 和 win7 远程登陆 + vnc登陆

    ubuntu 和 win7 远程登陆: 第一种(通过win7自带的远程桌面来连接ubuntu) 1. windows7配置 我的电脑->属性->远程设置.-----允许远程连接 2. ub ...

  9. 百度地图js版定位控件

    一 概述 百度地图在最新版已加入浏览器定位控件,个人认为应该是既高德地图更新了一个浏览器也能定位功能后,百度不甘落后自己简简单单,草草写了个这个功能的定位控件 GeolocationControl 这 ...

  10. c++ __declspec关键字详细用法

    c++ __declspec关键字详细用法 __declspec用于指定所给定类型的实例的与Microsoft相关的存储方式.其它的有关存储方式的修饰符如static与extern等是C和C++语言的 ...