安卓ViewAnimation的简单介绍
来源:互联网 发布:淘宝网 女士腕表 编辑:程序博客网 时间:2024/06/04 23:27
本篇是介绍Android动画系列博客的第一篇,也是最基础的一篇。其实android的动画分为很多种,其中Drawable中也是含有动画的,但是本质应该都是一样的(我没有看源码,不过我猜应该是一样的!!!)。在这篇博客中主要讲述如何使用scale、alpha、rotate和translate动画。这里的使用主要是指在xml中使用,当然在java代码中也可以使用,只不过是利用代码设置相关的属性罢了,就跟在java代码中实例化一个view一样。
好了废话不多说,还是先看一下效果图吧:
scale:
scale_animation.xml文件:
<?xml version="1.0" encoding="utf-8"?><scale xmlns:android="http://schemas.android.com/apk/res/android" android:duration="3000" android:fillAfter="false" android:fillBefore="true" android:fillEnabled="false" android:fromXScale="0.5" android:fromYScale="1" android:pivotX="50%" android:pivotY="50%" android:repeatCount="2" android:repeatMode="reverse" android:toXScale="2" android:toYScale="2"></scale>
这个动画有几个属性需要说一下,其中一个fillAfter,如果fillAfter为true,那么说明UI保持动画执行完之后的状态,在这里即为ImageView的x、y方向放大2倍。fillBefore为true,说明UI在动画完成之后保持动画执行之前的状态,其中测试的时候发现fillBefore和fillEnabled的属性产生的效果一样。
pivotX,pivotY代表的是缩放中心的坐标,未指定的情况,UI的缩放坐标为0.0,其中pivotX、pivotY的数值有三种形式
1、纯数字,比如100、200等,这代表缩放坐标相对于原点移动的距离,如果pivotY=100,pivotX=100,则代表缩放坐标为(100,100);
2、百分比,比如50%等,这代表也是代表缩放坐标相对与原点偏移的距离,如果都是50%,则代表缩放坐标在X轴和Y轴上分别偏移了UI的宽高的50%。
3、百分比+p,比如50%p,这里的p代表的是父布局,如果都是50%p,则代表缩放坐标在X轴和Y轴上分别偏移了父布局的宽高的50%。
其他的属性都比较简单,相信你肯定一看就知道了!!!(在使用xml的时候遇到一个比较坑的事情,就是有的属性编辑器不提示,看了一下源码才知道,有些属性不是在ScaleAnimation中解析的,而是在Animation中解析的。在Animation中解析的属性,在scale标签中设置属性时都不提示!!!感觉不怎么友好呀!!!)
alpha动画:
alpha_animation.xml文件:
<?xml version="1.0" encoding="utf-8"?><alpha xmlns:android="http://schemas.android.com/apk/res/android" android:duration="3000" android:fillAfter="false" android:fillBefore="true" android:fillEnabled="false" android:fromAlpha="1.0" android:repeatCount="1" android:repeatMode="reverse" android:toAlpha="0.3"></alpha>
不是所有的牛奶都叫*,也不是所有的动画都有pivotX、pivotY属性的!!!
这里的属性没什么好说的,都是比较简单,如果还是不太清楚的话,可以自己编写一个demo试一下,这样印象绝对深刻!!!
rotate动画:
rotate_animation.xml文件:
<?xml version="1.0" encoding="utf-8"?><rotate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="3000" android:fillAfter="false" android:fillBefore="true" android:fillEnabled="false" android:pivotX="50%" android:pivotY="50%" android:repeatCount="1" android:repeatMode="reverse" android:fromDegrees="0" android:toDegrees="360"></rotate>
在这里只是将控制变量换成了度数,直接控制UI旋转的范围,在这里设置为从0旋转到360度。
translate动画:
translate_animation.xml文件:
<?xml version="1.0" encoding="utf-8"?><translate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="3000" android:fillAfter="false" android:fillBefore="true" android:fillEnabled="false" android:repeatCount="1" android:repeatMode="reverse" android:fromXDelta="0" android:fromYDelta="0" android:toXDelta="700" android:toYDelta="700"></translate>
这里可以分别设置x、y轴上面移动的位置,也就是起始点和终点,都是比较简单的,没什么好说的,还不清楚的话,可以看代码或者私信找我!!!
最后一个,也是比较常用的一个,那就是动画集,将所有的动画一起播放。
先看一下效果图:
set_animation.xml文件:
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" android:duration="3000" android:fillAfter="false" android:fillBefore="true" android:repeatMode="reverse"> <scale android:fromXScale="0.5" android:fromYScale="1" android:pivotX="50%" android:pivotY="50%" android:repeatCount="2" android:toXScale="2" android:toYScale="2"/> <translate android:fromXDelta="0" android:fromYDelta="0" android:repeatCount="1" android:toXDelta="700" android:toYDelta="700"/> <rotate android:fromDegrees="0" android:pivotX="50%" android:pivotY="50%" android:repeatCount="1" android:toDegrees="360"/> <alpha android:fromAlpha="1.0" android:repeatCount="1" android:toAlpha="0.3"/></set>
就是将所有的动画都放在set标签下,具体的可以看一下上面的xml文件,也是比较简单的,其实有些动画可以设置插值器,在这里就没有说到,感觉插值器还是比较重要的,会单独写一篇博客用于介绍插值器的,希望大家可以多多关注一下!!!
主布局文件:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:weightSum="2" tools:context="com.example.zhuyuqiang.animation.MainActivity"> <ImageView android:id="@+id/iv" android:src="@drawable/image3" android:layout_width="match_parent" android:layout_height="0dp" android:layout_gravity="center_horizontal" android:layout_weight="1" /> <HorizontalScrollView android:layout_width="match_parent" android:layout_height="0dp" android:layout_gravity="center_horizontal" android:layout_weight="1"> <LinearLayout android:layout_gravity="center_horizontal" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <Button android:textAllCaps="false" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="startScaleAnimation" android:text="@string/scale" /> <Button android:textAllCaps="false" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="startAlphaAnimation" android:text="@string/alpha" /> <Button android:textAllCaps="false" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="startRotateAnimation" android:text="@string/rotate" /> <Button android:textAllCaps="false" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="startTranslateAnimation" android:text="@string/translate" /> <Button android:textAllCaps="false" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="startSetAnimation" android:text="@string/set" /> </LinearLayout> </HorizontalScrollView></LinearLayout>
我以为button会比较多,放不下,所以用了一个scrollerView包裹了一下,没想到空间够了,刚好放的下!!!布局方面在这里就不说了,网上介绍的很多不知道的可以自己查阅一下!!
activity文件:
package com.example.viewanimation;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.view.animation.AlphaAnimation;import android.view.animation.AnimationSet;import android.view.animation.AnimationUtils;import android.view.animation.RotateAnimation;import android.view.animation.ScaleAnimation;import android.view.animation.TranslateAnimation;import android.widget.ImageView;import com.example.viewanimation.R;public class MainActivity extends AppCompatActivity { private ImageView imageView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); imageView = (ImageView) findViewById(R.id.iv); } public void startScaleAnimation(View view){ ScaleAnimation scaleAnimation = (ScaleAnimation) AnimationUtils.loadAnimation(MainActivity.this,R.anim.scale_animation); imageView.startAnimation(scaleAnimation); } public void startAlphaAnimation(View view){ AlphaAnimation alphaAnimation = (AlphaAnimation) AnimationUtils.loadAnimation(MainActivity.this,R.anim.alpha_animation); imageView.startAnimation(alphaAnimation); } public void startRotateAnimation(View view){ RotateAnimation rotateAnimation = (RotateAnimation) AnimationUtils.loadAnimation(MainActivity.this,R.anim.rotate_animation); imageView.startAnimation(rotateAnimation); } public void startTranslateAnimation(View view){ TranslateAnimation translateAnimation = (TranslateAnimation) AnimationUtils.loadAnimation(MainActivity.this,R.anim.translate_animation); imageView.startAnimation(translateAnimation); } public void startSetAnimation(View view){ AnimationSet animationSet = (AnimationSet) AnimationUtils.loadAnimation(MainActivity.this,R.anim.set_animation); imageView.startAnimation(animationSet); }}
想要使用我们自己在xml中声明的动画,就需要我们使用android提供的AnimationUtils去加载一个动画,实例已在代码给出,而且你看这名字起得多贴心,一看就知道是干什么!!!
好了,关于ViewAnimation就说到这里,明天还会继续更新android动画方面的博客的,希望小伙伴们可以多多支持一下!!!
这是我的微信公众号,如果可以的话,希望您可以帮忙关注一下,这将是对我最大的鼓励了,谢谢!!
代码地址(后期关于动画方面的demo都会存放在这个仓库中):
https://github.com/zhuyuqiang2017/Animation
- 安卓ViewAnimation的简单介绍
- 安卓适配器的简单介绍
- ViewAnimation的基本使用方法
- 安卓 objectAnimator简单介绍
- 简单介绍安卓的五大布局
- 安卓开发RelativeLayout属性的简单介绍
- 安卓触摸事件处理机制的简单介绍
- 安卓驱动编译简单介绍
- 动画之视图动画--ViewAnimation的学习
- 安卓的Application介绍
- 安卓的基础介绍
- 简单的安卓记事本
- 安卓 简单的handle
- 安卓简单的画板
- 简单介绍安卓的活动及各种布局(8.18)
- 安卓Handler机制的简单介绍——随机点名案例
- 安卓学习笔记---Android MVP 模式 简单易懂的介绍方式
- 安卓xmpp简单介绍(登录)以及自签名证书的使用
- 算法(第4版本)1.1.28
- Oracle、MySql、SQLServer 数据分页查询
- 04二级指针三种内存模型
- component-scan使用的时候在component中实例化ApplicationContext
- HTTP Status 405
- 安卓ViewAnimation的简单介绍
- HDU 1704 Rank (floyd传递闭包)
- A
- 多线程_匿名内部类的方式实现多线程程序
- Eclipse中Android项目SVN过滤的目录
- 数组
- 设计模式(七)桥接模式
- java文件路径问题
- YUV420转换成RGB24