- 浏览: 65981 次
- 性别:
- 来自: Mercury
最新评论
一、LayoutAnimationsContrlller的使用方法
LayoutAnimationsContrlller可以用于实现使多个控件按顺序一个一个的显示。
1)LayoutAnimationsContrlller用于为一个layout里面的控件,或者是一个ViewGroup里面的控件设置动画效果。
2)每一个控件都有相同的动画效果。
3)控件的动画效果可以在不同的时间显示出来。
4)LayoutAnimationsContrlller可以在xml文件当中设置,以可以在代码当中进行设置。
二、ListView与Animaions结合使用
1.在xml当中使用LayoutAnimationsController
1)在res/anim文件夹下创建一个名为list_anim_layout.xml文件:
android:dylay - 动画间隔时间;
android:animationOrder - 动画执行的循序(normal:顺序,random:随机,reverse:反向显示)
android:animation – 引用动画效果文件
<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android" android:delay="0.5" android:animationOrder="normal" android:animation="@anim/list_anim"/>
2)在布局文件当中为ListVIew添加如下配置:
android:layoutAnimation="@anim/list_anim_layout"
完整代码:
List_anim_layout.xml
<?xml version="1.0" encoding="utf-8"?> <layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android" android:delay="0.5" android:animationOrder="normal" android:animation="@anim/list_anim"/>
List_anim.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"> <alpha android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="1000"/> </set>
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" > <ListView android:id="@id/android:list" android:layout_width="fill_parent" android:layout_height="wrap_content" android:scrollbars="vertical" android:layoutAnimation="@anim/list_anim_layout"/> <Button android:id="@+id/button" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="测试"/> </LinearLayout>
Item.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" android:paddingLeft="10dip" android:paddingRight="10dip" android:paddingTop="1dip" android:paddingBottom="1dip"> <TextView android:id="@+id/name" android:layout_width="180dip" android:layout_height="30dip" android:textSize="5pt" android:singleLine="true" /> <TextView android:id="@+id/sex" android:layout_width="fill_parent" android:layout_height="fill_parent" android:textSize="5pt" android:singleLine="true"/> </LinearLayout>
AnimationsActivity.java
package com.android.activity; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import android.app.ListActivity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ListAdapter; import android.widget.ListView; import android.widget.SimpleAdapter; public class AnimationsActivity extends ListActivity { private Button button = null; private ListView listView = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); listView = getListView(); button = (Button)findViewById(R.id.button); button.setOnClickListener(new ButtonListener()); } private ListAdapter createListAdapter() { List<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>(); HashMap<String,String> m1 = new HashMap<String,String>(); m1.put("name", "bauble"); m1.put("sex", "male"); HashMap<String,String> m2 = new HashMap<String,String>(); m2.put("name", "Allorry"); m2.put("sex", "male"); HashMap<String,String> m3 = new HashMap<String,String>(); m3.put("name", "Allotory"); m3.put("sex", "male"); HashMap<String,String> m4 = new HashMap<String,String>(); m4.put("name", "boolbe"); m4.put("sex", "male"); list.add(m1); list.add(m2); list.add(m3); list.add(m4); SimpleAdapter simpleAdapter = new SimpleAdapter( this,list,R.layout.item,new String[]{"name","sex"}, new int[]{R.id.name,R.id.sex}); return simpleAdapter; } private class ButtonListener implements OnClickListener{ public void onClick(View v) { listView.setAdapter(createListAdapter()); } } }
运行结果:每一个item都是淡入淡出的按顺序显示。
2.在代码当中使用LayoutAnimationsController
对于在代码中使用LayoutAnimationsController,只不过去掉了list_anim_layout.xml这个文件,以及listview当中的
android:layoutAnimation="@anim/list_anim_layout"
这句。将animation的布局设置更改到了ButtonListener代码当中进行。
1) 创建一个Animation对象:可以通过装载xml文件,或者是直接使用Animation的构造方法创建Animation对象;
Animation animation = (Animation)AnimationUtils.loadAnimation( AnimationsActivity.this, R.anim.list_anim);
2) 创建LayoutAnimationController对象:
LayoutAnimationController controller = new LayoutAnimationController(animation);
3) 设置控件的显示顺序以及延迟时间:
controller.setOrder(LayoutAnimationController.ORDER_NORMAL); controller.setDelay(0.5f);
4) 为ListView设置LayoutAnimationController属性:
listView.setLayoutAnimation(controller);
三、AnimationListener的使用方法
1.AnimationListener是一个监听器,该监听器在动画执行的各个阶段会得到通知,从而调用相应的方法;
2.AnimationListener主要包括如下三个方法:
·onAnimationEnd(Animation animation) - 当动画结束时调用
·onAnimationRepeat(Animation animation) - 当动画重复时调用
·onAniamtionStart(Animation animation) - 当动画启动时调用
实例:
Main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/layout" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <Button android:id="@+id/addButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:text="添加图片" /> <Button android:id="@+id/deleteButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_above="@id/addButton" android:text="删除图片" /> <ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:layout_marginTop="100dip" android:src="@drawable/image" /> </RelativeLayout>
AnimationListenerActivity.java
package com.android.activity; 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 AnimationListenerActivity extends Activity { private Button addButton = null; private Button deleteButton = null; private ImageView imageView = null; private ViewGroup viewGroup = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); addButton = (Button)findViewById(R.id.addButton); deleteButton = (Button)findViewById(R.id.deleteButton); imageView = (ImageView)findViewById(R.id.image); //LinearLayout下的一组控件 viewGroup = (ViewGroup)findViewById(R.id.layout); addButton.setOnClickListener(new AddButtonListener()); deleteButton.setOnClickListener(new DeleteButtonListener()); } private class AddButtonListener implements OnClickListener{ public void onClick(View v) { //淡入 AlphaAnimation animation = new AlphaAnimation(0.0f, 1.0f); animation.setDuration(1000); animation.setStartOffset(500); //创建一个新的ImageView ImageView newImageView = new ImageView( AnimationListenerActivity.this); newImageView.setImageResource(R.drawable.image); viewGroup.addView(newImageView, new LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); newImageView.startAnimation(animation); } } private class DeleteButtonListener implements OnClickListener{ public void onClick(View v) { //淡出 AlphaAnimation animation = new AlphaAnimation(1.0f, 0.0f); animation.setDuration(1000); animation.setStartOffset(500); //为Aniamtion对象设置监听器 animation.setAnimationListener( new RemoveAnimationListener()); imageView.startAnimation(animation); } } private class RemoveAnimationListener implements AnimationListener{ //动画效果执行完时remove public void onAnimationEnd(Animation animation) { System.out.println("onAnimationEnd"); viewGroup.removeView(imageView); } public void onAnimationRepeat(Animation animation) { System.out.println("onAnimationRepeat"); } public void onAnimationStart(Animation animation) { System.out.println("onAnimationStart"); } } }
运行结果:
删除时慢慢淡出,添加时慢慢淡入
发表评论
文章已被作者锁定,不允许评论。
-
Android40_Dialog
2011-11-14 00:11 2943Dialog是Android常用的对话框控件。AlertDia ... -
Android39_Clock和TimePicker
2011-11-14 00:08 2317一、AnalogClock和DigitalClock ... -
Android38_ImageView和Gallery
2011-11-14 00:07 3558一、ImageView使用方法 ImageVi ... -
Android37_JSON数据解析
2011-11-08 00:14 2304一、JSON介绍 JSON(JavaSc ... -
Android35_Animations使用(三)
2011-11-08 00:13 2609一、AnimationSet的具体使用方法 ... -
Android34_Animations使用(二)
2011-11-08 00:12 1919在代码中使用Animations可以很方便的调试、运行 ... -
Android33_Animations使用(一)
2011-11-08 00:12 2246一、Animations介绍 Anima ... -
Android31_AppWidget使用(二)
2011-11-05 00:09 2462一、PendingIntent介绍 PendingIn ... -
Android30_AppWidget使用(一)
2011-11-05 00:08 2227一、App Widget定义 App ... -
Android32_Notification用法
2011-11-05 00:09 1841Android系统的状态栏(Status Bar)中有一 ... -
Android29_SeekBar和RatingBar
2011-11-02 23:21 2065一、使用SeekBar步骤: SeekB ... -
Android28_ExpandableListActivity
2011-11-02 23:21 1456ExpandableListActivity就是可扩展的 ... -
Android27_AutoCompleteTextView
2011-11-02 23:21 1029一、创建AutoCompleteTextView ... -
Android26_DatePicker
2011-11-02 23:20 1746一、DatePicker和DatePickerDialo ... -
Android25_Spinner使用方法
2011-11-02 23:20 2772一、创建Spinner的步骤 1.在布局 ... -
Android24_Service初步
2011-10-18 22:27 965一、Service概念 ... -
Android23_Socket编程
2011-10-18 22:19 1467一、什么是Socket Socket是基 ... -
Android22_WIFI网络操作
2011-10-18 22:12 1652一、什么是WIFI WIFI就是一种无线 ... -
Android21_广播机制(二)
2011-10-18 22:00 964一、注册BroadcastReceiver的方法 ... -
Android20_广播机制(一)
2011-10-18 21:48 1047一、Android广播机制介绍 Android:操作系统 ...
相关推荐
Android-android_additive_animations.zip,Android附加动画!,安卓系统是谷歌在2008年设计和制造的。操作系统主要写在爪哇,C和C 的核心组件。它是在linux内核之上构建的,具有安全性优势。
android-sdk-sources-android-28.rar
android_animations:Android动画-准备使用XML文件
在此处获得此库的良好概述: : 一体化要在您的项目中使用AdditiveAnimator ,请在build.gradle添加以下几行: dependencies { compile 'at.wirecube:additive_animations:1.9.0'}...repositories { jcenter()}快速...
Animations Beautiful animations from AOSP 依賴 Add it in your root build.gradle at the end of repositories: allprojects { repositories { ... maven { url 'https://jitpack.io' } } } 查詢最新...
For making animations more real, I created another project named Android Easing Functions which is an implementations of easing functions on Android. So, we need to dependent that project. Step 1 ...
详细解释了Android的animation四种类型:alpha、scale、translate、rotate,及其在代码中的获取与使用
01_FirstApp 02_MVC 03_ActivityLifecycle 05_SecondActivity 06_AndroidVersions 07_UIFragments 08_LayoutsAndWidgets 09_RecyclerView ...30_Animations 31_Locations 32_Maps 33_MaterialDesignTopics
关于这个项目 PERSONAL Android 开发学习过程中,有两处重要的代码资料来源,一是 Android Training 及其 ...Android Training - Adding Animations,演示如何向应用中添加一些常见的动画,分为5个部分,包括详细的
Android 3.0 Animations Beginner’s Guide Android 3.0 中动画的操作
[android.开发书籍].Android.3.0.Animations.pdf
nineoldandroids 界面库 实习
RxAnimations is a library with the main goal to make android animations more solid and cohesive. Download compile 'oxim.digital:rx2anim:0.9.1' compile 'io.reactivex.rxjava2:rxandroid:2.0.1' ...
Packt.Android.3.0.Animations.Beginner's.Guide
本来想不要分的,但是最近的确有点缺分,我把android官网demo之animations.zip的拿出来看了一遍,然后整了一个eclipse可用的
This is a Calendar app with various animations
Python script to create android boot animations gifs
主要介绍了Android开发之Animations动画用法,结合实例形式详细分析了Animations动画的类型、组成、模式及对应的使用技巧,需要的朋友可以参考下
它是一款实现了各种Material风格动画效果的开源项目,包括Activity之间的跳转动画,View的动画等。
android-view-animations-java,Awesome Library可使用视图动画系统执行倾斜动画,如关注、反弹、淡入、翻转、旋转、滑动和缩放视图。.zip