今天观看了一个关于android动画的一些知识,就顺便记录下来,以备之后的学习和参考。


在XML文件中使用LayoutAnimationController


第一步:

在res/anim文件夹下创建一个xml文件,如list_layout_animation.xml.代码的内容如下面的简单的示例:

<layoutAnimation
    xmlns:android="http://schemas.android.com/res/apk/android"
    android:delay="0.5"
    android:animationOrder="random"
    android:animation="@anim/alpha"
    />

其中animation属性对应的值就是添加的动画资源文件。


第二步:

在ListView的xml声明文件中的layoutAnimation属性上进行设置即可完成添加动画的这一效果。

<ListView
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layoutAnimation="@anim/list_layout_animation"
        />

这就是通过xml文件控制的方式来实现的效果!

在代码中进行控制


第一步:

创建一个Animation对象:既可以通过xml文件装载,也可以直接使用Animation的构造函数创建Animation。


第二步:

使用如下代码创建LayoutAnimationController对象LayoutAnimationController lac=new LayoutAnimationController(animation);


第三步:

设置空间的显示的顺序:

lac.setOrder(LayoutAnimationCOntroller.ORDER_NORMAL);

第四步:

为ListView设置LayoutAnimationController属性,listView.setLayoutAnimation(Lac);


这样便也可以完成用代码的方式对动画的设置。


AnimationListener的学习


首先,顾名思义它是一个监听器,而且是一个监听动画的执行效果的和执行时间的监听器(在不同的时段会得到不同的系统的通知,从而调用相关的方法完成一些逻辑的操作!)。主要有如下几个方法

  • onAnimationStart(Animation animation)
  • onAnimationRepeat(Animation animation)
  • onAnimationEnd(Animation animation)

下面是一个简单的例子,代码的思路很清晰,就不在做注释了。

package com.sumer.animationutils;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity {

    private Button removeButton=null;
    private Button addButton=null;
    private ImageView mImageView=null;
    private ViewGroup mViewGroup=null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        removeButton=(Button) findViewById(R.id.removeButtonId);
        removeButton.setOnClickListener(new RemoveButtonListener());
        addButton=(Button) findViewById(R.id.addButtonId);
        addButton.setOnClickListener(new AddButtonListener());

        mViewGroup=(ViewGroup) findViewById(R.id.layoutId);
        mImageView=(ImageView) findViewById(R.id.imageViewId);
    }

    private class AddButtonListener implements OnClickListener{

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            AlphaAnimation animation=new AlphaAnimation(0.0f,1.0f);
            animation.setDuration(1500);
            animation.setStartOffset(500);
            ImageView imageViewAdd=new ImageView(MainActivity.this);
            imageViewAdd.setImageResource(R.drawable.ic_launcher);
            mViewGroup.addView(imageViewAdd, new LayoutParams(
                    LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
            imageViewAdd.startAnimation(animation);
        }
    }
    private class RemoveButtonListener implements OnClickListener{

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            AlphaAnimation animation=new AlphaAnimation(1.0f,0.0f);
            animation.setDuration(1500);
            animation.setStartOffset(500);
            animation.setAnimationListener(new RemoveAnimationListener());
            mImageView.startAnimation(animation);
        }

    }

    private class RemoveAnimationListener implements AnimationListener{

        @Override
        public void onAnimationEnd(Animation arg0) {
            // TODO Auto-generated method stub
            System.out.println("END!!!!!!!!!!!!!!!!!!!");
            mViewGroup.removeView(mImageView);
        }

        @Override
        public void onAnimationRepeat(Animation arg0) {
            // TODO Auto-generated method stub
            System.out.println("REPEAT!!!!!!!!!!!!!!!!");
        }

        @Override
        public void onAnimationStart(Animation arg0) {
            // TODO Auto-generated method stub
            System.out.println("START!!!!!!!!!!!!!!!!!");
        }

    }

}

下面是布局界面(注意是RelativeLayout)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layoutId"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"  >

    <Button
        android:id="@+id/addButtonId"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="Add Picture!"
        />
    <Button
        android:id="@+id/removeButtonId"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/addButtonId"
        android:text="Remove Picture!"
        />
    <ImageView
        android:id="@+id/imageViewId"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_marginTop="100dip"
        android:src="@drawable/ic_launcher"
        />

</RelativeLayout>

总结:

对于Android的Animation的操作,是一个很有意思的小知识点。其中需要注意的是怎样将这些效果组合起来,形成一个优雅的界面,并能给用户更加舒服的用户体验。这才是我们最需要,也是最值得掌握的地方!

Android学习之Animation(三)的更多相关文章

  1. 【转】 Pro Android学习笔记(三二):Menu(3):Context菜单

    目录(?)[-] 什么是Context menu 注册View带有Context menu 填Context菜单内容 Context菜单点击触发 什么是Context menu 在桌面电脑,我们都很熟 ...

  2. 【转】Pro Android学习笔记(三十):Menu(1):了解Menu

    目录(?)[-] 创建Menu MenuItem的属性itemId MenuItem的属性groupId MenuItem的属性orderId MenuItem的属性可选属性 Menu触发 onOpt ...

  3. 【转】Pro Android学习笔记(三):了解Android资源(上)

    在Android开发中,资源包括文件或者值,它们和执行应用捆绑,无需在源代码中写死,因此我们可以改变或替换他们,而无需对应用重新编译. 了解资源构成 参考阅读Android学习笔记(三八):资源res ...

  4. Android学习记录(三)——安装SQLite

    这次学习安装SQLite. 一.SQLite简介 重要特性:零配置,即不需要复杂的配置即可使用 详细:https://www.runoob.com/sqlite/sqlite-intro.html 二 ...

  5. Android学习笔记(三) UI布局

    每一个布局都有其适合的方式,另外,这几个布局元素可以相互嵌套应用,做出美观的界面. 一.线性布局(LinearLayout) 线性布局,这个东西,从外框上可以理解为一个div,他首先是一个一个从上往下 ...

  6. Android学习笔记(三)——初探Intent

    //此系列博文是<第一行Android代码>的学习笔记,如有错漏,欢迎指正! Intent 是 Android 程序中各组件之间进行交互的一种重要方式,它不仅可以指明当前组件想要执行的动作 ...

  7. Android学习之Animation(二)

    接着上次的View Animation动画,这次是Frame Animation.具体点来讲就是在Frame层面上进行变化的动画效果的设置.说白了就是定时更换"背景"图.来实现不同 ...

  8. Android学习之Animation(一)

    3.0以前,android支持两种动画模式,Tween Animation,Frame Animation,在android3.0中又引入了一个新的动画系统:Property Animation,这三 ...

  9. Android学习笔记——Menu(三)

    知识点 今天继续昨天没有讲完的Menu的学习,主要是Popup Menu的学习. Popup Menu(弹出式菜单) 弹出式菜单是一种固定在View上的菜单模型.主要用于以下三种情况: 为特定的内容提 ...

随机推荐

  1. google-gson 解析json

    http://www.cnblogs.com/jianyungsun/p/6647203.html 在JSON官网我们可以查看到各个语法对json的支持,对于java来说比较成熟的是google-gs ...

  2. 查询优化--ORDER BY查询优化

    Mysql 系列文章主页 =============== ORDER BY 子句,尽量使用 Index 查询,避免使用 FileSort 排序 尽可能在索引列上完成排序操作,遵照索引的最佳左前缀原则 ...

  3. [原创]手把手教你写网络爬虫(7):URL去重

    手把手教你写网络爬虫(7) 作者:拓海 摘要:从零开始写爬虫,初学者的速成指南! 封面: 本期我们来聊聊URL去重那些事儿.以前我们曾使用Python的字典来保存抓取过的URL,目的是将重复抓取的UR ...

  4. 传统方法过渡到ES6去优雅地实现JavaScript的继承

    众所周知,面向对象编程有三个重要的概念: 封装.继承.多态.而JS作为面向对象的弱类型语言,应该说是基于对象的语言,正如常说的,JS的世界里,万物皆对象.虽然JS本身不是面向对象的语言,我们可以通过模 ...

  5. Spring Boot消息队列应用实践

    消息队列是大型复杂系统解耦利器.本文根据应用广泛的消息队列RabbitMQ,介绍Spring Boot应用程序中队列中间件的开发和应用. 一.RabbitMQ基础 1.RabbitMQ简介 Rabbi ...

  6. Codeforces Round #305 (Div. 2) B. Mike and Fun 暴力

     B. Mike and Fun Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/548/pro ...

  7. android自定义View之3D索引效果

    效果图: 我的小霸王太卡了. 最近工作比较忙,今天搞了一下午才搞出来这个效果,这种效果有很多种实现方式,最常见的应该是用贝塞尔曲线实现的.今天我们来看另一种不同的实现方式,只需要用到 canvas.s ...

  8. 【mybatis深度历险系列】mybatis中的输入映射和输出映射

    在前面的博文中,小编介绍了mybatis的框架原理以及入门程序,还有mybatis中开发到的两种方法,原始开发dao的方法和mapper代理方法,今天博文,我们来继续学习mybatis中的相关知识,随 ...

  9. 从Stage角度看cassandra write

    声明 文章发布于CSDN cassandra concurrent 具体实现 cassandra并发技术文中介绍了java的concurrent实现,这里介绍cassandra如何基于java实现ca ...

  10. RxJava(10-操作符原理&自定义操作符)

    转载请标明出处: http://blog.csdn.net/xmxkf/article/details/51791120 本文出自:[openXu的博客] 目录: 自定义创建操作符 数据序列操作符li ...