Android学习之Animation(三)
来源:互联网 发布:windows正版镜像 编辑:程序博客网 时间:2024/06/05 02:41
今天观看了一个关于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(三)
- Android动画之Property Animation(三)
- Android学习之Animation
- Android学习之Animation
- android学习之animation
- Android Animation(三)
- 【Android】Animation(三)
- Android学习之Animation(一)
- Android学习之Animation(二)
- Android animation学习笔记之propery animation
- Android Animation学习之 Animation概述
- Android动画学习总结(三)------- property animation
- Android动画学习-Animation动画总述(三)
- Android学习笔记之Animation
- Android Animation学习之 ApiDemos
- Android动画之属性动画(Property Animation)(三)
- Android动画(三)之属性动画(Property Animation)
- Android动画之三:Property Animation 完全解析 (上)
- springmvc配置
- 实际上Java访问数据库的方式主要有四种
- codeforces 581d
- android学习之选择媒体库中的图片
- jfinal 学习笔记
- Android学习之Animation(三)
- 执行django-admin.py时,打开编辑器的解决办法
- 【ASP】利用数据库操作类优化数据库表的增删改查
- java入门学习(11)—接口定义(interface)及使用(implements)
- ORA-02080:database link is in use
- System.arraycopy
- onPrepareOptionsMenu 和onCreateOptionsMenu 的区别
- folder汇总字段的实现
- Spring4.2第一个小例子