android Animation的使用

本文详细介绍了Android中AnimationSet的使用方法,包括如何创建、设置属性及使用不同类型的Interpolator。同时,展示了Frame-By-Frame Animation的实现方式,通过XML文件和Java代码演示了动态图像的制作过程。

转自:http://blog.sina.com.cn/s/blog_5688414b0100yl27.html


一、AnimationSet的具体使用方法

       1.AnimationSetAnimation的子类;

       2.一个AnimationSet包含了一系列的Animation

       3.针对AnimationSet设置一些Animation的常见属性(如startOffsetduration等),可以被包含在AnimationSet当中的Animation集成;

例:一个AnimationSet中有两个Animation,效果叠加

第一种方法:

doubleani.xml

<?xml version="1.0" encoding="utf-8"?>

<set xmlns:android="http://schemas.android.com/apk/res/android"

    android:interpolator="@android:anim/accelerate_interpolator"

    android:shareInterpolator="true">

    <!-- fromAlphatoAlpha是起始透明度和结束时透明度 -->

    <alpha

        android:fromAlpha="1.0"

        android:toAlpha="0.0"

        android:startOffset="500"

        android:duration="500"/>

    <translate

        android:fromXDelta="0%"

        android:toXDelta="100%"

        android:fromYDelta="0%"

        android:toYDelta="100%"

        android:duration="2000"/>

</set>

.java文件中

classDoubleButtonListener implements OnClickListener {

       public void onClick(View v) {

           // 使用AnimationUtils装载动画配置文件

           Animation animation = AnimationUtils.loadAnimation(

                  Animation2Activity.this, R.anim. doubleani);

           // 启动动画

           image.startAnimation(animation);

       }

    }

第二种方法:

.java文件中

classDoubleButtonListener implements OnClickListener {

       public void onClick(View v) {

           AnimationSet animationSet = new AnimationSet(true);

           AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);

           RotateAnimation rotateAnimation = new RotateAnimation(0, 360,

                  Animation.RELATIVE_TO_SELF,0.5f,

                  Animation.RELATIVE_TO_SELF,0.5f);

           rotateAnimation.setDuration(1000);

           animationSet.addAnimation(rotateAnimation);

           animationSet.addAnimation(alphaAnimation);

           image.startAnimation(animationSet);

 

       }

    }

二、Interpolator的具体使用方法

       Interpolator定义了动画变化的速率,在Animations框架当中定义了一下几种Interpolator

Ø         AccelerateDecelerateInterpolator:在动画开始与结束的地方速率改变比较慢,在中间的时候速率快。

Ø         AccelerateInterpolator:在动画开始的地方速率改变比较慢,然后开始加速

Ø         CycleInterpolator:动画循环播放特定的次数,速率改变沿着正弦曲线

Ø         DecelerateInterpolator:在动画开始的地方速率改变比较慢,然后开始减速

Ø         LinearInterpolator:动画以均匀的速率改变

分为以下几种情况:

1、在set标签中

<set xmlns:android="http://schemas.android.com/apk/res/android"

    android:interpolator="@android:anim/accelerate_interpolator"/>

2、如果在一个set标签中包含多个动画效果,如果想让这些动画效果共享一个Interpolator

    android:shareInterpolator="true"

3、如果不想共享一个interpolator,则设置android:shareInterpolator="true"并且需要在每一个动画效果处添加interpolator

<alpha

        android:interpolator="@android:anim/accelerate_decelerate_interpolator"

        android:fromAlpha="1.0"

        android:toAlpha="0.0"

        android:startOffset="500"

        android:duration="500"/>

4、如果是在代码上设置共享一个interpolator则可以在AnimationSet设置interpolator

AnimationSet animationSet = newAnimationSet(true);

animationSet.setInterpolator(new AccelerateInterpolator());

5、如果不设置共享一个interpolator则可以在每一个Animation对象上面设置interpolator

AnimationSet animationSet = newAnimationSet(false);

alphaAnimation.setInterpolator(new AccelerateInterpolator());

rotateAnimation.setInterpolator(new DecelerateInterpolator());

三、Frame-By-Frame Animations的使用方法

       Frame-By-Frame Animations是一帧一帧的格式显示动画效果。类似于电影胶片拍摄的手法。

1、  文件结构

Android学习二_十:Animation的使用(三)

2、  main.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent">

    <LinearLayout

        android:orientation="horizontal"

        android:layout_height="wrap_content"

        android:layout_width="wrap_content">

       <Button

           android:id="@+id/button"

               android:layout_width="wrap_content"

               android:layout_height="wrap_content"

               android:text="运动"/>

    </LinearLayout>

    <LinearLayout

        android:orientation="vertical"

        android:layout_width="fill_parent"

        android:layout_height="fill_parent">

       <ImageView

           android:id="@+id/image"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:layout_centerInParent="true"/>

    </LinearLayout>

</LinearLayout>

3、anim.xml

<?xml version="1.0" encoding="utf-8"?>

<animation-list xmlns:android="http://schemas.android.com/apk/res/android"

    android:oneshot="false">

    <item android:drawable="@drawable/a_01" android:duration="50"/>

    <item android:drawable="@drawable/a_02" android:duration="50"/>

    <item android:drawable="@drawable/a_03" android:duration="50"/>

    <item android:drawable="@drawable/a_04" android:duration="50"/>

    <item android:drawable="@drawable/a_05" android:duration="50"/>

    <item android:drawable="@drawable/a_06" android:duration="50"/>

</animation-list>

4.java文件

importandroid.app.Activity;

importandroid.graphics.drawable.AnimationDrawable;

importandroid.os.Bundle;

importandroid.view.View;

importandroid.view.View.OnClickListener;

importandroid.widget.Button;

importandroid.widget.ImageView;

public class AnimationsActivity extends Activity {

    private Button button = null;

    private ImageView imageView = null;

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        button = (Button)findViewById(R.id.button);

        imageView = (ImageView)findViewById(R.id.image);

        button.setOnClickListener(newButtonListener());

    }

    class ButtonListener implementsOnClickListener{

       public void onClick(View v) {

           imageView.setBackgroundResource(R.anim.anim);

           AnimationDrawable animationDrawable = (AnimationDrawable)

              imageView.getBackground();

           animationDrawable.start();

       }

    }

}

 

内容概要:本文详细介绍了一个基于C++的城市停车需求分析平台的设计与实现,旨在通过科学化手段解决城市停车资源紧张、管理低效等问题。平台采用模块化架构,涵盖数据采集与融合、实时流式处理、数据存储与管理、智能分析与预测、可视化交互、安全防护、系统集成及运维监控八大核心模块。通过C++高性能特性支持高并发、实时数据处理与复杂算法运算,结合时间序列预测(如加权移动平均)、聚类分析(KMeans)等算法实现停车需求预测与热点区域识别,并提供开放API接口支持系统扩展与外部集成。文中还给出了关键模块的C++代码示例,包括数据清洗、多线程处理、数据查询与权限管理等。; 适合人群:具备C++编程基础、熟悉数据结构与算法的软件开发人员、城市交通系统研究人员及智慧城市相关领域的技术人员,尤其适合从事大数据处理、智能交通系统开发的1-5年经验从业者; 使用场景及目标:①构建高性能城市级停车资源管理平台,实现停车数据的实时采集、分析与预测;②为政府提供科学决策支持,优化停车设施布局与交通政策;③提升市民出行效率,减少道路拥堵与碳排放;④作为智慧城市建设中交通子系统的参考架构与技术实现方案; 阅读建议:此资源不仅提供完整的系统设计思路与模型描述,还包含可运行的关键代码片段,建议读者结合实际开发环境动手实践,深入理解各模块间的协同机制,并在此基础上进行功能扩展与性能优化,以适应不同城市规模与业务需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值