`
007007jing
  • 浏览: 41193 次
  • 性别: 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代码-dlib-android-app

    dlib-android-app See http://dlib.net for the main project documentation. See dlib-android for JNI lib. Refer to dlib-android/jni/jnilib_ex Grap the source $ git clone ...

    安卓串口demo(android-serialport-api-android-serialport-api

    安卓串口Demo程序 基于谷歌提供的源码android-serialport-api-android-serialport-api

    React-Native官方demo(对应 最新v0.55.0版本20180620编译)

    2018年6月20最新编译React-Native官方demo,可直接运行版本。对应执行如下命令: 1.D:\reactnative\RNTester&gt; react-native bundle --platform android ...2.D:\reactnative\RNTester\android&gt;gradlew :app:installDebug

    Android代码-cordova-plugin-app-update

    cordova-plugin-app-update App updater for Cordova/PhoneGap Demo Try it yourself: Just clone and install this demo. cordova-plugin-app-update-DEMO :tada: 如果喜欢它,请别忘了给我一颗鼓励的星 Support ...

    android-serialport-api 串口demo.zip

    这是很据android-serialport-api 自己简化的一个demo ,可以使用。原来android-serial-api的程序很多人反映都不能使用,所以自己写了这个,只有一个activity,可以做为你的学习参考。

    WebRTCDemo-master_android-studio

    WebRTCDemo-master_android-studio,webrtcdemo,p2p,android-studio编译

    upload-download

    准备工作$ npm install启动服务找到对应的实例代码,然后启动 所在目录的 server/app.js如node ./src/upfiles-demo/demo-a1-form/server/app.jsnode ./src/upfiles-demo/demo-a2-formdata/server/app.jsnode ./src/...

    基于mAppwidget实现的手绘地图demo

    可使用不同的图片作为地图...//下载demo http://mapp.android-libraries.com/download //学习地图 http://www.67tgb.com/?p=610 图片切割 http://www.67tgb.com/?p=597 http://mapp.android-libraries.com/slicingtool/

    Android Camera2 Demo - 实现相机预览、拍照、录制视频功能

    因为工作中要使用 Android Camera2 API 来实现相机功能,但因为Camera2比较复杂,网上资料也比较乱,有一定入门门槛,所以花了几天时间系统研究了下,这个项目就是我研究的成果。 其中包括一个自己写的Camera2的Demo...

    Android语音识别示例

    由于最近在做智能家居方向的产品,需要在App上对机器人实现一个简单的语音控制,于是开始寻找相应的解决方案,由于某种原因,google自己的语音识别API并不能在国内发挥作用,所以我们选择国内的科大讯飞语音识别服务...

    Android-Animation demo

    Provide Android animation API/Demo

    android 项目 开发 Demo

    android 项目 开发 demo &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.demo" android:versionCode="1" android:versionName="1.0" &gt; &lt;uses-sdk android:...

    Android代码-安卓串口打开、接收与发送

    Android串口操作,简化android-serialport-api的demo(附源码) 我把文中的源码导入 android studio 源码来自谷歌:android-serialport-api 涉及到的 jni 开发可以参考:【Android 应用开发】Android 开发 之 JNI...

    SAML协议交互,实现工程Demo(有注释)

    OpenSAML-ref-project-demo-v3 这一个使用OpenSAML库的示例项目: 一个很简单的网址,其充当SP;同时该项目还包括一个很简单的IDP; SAML协议的交互将在这二者之间展开。项目启动之后,访问如下网址: ...

    Face-api.js静态页面版Demo

    JavaScript人脸识别库Face-api.js的示例,无需安装nodejs,iis本地直接看效果。注意调用摄像头不能用IP访问,只能localhost,远程预览需要HTTPS;iis无扩展名文件若出现404,需在mime类型中添加扩展名【.】类型...

    ci-demo-app-homebrew-tap:CI演示应用程序的自制方法

    ci-demo-app-homebrew-tap CI演示应用程序的自制方法 安装 注册水龙头: brew tap embano1/ci-demo-app git@github.com:embano1/ci-demo-app-homebrew-tap 注意:如果您没有使用ssh设置git,您也可以使用...

    Android SipDemo( 网络电话的例子源码,android 2.3以上才能用)

    这是android 2.3及2.3以后才能用的Sip (网络电话)相关接口使用的例子,实现了基本的网络电话功能(呼叫及被呼叫),用的是Android提供的编程接口。

    Android代码-DroidDrop–Remote-Logging

    DroidDrop - Remote Logging DroidDrop Remote logging give Android Developers the ability to log data from their applications to a drop on drop.io. ...Get a developer API Key from drop.io (free

    android模拟launcher公司项目demo源码-2011-8-2

    android模拟launcher公司项目demo源码:可以学习到 自定义组件(图片和文字帮定),启动其它包内的程序.....................希望同仁好好利用!

    android demo api

    android demo 的例子 用来学习android的效果 ,view 都是很好的帮助 还是中文的 通俗易懂 谢谢

Global site tag (gtag.js) - Google Analytics