今天来到了外地,期间碰到一个老师,再循例问些基础知识点的时候,提到了jquery。我说我做过PHP开发,JQ那个时候深入的研究过。虽然时间有点久,但拾起来应该不是问题。
说完这些之后我脑海中突然有了一个想法,为什么以前没有用JQ的理念来理解并深入研究安卓的UI动画呢。况且,滑动弹出切换这些动画效果与JQ的理念很相似啊。
然后问的别的基础点什么的,知识常规的问询,所以没什么难度,都一一应对了。
不过接下来自己的脑海里总是出现JQ与AD(android)的某些可用之处。然后着实吓了一跳,太相似了。
下面我列举一些JQ的知识点:
jquery中常用的动画的方法就是hide()与show().
$(element).hide()这段代码可以与这相等element.css("display","none")
在hide(time)与show(time)中填入事件,可以慢慢消失跟显现。可以修改元素的多个样式,高度,宽度,不透明度。
另一组方法fadeIn()与fadeOut()这个与hide跟show不同的是,当使用hide或者show的时候会改变网页的高度,而fadeIn与fadeOut则不会。
$("#panel h5.head").toggle(function(){ $(this).addClass("highlight"); $(this).next().fadeOut(1000); },function(){ $(this).removeClass("highlight"); $(this).next("div .content").fadeIn(1000); });
还有一组是slideUp,slideDown改变高度。
动画方法概括
方法名 | 说明 |
Hide()跟show() | 同时修改多个样式属性:高度、宽度、不透明度 |
fadeIn(),fadeout() | 只修改不透明度 |
slideUp()和slideDown() | 只改变高度 |
fadeTo() | 只改变不透明度 |
Toggle() | 用来替代hide()方法和show()方法,所以会同时修改多个样式属性即高度宽度和不透明度 |
slideToggle() | 用来代替slideUp()和slideDown()方法,所以只能改变高度 |
Animate() | 属于自定义动画的方法,以上各种动画方法实质上都掉了了animate()方法,直接使用animate()方法还能自定义其他的属性样式,例如left,marginLeft,scrollTop等 |
动画队列
(1) 一组元素上的动画效果。
a) 当在一个animate()方法中应用多个属性时,动画是同时发生的。
b) 当以链式的写法应用动画方法时,动画是按照顺序发生的。
(2)多组元素上的动画效果
a)默认情况下,动画都是同时发生的。
b)当以回调的形式应用动画方式时,动画是按照回调顺序发生的。
另外,在动画方法中,要注意其他非动画的方法会插队,例如css()方法,要使这些非动画的方法也按照顺序来执行,需要把这些方法写在动画方法的回调函数中。
举一个animate的例子:
$(“#id”).animat({left:”400px”,top:”300px”},3000,function(){
$(this).css(“border”,”1px solid blue”);
});
若想要动画停止,需要在animate()方法前插入stop()方法
例如:$(“#id”).stop().animate()注意stop中的两个参数。
判断元素是否在动画状态的方法时:
$(element).is(“:animated”);
******************************************************************************************************************************************
一下内容摘自http://blog.csdn.net/feng88724/article/details/6320507
熟悉AD的童鞋估计很快就会对号入座,
Android 平台提供了两类动画。 一类是Tween动画,就是对场景里的对象不断的进行图像变化来产生动画效果(旋转、平移、放缩和渐变)。
第二类就是 Frame动画,即顺序的播放事先做好的图像,与gif图片原理类似。
下面就讲一下Tweene Animations。
主要类:
Animation 动画
AlphaAnimation 渐变透明度
RotateAnimation 画面旋转
ScaleAnimation 渐变尺寸缩放
TranslateAnimation 位置移动
AnimationSet 动画集
有了这些类,那么我们如何来实现动画效果呢?
以自定义View为例,该View很简单,画面上只有一个图片。 现在我们要对整个View分别实现各种Tween动画效果。
AlphaAnimation
通过代码实现 AlphaAnimation,如下:
- //初始化
- Animation alphaAnimation = new AlphaAnimation(0.1f, 1.0f);
- //设置动画时间 alphaAnimation.setDuration(3000);
- this.startAnimation(alphaAnimation);
其中AlphaAnimation类第一个参数fromAlpha表示动画起始时的透明度, 第二个参数toAlpha表示动画结束时的透明度。
setDuration用来设置动画持续时间。
RotateAnimation
代码:
- Animation rotateAnimation = new RotateAnimation(0f, 360f);
- rotateAnimation.setDuration(1000);
- this.startAnimation(rotateAnimation);
其中RotateAnimation类第一个参数fromDegrees表示动画起始时的角度, 第二个参数toDegrees表示动画结束时的角度。
另外还可以设置伸缩模式pivotXType、pivotYType, 伸缩动画相对于x,y 坐标的开始位置pivotXValue、pivotYValue等。
ScaleAnimation
代码:
- //初始化
- Animation scaleAnimation = new ScaleAnimation(0.1f, 1.0f,0.1f,1.0f);
- //设置动画时间
- scaleAnimation.setDuration(500);
- this.startAnimation(scaleAnimation);
ScaleAnimation类中
第一个参数fromX ,第二个参数toX:分别是动画起始、结束时X坐标上的伸缩尺寸。
第三个参数fromY ,第四个参数toY:分别是动画起始、结束时Y坐标上的伸缩尺寸。
另外还可以设置伸缩模式pivotXType、pivotYType, 伸缩动画相对于x,y 坐标的开始位置pivotXValue、pivotYValue等。
TranslateAnimation
代码:
- //初始化
- Animation translateAnimation = new TranslateAnimation(0.1f, 100.0f,0.1f,100.0f);
- //设置动画时间 translateAnimation.setDuration(1000);
- this.startAnimation(translateAnimation);
TranslateAnimation类
第一个参数fromXDelta ,第二个参数toXDelta:分别是动画起始、结束时X坐标。
第三个参数fromYDelta ,第四个参数toYDelta:分别是动画起始、结束时Y坐标。
参数详细说明:
表二 | ||
XML节点 | 功能说明 | |
alpha | 渐变透明度动画效果 | |
<alpha android:fromAlpha=”0.1″ android:toAlpha=”1.0″ android:duration=”3000″ /> | ||
fromAlpha | 属性为动画起始时透明度 | 0.0表示完全透明 1.0表示完全不透明 以上值取0.0-1.0之间的float数据类型的数字 |
toAlpha | 属性为动画结束时透明度 |
表三 | |||
scale | 渐变尺寸伸缩动画效果 | ||
<scale android:interpolator= “@android:anim/accelerate_decelerate_interpolator” android:fromXScale=”0.0″ android:toXScale=”1.4″ android:fromYScale=”0.0″ android:toYScale=”1.4″ android:pivotX=”50%” android:pivotY=”50%” android:fillAfter=”false” android:startOffset=“700” android:duration=”700″ android:repeatCount=”10″ /> | |||
fromXScale[float] fromYScale[float] | 为动画起始时,X、Y坐标上的伸缩尺寸 | 0.0表示收缩到没有 1.0表示正常无伸缩 值小于1.0表示收缩 值大于1.0表示放大 | |
toXScale [float] toYScale[float] | 为动画结束时,X、Y坐标上的伸缩尺寸 | ||
pivotX[float] pivotY[float] | 为动画相对于物件的X、Y坐标的开始位置 | 属性值说明:从0%-100%中取值,50%为物件的X或Y方向坐标上的中点位置 | |
表四 | |||
translate | 画面转换位置移动动画效果 | ||
<translate android:fromXDelta=”30″ android:toXDelta=”-80″ android:fromYDelta=”30″ android:toYDelta=”300″ android:duration=”2000″ /> | |||
fromXDelta toXDelta | 为动画、结束起始时 X坐标上的位置 | ||
fromYDelta toYDelta | 为动画、结束起始时 Y坐标上的位置 | ||
表五 | |||
rotate | 画面转移旋转动画效果 | ||
<rotate android:interpolator=”@android:anim/accelerate_decelerate_interpolator” android:fromDegrees=”0″ android:toDegrees=”+350″ android:pivotX=”50%” android:pivotY=”50%” android:duration=”3000″ /> | |||
fromDegrees | 为动画起始时物件的角度 | 说明 当角度为负数——表示逆时针旋转 当角度为正数——表示顺时针旋转 (负数from——to正数:顺时针旋转) (负数from——to负数:逆时针旋转) (正数from——to正数:顺时针旋转) (正数from——to负数:逆时针旋转) | |
toDegrees | 属性为动画结束时物件旋转的角度 可以大于360度 | ||
pivotX pivotY | 为动画相对于物件的X、Y坐标的开始位 | 说明:以上两个属性值 从0%-100%中取值 50%为物件的X或Y方向坐标上的中点位置 | |
以上都是单独的使用某个动画,那么如何让多个动画同时生效呢?
答案是 AnimationSet。
初看整个类名,还以为只是用来存放 Animation的一个Set, 细看才发现,该类也是继承自 Animation的。
下面我们实现一个动画,该动画会让图片移动的同时,图片透明度渐变,直接看代码吧。
- //初始化 Translate动画
- translateAnimation = new TranslateAnimation(0.1f, 100.0f,0.1f,100.0f);
- //初始化 Alpha动画
- alphaAnimation = new AlphaAnimation(0.1f, 1.0f);
- //动画集
- AnimationSet set = new AnimationSet(true);
- set.addAnimation(translateAnimation);
- set.addAnimation(alphaAnimation);
- //设置动画时间 (作用到每个动画)
- set.setDuration(1000);
- this.startAnimation(set);
Android 平台提供了两类动画。 一类是Tween动画,就是对场景里的对象不断的进行图像变化来产生动画效果(旋转、平移、放缩和渐变)。第二类就是 Frame动画,即顺序的播放事先做好的图像,与gif图片原理类似。
下面就讲一下Frame Animation。
其实使用起来比较简单,首先需要创建一个AnimationDrawable对象,通过addFrame方法把每一帧要显示的内容添加进去,最后通过Start方法来播放动画。 同时还有设置循环setOneShot等方法可供使用。
下面先看一下官方对AnimationDrawable类的说明:
An object used to create frame-by-frame animations, defined by a series of Drawable objects, which can be used as a View object's background.
The simplest way to create a frame-by-frame animation is to define the animation in an XML file, placed in the res/drawable/ folder, and set it as the background to a View object. Then, call
run()
to start the animation.An AnimationDrawable defined in XML consists of a single
<animation-list>
element, and a series of nested<item>
tags. Each item defines a frame of the animation. See the example below.spin_animation.xml file in res/drawable/ folder:
<!-- Animation frames are wheel0.png -- wheel5.png files inside the res/drawable/ folder --> <animation-list android:id="selected" android:oneshot="false"> <item android:drawable="@drawable/wheel0" android:duration="50" /> <item android:drawable="@drawable/wheel1" android:duration="50" /> <item android:drawable="@drawable/wheel2" android:duration="50" /> <item android:drawable="@drawable/wheel3" android:duration="50" /> <item android:drawable="@drawable/wheel4" android:duration="50" /> <item android:drawable="@drawable/wheel5" android:duration="50" /> </animation-list>Here is the code to load and play this animation.
// Load the ImageView that will host the animation and // set its background to our AnimationDrawable XML resource. ImageView img = (ImageView)findViewById(R.id.spinning_wheel_image); img.setBackgroundResource(R.drawable.spin_animation); // Get the background, which has been compiled to an AnimationDrawable object. AnimationDrawable frameAnimation = (AnimationDrawable) img.getBackground(); // Start the animation (looped playback by default). frameAnimation.start()
相信看了这个说明,大家也都知道如何做了吧?
下面我们使用Frame动画做一个 loading 的效果。
首先准备素材,如下:
然后新建一个AnimationDrawable对象,把这些图片加载进去。 addFrame第一参数表示要加载的内容,第二参数表示持续时间。
代码:
- frameAnimation = new AnimationDrawable();
- for (int i = 0; i < 10; i++) {
- int id = getResources().getIdentifier("load" + (i+1), "drawable", this.getContext().getPackageName());
- frameAnimation.addFrame(getResources().getDrawable(id), 100);
- }
- //设置循环播放 false表示循环 true表示不循环,仅播放一次
- frameAnimation.setOneShot(false);
AnimationDrawable 类是Drawable类的子类,因此可以将 AnimationDrawable 设为背景等来查看效果。
最后只要调用 frameAnimation.start(); 就可以启动该Frame动画了。
效果图改日补上。
那么上面是使用Java代码的方式来加载的Frame动画, 也可以像Tween动画那样,使用纯XML局部文件来做。 API中已经有说明,这边不再赘述了。
直接看示例:
- <?xml version="1.0" encoding="utf-8"?>
- <animaltion-list xmlns:android="http://schemas.android.com/apk/res/android"
- android:oneshot="false">
- <item android:drawable="@drawable/load1" android:duration="50" />
- <item android:drawable="@drawable/load2" android:duration="50" />
- <item android:drawable="@drawable/load3" android:duration="50" />
- <item android:drawable="@drawable/load4" android:duration="50" />
- <item android:drawable="@drawable/load5" android:duration="50" />
- <item android:drawable="@drawable/load6" android:duration="50" />
- <item android:drawable="@drawable/load7" android:duration="50" />
- <item android:drawable="@drawable/load8" android:duration="50" />
- <item android:drawable="@drawable/load9" android:duration="50" />
- <item android:drawable="@drawable/load10" android:duration="50" />
- </animaltion-list>
有不清楚之处,欢迎留言交流。
附上参数详细说明(来自: http://www.moandroid.com/?p=790)
表一 | ||
属性[类型] | 功能 | |
Duration[long] | 属性为动画持续时间 | 时间以毫秒为单位 |
fillAfter [boolean] | 当设置为true ,该动画转化在动画结束后被应用 | |
fillBefore[boolean] | 当设置为true ,该动画转化在动画开始前被应用 | |
interpolator | 指定一个动画的插入器 | 有一些常见的插入器 accelerate_decelerate_interpolator 加速-减速 动画插入器 accelerate_interpolator 加速-动画插入器 decelerate_interpolator 减速- 动画插入器其他的属于特定的动画效果 |
repeatCount[int] | 动画的重复次数 | |
RepeatMode[int] | 定义重复的行为 | 1:重新开始 2:plays backward |
startOffset[long] | 动画之间的时间间隔,从上次动画停多少时间开始执行下个动画 | |
zAdjustment[int] | 定义动画的Z Order的改变 | 0:保持Z Order不变 1:保持在最上层 -1:保持在最下层 |
以上不足之处还请指正,如有更多易于理解安卓某些知识点的其它,务必不吝赐教啊。