Android Property Animator (属性动画)第二篇
2016-04-05 18:05
375 查看
Android Property Animator (属性动画)第一篇了解到了
首先在
我们只需要在
那么你肯定会想呢,只支持单纯单一的动画吗?如果我需要多动画呢?
使用set标签,
我们来看看效果图:
![](http://img.blog.csdn.net/20160406141244758)
使用之前,我们要来了解下
LayoutTransition.APPEARING :一个View在ViewGroup中出现,该View的动画
LayoutTransition.CHANGE_APPEARING :一个View在ViewGroup中出现,该View对该ViewGroup中其他View位置造成影响,其他View的动画
LayoutTransition.DISAPPEARING :一个View在ViewGroup中消失,该View的动画
LayoutTransition.CHANGE_DISAPPEARING :一个View在ViewGroup中消失,该View对该ViewGroup中其他View位置造成影响,其他View的动画
这里重点区分加粗字体的含义,我们还是通过一个例子具体来看一下:
布局文件:
Activity代码:
效果图:
![](http://img.blog.csdn.net/20160406150310855)
属性动画是不是很很容易实现,你也动手试试吧,效果肯定很棒。属性动画第二篇和第一篇都是一些基础知识,你如果感兴趣做出一些比较拉风,绚丽的动画,我们可以一起研究。
有什么疑问,什么地方写得不当的地方,还请多多指出。
Property Animator实现动画的基本流程。这篇主要讲解布局动画以及做出简单的动画效果。
1、如何使用xml文件来创建属性动画
大家都知道,View Animator 、Drawable Animator都可以在res/anim文件夹下创建动画。那么 Property Animator 也可以在文件中申明:首先在
res文件夹创建
animator文件夹,然后在
animator文件夹下创建自己
xml文件。如
res/animator/scale_anim.xml
<?xml version="1.0" encoding="utf-8"?> <objectAnimator xmlns:android="http://schemas.android.com/apk/res/android" android:duration="2000" android:propertyName="rotationX" android:valueFrom="0.0" android:valueTo="360.0" android:valueType="floatType"></objectAnimator>
我们只需要在
Activity中这么调用:
public void bntXml(View v) { Animator animator = AnimatorInflater.loadAnimator(this, R.animator.scale_anim); animator.setTarget(ivFace); animator.start(); }
那么你肯定会想呢,只支持单纯单一的动画吗?如果我需要多动画呢?
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:ordering="together"> <objectAnimator android:duration="2000" android:propertyName="rotationX" android:valueFrom="0.0" android:valueTo="360.0" android:valueType="floatType"></objectAnimator> <objectAnimator android:duration="2000" android:propertyName="scaleX" android:valueFrom="1.0" android:valueTo="2.0" android:valueType="floatType"></objectAnimator> <objectAnimator android:duration="2000" android:propertyName="scaleY" android:valueFrom="1.0" android:valueTo="2.0" android:valueType="floatType"></objectAnimator> </set>
使用set标签,
android:ordering属性值为
together表示一起运动,值还可以为
sequentially表示按着先后顺序运动。
我们来看看效果图:
Layout Animations(布局动画)
主要是通过使用LayoutAnimation为布局的容器设置动画,当容器中的视图层次发生变化时存在过渡的动画效果。
使用之前,我们要来了解下
LayoutAnimation有哪些类型:
LayoutTransition.APPEARING :一个View在ViewGroup中出现,该View的动画
LayoutTransition.CHANGE_APPEARING :一个View在ViewGroup中出现,该View对该ViewGroup中其他View位置造成影响,其他View的动画
LayoutTransition.DISAPPEARING :一个View在ViewGroup中消失,该View的动画
LayoutTransition.CHANGE_DISAPPEARING :一个View在ViewGroup中消失,该View对该ViewGroup中其他View位置造成影响,其他View的动画
这里重点区分加粗字体的含义,我们还是通过一个例子具体来看一下:
布局文件:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/bntXml" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="bntXml" android:text="xml动画" android:textColor="#ffffff" /> <Button android:id="@+id/bntAdd" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@+id/bntXml" android:onClick="bntAdd" android:text="addBtn动画" android:textColor="#ffffff" /> <GridLayout android:id="@+id/gridLayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/bntXml" android:columnCount="5"></GridLayout> <ImageView android:id="@+id/ivFace" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:src="@mipmap/face" /> </RelativeLayout>
Activity代码:
package com.github.ws.animationdemo; import android.animation.Animator; import android.animation.AnimatorInflater; import android.animation.LayoutTransition; import android.animation.ObjectAnimator; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.GridLayout; import android.widget.ImageView; /** * Created by Administrator on 4/6 0006. */ public class XmlAnimatorActivity extends Activity { private ImageView ivFace; private GridLayout gridLayout; private LayoutTransition transition; private int count = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.xml_anim); ivFace = (ImageView) findViewById(R.id.ivFace); gridLayout = (GridLayout) findViewById(R.id.gridLayout); transition = new LayoutTransition(); transition.setAnimator(LayoutTransition.APPEARING, ObjectAnimator.ofFloat(gridLayout, "scaleX", 0.0f, 1.0f)); transition.setDuration(1000); gridLayout.setLayoutTransition(transition); } void bntAdd(View view) { Button btn = new Button(this); btn.setText(""+count++); gridLayout.addView(btn,Math.min(1, gridLayout.getChildCount())); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { gridLayout.removeView(view); } }); } public void bntXml(View v) { Animator animator = AnimatorInflater.loadAnimator(this, R.animator.more_anim); animator.setTarget(ivFace); animator.start(); } }
效果图:
属性动画是不是很很容易实现,你也动手试试吧,效果肯定很棒。属性动画第二篇和第一篇都是一些基础知识,你如果感兴趣做出一些比较拉风,绚丽的动画,我们可以一起研究。
有什么疑问,什么地方写得不当的地方,还请多多指出。
相关文章推荐
- Android消息处理机制Message,Looper,Handler
- Android 开源库实践
- Android事件分发机制详解
- Android 中的一些开发技巧(延迟启动,文件写出,透明状态栏,任意界面退出)
- Android百度地图开发 百度地图得到当前位置
- 济南android培训机构应该如何来选择?
- Android开发常用工具方法
- Android中Binder的学习
- 下拉刷新Phoenix分析
- android 预制32 64 位so, bin
- Android Studio运行NDK程序,简单的JNI实例
- Android Studio下使用JNI编程备忘录
- Android触摸事件(一)-AbsTouchEventHandle
- Android源码-Volley
- android中的事件传递机制(1)
- Android语音聊天
- Android音视频录制类MediaRecorder用法举例
- 全面解析Android中对EditText输入实现监听的方法
- 在AndroidStudio中引入ZXing扫描二维码的简单实现
- Cordova android框架详解