`
007007jing
  • 浏览: 42338 次
  • 性别: Icon_minigender_1
  • 来自: 济南
社区版块
存档分类
最新评论

android2.3 api demo 学习系列(2)--App/Activity/Animation

阅读更多

1、首先学习sdk中关于Animation的说明。andorid的动画主要分为下面三种

  • Property Animation

    版本要求: Android 3.0 (API level 11), property animation支持任何对象的动画效果,也是android推荐的模式。 可以很好的扩展。(因为本次demo版本为2.3 在这里不在研究,留在以后研究)

  • View Animation

    View Animation 只能在Views使用. 优点是实现简单.缺点也很明显:首先只支持view,其次是他只是改变view的动画效果,并没有实际改变view,例如让一个按钮在屏幕上移动,动画效果是正确实现,但是按钮的点击位置并没有改变,需要自己另外实现。

View Animation提供了Tween和frame两种实现方式。

接下来学习Tween Animation:

动画的定义可以使用xml配置文件或者代码实现。xml配置文件定义相对于代码实现具有可读性、重用行、方便维护等特性,配置文件放置在工程的res/anim/文件内,xml配置文件必须包含下面其中一个元素:<alpha><scale>,<translate><rotate>或者 <set> ,嵌套set。例如下面的一个示例拉伸旋转view

 

<set android:shareInterpolator="false">
    <scale
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:fromXScale="1.0"
        android:toXScale="1.4"
        android:fromYScale="1.0"
        android:toYScale="0.6"
        android:pivotX="50%"
        android:pivotY="50%"
        android:fillAfter="false"
        android:duration="700" />
    <set android:interpolator="@android:anim/decelerate_interpolator">
        <scale
           android:fromXScale="1.4"
           android:toXScale="0.0"
           android:fromYScale="0.6"
           android:toYScale="0.0"
           android:pivotX="50%"
           android:pivotY="50%"
           android:startOffset="700"
           android:duration="400"
           android:fillBefore="false" />
        <rotate
           android:fromDegrees="0"
           android:toDegrees="-45"
           android:toYScale="0.0"
           android:pivotX="50%"
           android:pivotY="50%"
           android:startOffset="700"
           android:duration="400" />
    </set>
</set>

  保存xml文件后在代码中调用

 

 

ImageView spaceshipImage = (ImageView) findViewById(R.id.spaceshipImage);
Animation hyperspaceJumpAnimation = AnimationUtils.loadAnimation(this, R.anim.xml配置文件);
spaceshipImage.startAnimation(hyperspaceJumpAnimation);

 

 接下来看看每一个的属性

set:

android:interpolator:可以使用平台提供的属性 如下图所示

如果你觉得平台定义的不符合需求,可以更改他们(定义的时候名字强制使用小写) 

各个Interpolator的属性如下

 

<accelerateDecelerateInterpolator>
没有属性
<accelerateInterpolator>
The rate of change starts out slowly, then accelerates.

attributes:

android:factor
Float. The acceleration rate (default is 1).
<anticipateInterpolator>
The change starts backward then flings forward.

attributes:

android:tension
Float. The amount of tension to apply (default is 2).
<anticipateOvershootInterpolator>
The change starts backward, flings forward and overshoots the target value, then settles at the final value.

attributes:

android:tension
Float. The amount of tension to apply (default is 2).
android:extraTension
Float. The amount by which to multiply the tension (default is 1.5).
<bounceInterpolator>
The change bounces at the end.

No attributes

<cycleInterpolator>
Repeats the animation for a specified number of cycles. The rate of change follows a sinusoidal pattern.

attributes:

android:cycles
Integer. The number of cycles (default is 1).
<decelerateInterpolator>
The rate of change starts out quickly, then decelerates.

attributes:

android:factor
Float. The deceleration rate (default is 1).
<linearInterpolator>
The rate of change is constant.

No attributes.

<overshootInterpolator>
The change flings forward and overshoots the last value, then comes back.

attributes:

android:tension
Float. The amount of tension to apply (default is 2).

 

修改时例如下面的xml定义

 

<?xml version="1.0" encoding="utf-8"?>
<overshootInterpolator xmlns:android="http://schemas.android.com/apk/res/android"
    android:tension="7.0"
    />

xml文件保存在res/anim文件夹内

 

  同事别的动画配置文件可以引用interpolator 例如

 

<scale xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@anim/预先定义的interpolator"
    android:fromXScale="1.0"
    android:toXScale="3.0"
    android:fromYScale="1.0"
    android:toYScale="3.0"
    android:pivotX="50%"
    android:pivotY="50%"
    android:duration="700" />

 set的另外一个属性 android:shareInterpolator:true向所有的子元素共享interpolator

 

 

 

<alpha>
A fade-in or fade-out animation. Represents an AlphaAnimation.

attributes:

android:fromAlpha
Float. Starting opacity offset, where 0.0 is transparent and 1.0 is opaque.
android:toAlpha
Float. Ending opacity offset, where 0.0 is transparent and 1.0 is opaque.
<scale>
A resizing animation. You can specify the center point of the image from which it grows outward (or inward) by specifying pivotX and pivotY. For example, if these values are 0, 0 (top-left corner), all growth will be down and to the right. Represents a ScaleAnimation.

attributes:

android:fromXScale
Float. Starting X size offset, where 1.0 is no change.
android:toXScale
Float. Ending X size offset, where 1.0 is no change.
android:fromYScale
Float. Starting Y size offset, where 1.0 is no change.
android:toYScale
Float. Ending Y size offset, where 1.0 is no change.
android:pivotX
Float. The X coordinate to remain fixed when the object is scaled.
android:pivotY
Float. The Y coordinate to remain fixed when the object is scaled.
<translate>
A vertical and/or horizontal motion. Supports the following attributes in any of the following three formats: values from -100 to 100 ending with "%", indicating a percentage relative to itself; values from -100 to 100 ending in "%p", indicating a percentage relative to its parent; a float value with no suffix, indicating an absolute value. Represents a TranslateAnimation.

attributes:

android:fromXDelta
Float or percentage. Starting X offset. Expressed either: in pixels relative to the normal position (such as "5"), in percentage relative to the element width (such as "5%"), or in percentage relative to the parent width (such as "5%p").
android:toXDelta
Float or percentage. Ending X offset. Expressed either: in pixels relative to the normal position (such as "5"), in percentage relative to the element width (such as "5%"), or in percentage relative to the parent width (such as "5%p").
android:fromYDelta
Float or percentage. Starting Y offset. Expressed either: in pixels relative to the normal position (such as "5"), in percentage relative to the element height (such as "5%"), or in percentage relative to the parent height (such as "5%p").
android:toYDelta
Float or percentage. Ending Y offset. Expressed either: in pixels relative to the normal position (such as "5"), in percentage relative to the element height (such as "5%"), or in percentage relative to the parent height (such as "5%p").
<rotate>
A rotation animation. Represents a RotateAnimation.

attributes:

android:fromDegrees
Float. Starting angular position, in degrees.
android:toDegrees
Float. Ending angular position, in degrees.
android:pivotX
Float or percentage. The X coordinate of the center of rotation. Expressed either: in pixels relative to the object's left edge (such as "5"), in percentage relative to the object's left edge (such as "5%"), or in percentage relative to the parent container's left edge (such as "5%p").
android:pivotY
Float or percentage. The Y coordinate of the center of rotation. Expressed either: in pixels relative to the object's top edge (such as "5"), in percentage relative to the object's top edge (such as "5%"), or in percentage relative to the parent container's top edge (such as "5%p").

 

上述<alpha><scale><translate><rotate>同样拥有下图中得属性


介绍完了tween animation  接下来看看 frame animation的内容

frame animation主要是定义显示一系列图片的动画,xml配置文件保存在res/drawable/ xml的语法如下:

 

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot=["true" | "false"] >
    <item
        android:drawable="@[package:]drawable/图片资源引用"
        android:duration="integer持续时间" />
</animation-list>

 在按照语法定义完后 xml可以保存在 res/anim or drawable文件夹下面

 

调用代码如下 为什么在这个方法里面调用 请看注释不多解释

 

@Override
	public void onWindowFocusChanged(boolean hasFocus) {
		// TODO Auto-generated method stub
		super.onWindowFocusChanged(hasFocus);
		//图片背景置换 frame animation
		//此段代码为什么放在 onWindowFocusChanged
		//因为当我们在onCreate中调用AnimationDrawable的start方法时,窗口Window对象还没有完全初始化,
		//AnimationDrawable不能完全追加到窗口Window对象中,那么该怎么办呢?我们需要把这段代码放在onWindowFocusChanged方法中,
		//当Activity展示给用户时,onWindowFocusChanged方法就会被调用,我们正是在这个时候实现我们的动画效果。
		//当然,onWindowFocusChanged是在onCreate之后被调用的
		ImageView changeImage = (ImageView) findViewById(R.id.animation_frame_img);
		changeImage.setBackgroundResource(R.anim.animation_frame);//xml配置文件
		AnimationDrawable changeAnimation = (AnimationDrawable) changeImage.getBackground();
		changeAnimation.start();
	}

 如果用纯代码实现frame animation 代码示如下

 

AnimationDrawable anim = new AnimationDrawable();  
for (int i = 1; i <= 4; i++) {  
    int id = getResources().getIdentifier("animation_frame_" + i, "drawable", getPackageName());  
    Drawable drawable = getResources().getDrawable(id);  
    anim.addFrame(drawable, 300);  
}  
anim.setOneShot(false);  
image.setBackgroundDrawable(anim);  
anim.start();  
 
  • Drawable Animation

 见上文 frame animation

 

最后说一下 apidemo的activity的淡入淡出和放大缩小动画入场

1.淡入淡出 配置文件

 

<alpha xmlns:android="http://schemas.android.com/apk/res/android"
       android:interpolator="@android:anim/accelerate_interpolator"
       android:fromAlpha="0.0" android:toAlpha="1.0"
       android:duration="@android:integer/config_longAnimTime" />

 

 

<translate xmlns:android="http://schemas.android.com/apk/res/android"
       android:interpolator="@android:anim/accelerate_interpolator"
       android:fromXDelta="0" android:toXDelta="0"
       android:duration="@android:integer/config_longAnimTime" />

 代码中调用 

 

 

startActivity(new Intent(Animation.this, ApiDemosStudyActivity.class));
overridePendingTransition(R.anim.animation_fade,R.anim.animation_hold);

 其中关于overridePendingTransition方法 sdk中有如下说明

Call immediately after one of the flavors of startActivity(Intent) or finish to specify an explicit transition animation to perform next.

2.zoom效果同样 只是xml配置有区别

 

  • 大小: 13.4 KB
  • 大小: 6.2 KB
分享到:
评论

相关推荐

    android api demo讲解

    ### Android API Demo详解 #### 一、概述 本篇文章旨在为初学者提供一套全面而深入的Android API Demo解析,帮助大家更好地理解Android开发中的各种基础知识和技术细节。文章将按照给出的目录顺序,逐一分析每个...

    android api demo

    本文档是关于基于Android 2.3版本的API Demo解析,旨在帮助开发者更好地理解Android应用开发中的各种控件和技术点。通过一系列实例,详细介绍了如何使用Android SDK中的各个功能模块。 #### 二、主要内容概述 文档...

    Android应用源码之Activity实现透明的最简洁Demo-IT计算机-毕业设计.zip

    在Android应用开发中,...通过这个Demo,开发者不仅能学习到如何实现Activity的透明效果,还能了解到Android系统中主题、布局、动画、Activity管理等多个方面的知识。这为移动应用开发提供了丰富的实践经验和理论基础。

    Android_api_demo

    ### Android API Demo 知识点概述 #### 一、概览 本文档旨在全面解析 Android API Demo 中的各种案例,通过具体实例深入理解 Android 开发中的关键技术和应用实践。该文档覆盖了从简单的用户界面设计到复杂的后台...

    android API-DEMOS中文解析文档

    #### 2.3 App-&gt;Activity-&gt;Animation 该章节讲解了动画在Activity中的应用,包括但不限于淡入淡出效果、缩放动画等。这些动画效果可以让用户界面变得更加生动有趣,提高用户体验。 #### 2.4 App-&gt;Activity-&gt;...

    100多个Android Demo的集合

    这个"100多个Android Demo的集合"是一个非常宝贵的资源库,它包含了丰富的实例,旨在帮助初学者深入理解和实践Android开发中的各种组件和功能。下面,我们将详细探讨其中可能包含的一些重要知识点。 1. **用户界面...

    Android 带文字的ProgressBar Demo源码-IT计算机-毕业设计.zip

    这个"Android带文字的ProgressBar Demo源码"提供了一个具体的实例,展示了如何在ProgressBar上同时显示进度和相关文字信息,这对于用户交互和提升用户体验有重要作用。下面将详细解析这个Demo涉及的知识点。 1. **...

    android实例--窗口抖动demo

    2. **布局文件**:在`res/layout/activity_main.xml`中,添加一个需要抖动的View,如TextView或ImageView,并为其设置ID。 ```xml xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app=...

    android开发的几个demo

    在Android开发领域,示例代码(Demo)是学习和理解平台特性和技术的关键。本话题主要探讨了几个Android开发的Demo,这些示例通常涵盖了基本功能到高级特性的实现,帮助开发者快速上手并深入理解Android应用开发。...

    android导航栏按钮视频demo

    总之,“android导航栏按钮视频demo”是一个很好的学习资源,它涵盖了Android应用开发中的基本导航栏实现,包括布局定义、菜单设置以及事件监听。通过深入研究这个demo,开发者可以轻松地将类似的功能集成到自己的...

    Android应用源码之多级PopupWindow的小demo-IT计算机-毕业设计.zip

    这个“Android应用源码之多级PopupWindow的小demo”是一个很好的学习资源,特别适合进行毕业设计或者论文研究的同学们。下面将详细解析这个Demo所涉及的知识点。 1. **PopupWindow基本概念** PopupWindow是Android...

    android官网demo之animations。zip

    - 使用`&lt;animation-list&gt;`在XML中定义一系列的帧图片,通过`AnimationDrawable`来播放这些帧,实现类似GIF的动画效果。 4. **动画监听器(AnimationListeners)**: - 可以添加`Animation.AnimationListener`来...

    Xamarin.Forms Android闪屏 APP启动屏图片

    1. **创建启动Activity**:在Xamarin.Android项目中,新建一个继承自`Android.App.Activity`的类,例如`SplashActivity`。在这个类中,我们将设置闪屏页面的布局和显示时间。 2. **设计闪屏布局**:使用XML文件创建...

    Android应用源码之(Animation动画)-IT计算机-毕业设计.zip

    这个压缩包"Android应用源码之(Animation动画)"提供了Android应用中动画效果的源码示例,非常适合那些正在进行毕业设计或者想要深入理解Android动画机制的学生进行学习。在本文中,我们将详细探讨Android动画系统...

    android应用源码动画文字自由移动-IT计算机-毕业设计.zip

    这个"android应用源码动画文字自由移动"的项目提供了一个实际的Demo,非常适合学习者理解和掌握Android平台上的动态效果实现。下面将详细介绍这个项目可能涉及的关键知识点。 1. **Android动画系统**:Android提供...

    activity 动画跳转demo (仿大众点评)

    本Demo旨在模拟大众点评App中的Activity动画跳转效果,通过自定义动画实现平滑、自然的过渡。 首先,我们需要了解Android中两种基本的Activity跳转动画类型:进入动画(Enter Animation)和退出动画(Exit ...

    黑马程序员 安卓学院 万元哥项目经理 分享220个代码实例

    |--Activity不允许横竖屏切换 |--Activity常用小技巧 |--Activity按返回直接回到桌面 |--aidl之结合反射获取应用缓存大小等空间占用 |--aidl调用系统service未公开的方法挂电话 |--aidl调用系统未公开的方法代码示例...

    android开发资料大全

    两分钟彻底让你明白Android Activity生命周期(图文)! Android 图形系统剖析 Android 立体效果图片 NDK动态库的调用 Android 姿态传感器 Android 很酷的图像旋转 Android 添加音频 在Android中实现多线程断点下载 ...

    仿桌面删除app Demo

    综上所述,"仿桌面删除app Demo"是一个涵盖Android应用开发、自定义启动器实现、权限管理、数据存储、事件处理、动画效果等多个方面的项目,对于希望深入学习Android系统的开发者来说,这是一个很好的实践案例。

Global site tag (gtag.js) - Google Analytics