Android 动画效果及Interpolator和AnimationListener的使用
?
Animation的4個(gè)基本動(dòng)畫效果What is Animation?
Animation
extends Object
implements Cloneable
Abstraction for an Animation that can be applied to Views, Surfaces, or other objects.
1、AlphaAnimation:淡入淡出效果 public classAlphaAnimation
extends Animation
An animation that controls the alpha level of an object. Useful for fading things in and out. This animation ends up changing the alpha property of aTransformation
Public ConstructorsAlphaAnimation(Context context, AttributeSet attrs)
Constructor used when an AlphaAnimation is loaded from a resource.
AlphaAnimation(float fromAlpha, float toAlpha)
Constructor to use when building an AlphaAnimation from code public class
AnimationSet
extends Animation
Represents a group of Animations that should be played together. The transformation of each individual animation are composed together into a single transform. If AnimationSet sets any properties that its children also set (for example, duration or fillBefore), the values of AnimationSet override the child values.
在代碼中實(shí)現(xiàn)動(dòng)畫效果的方法:
在XML文件中實(shí)現(xiàn)動(dòng)畫效果的方法:
① 在res目錄下創(chuàng)建一個(gè)anim文件夾,在里面添加一個(gè)alpha.xml文件:
② 在Activity中使用AnimationUtils獲取Animation并進(jìn)行設(shè)置:
Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.alpha); imageView.startAnimation(animation); 2、ScaleAnimation:縮放效果 public classScaleAnimation
extends Animation
An animation that controls the scale of an object. You can specify the point to use for the center of scaling.
Public ConstructorsScaleAnimation(Context context, AttributeSet attrs)
Constructor used when a ScaleAnimation is loaded from a resource.
ScaleAnimation(float fromX, float toX, float fromY, float toY)
Constructor to use when building a ScaleAnimation from code
ScaleAnimation(float fromX, float toX, float fromY, float toY, float pivotX, float pivotY)
Constructor to use when building a ScaleAnimation from code
ScaleAnimation(float fromX, float toX, float fromY, float toY, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)
Constructor to use when building a ScaleAnimation from code
在代碼中實(shí)現(xiàn)動(dòng)畫效果:
在XML文件中實(shí)現(xiàn)動(dòng)畫效果的方法:
① 在res的anim文件夾下,創(chuàng)建一個(gè)scale.xml文件:
② 在Activity中使用AnimationUtils獲取Animation并進(jìn)行設(shè)置:
Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.scale); imageView.startAnimation(animation); 3、Rotate:旋轉(zhuǎn)效果 public classRotateAnimation
extends Animation
An animation that controls the rotation of an object. This rotation takes place int the X-Y plane. You can specify the point to use for the center of the rotation, where (0,0) is the top left point. If not specified, (0,0) is the default rotation point.
Public ConstructorsRotateAnimation(Context context, AttributeSet attrs)
Constructor used when a RotateAnimation is loaded from a resource.
RotateAnimation(float fromDegrees, float toDegrees)
Constructor to use when building a RotateAnimation from code.
RotateAnimation(float fromDegrees, float toDegrees, float pivotX, float pivotY)
Constructor to use when building a RotateAnimation from code
RotateAnimation(float fromDegrees, float toDegrees, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)
Constructor to use when building a RotateAnimation from code
在代碼中實(shí)現(xiàn)動(dòng)畫效果:
在XML文件中實(shí)現(xiàn)動(dòng)畫效果的方法:
① 在res的anim文件夾下,創(chuàng)建一個(gè)rotate.xml文件:
② 在Activity中使用AnimationUtils獲取Animation并進(jìn)行設(shè)置:
Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.rotate); imageView.startAnimation(animation); 4、 Translate:移動(dòng)效果 public classTranslateAnimation
extends Animation
An animation that controls the position of an object.
Public ConstructorsTranslateAnimation(Context context, AttributeSet attrs)
Constructor used when a TranslateAnimation is loaded from a resource.
TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta)
Constructor to use when building a TranslateAnimation from code
TranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue, int fromYType, float fromYValue, int toYType, float toYValue)
Constructor to use when building a TranslateAnimation from code
在代碼中實(shí)現(xiàn)動(dòng)畫效果:
在XML文件中實(shí)現(xiàn)動(dòng)畫效果的方法:
① 在res的anim文件夾下,創(chuàng)建一個(gè)translate.xml文件:
其中100%p表示相對于父空間的位置
② 在Activity中使用AnimationUtils獲取Animation并進(jìn)行設(shè)置:
Animation animation = (Animation) AnimationUtils.loadAnimation(MainActivity.this, R.anim.translate); imageView.startAnimation(animation); 也可以使用AnimationSet為一個(gè)控件添加多個(gè)動(dòng)畫,或者在xml文件中添加多個(gè)動(dòng)畫標(biāo)簽,以下分別使用代碼和XML文件實(shí)現(xiàn)相同的效果: 代碼中實(shí)現(xiàn): AnimationSet animationSet = new AnimationSet(false); AlphaAnimation alpha = new AlphaAnimation(1.0f, 0.0f); ScaleAnimation scale = new ScaleAnimation(1, 0.5f, 1, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); animationSet.addAnimation(alpha); animationSet.addAnimation(scale); animationSet.setDuration(2000); animationSet.setStartOffset(1000); animationSet.setFillAfter(true); imageView.startAnimation(animationSet); XML實(shí)現(xiàn):alpha.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"android:fillAfter="true"><alphaandroid:fromAlpha="1.0"android:toAlpha="0.0"android:startOffset="1000"android:fillAfter="true"android:duration="2000" /><scale android:fromXScale="1.0"android:toXScale="0.5"android:fromYScale="1.0"android:toYScale="0.5"android:pivotX="50%"android:pivotY="50%"android:startOffset="1000"android:duration="2000" /> </set>Activity中的代碼:
Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.alpha); imageView.startAnimation(animation); Interpolator的使用
什么是Interpolator
Interpolator
extends Object Interpolator定義了動(dòng)畫變化的速率或規(guī)律,其具體的實(shí)現(xiàn)可以使用以下子類:
AccelerateDecelerateInterpolator:
AccelerateDecelerateInterpolator
extends Object
implements Interpolator
An interpolator where the rate of change starts and ends slowly but accelerates through the middle.
AccelerateInterpolater:
AccelerateInterpolator
extends Object
implements Interpolator
An interpolator where the rate of change starts out slowly and and then accelerates.
CycleInterpolator:
CycleInterpolator
extends Object
implements Interpolator
Repeats the animation for a specified number of cycles. The rate of change follows a sinusoidal pattern.
DecelerateInterpolator:
DecelerateInterpolator
extends Object
implements Interpolator
An interpolator where the rate of change starts out quickly and and then decelerates.
LinearInterpolator:
LinearInterpolator
extends Object
implements Interpolator
An interpolator where the rate of change is constant.
這些Interpolator可以在代碼或XML文件中定義:
XML文件定義在set標(biāo)簽里或每個(gè)動(dòng)畫標(biāo)簽
set標(biāo)簽中定義:
每個(gè)動(dòng)畫標(biāo)簽中定義:
<set xmlns:android="http://schemas.android.com/apk/res/android"android:interpolator="@android:anim/accelerate_interpolator"android:shareInterpolator="false"android:fillAfter="true"><alphaandroid:interpolator="@android:anim/accelerate_interpolator"android:fromAlpha="1.0"android:toAlpha="0.0"android:startOffset="1000"android:fillAfter="true"android:duration="2000" /><scaleandroid:interpolator="@android:anim/accelerate_decelerate_interpolator"android:fromXScale="1.0"android:toXScale="0.5"android:fromYScale="1.0"android:toYScale="0.5"android:pivotX="50%"android:pivotY="50%"android:startOffset="1000"android:duration="2000" /> </set>
在代碼中設(shè)置:
或者分別為每個(gè)動(dòng)畫設(shè)置:
AnimationSet animationSet = new AnimationSet(false); AlphaAnimation alpha = new AlphaAnimation(1.0f, 0.0f); alpha.setInterpolator(new AccelerateInterpolator()); ScaleAnimation scale = new ScaleAnimation(1, 0.5f, 1, 0.5f,Animation.RELATIVE_TO_SELF, 0.5f,Animation.RELATIVE_TO_SELF, 0.5f); scale.setInterpolator(new AccelerateDecelerateInterpolator()); Frame-By-Frame Animations的使用① 準(zhǔn)備4張圖片run1.png,run2.png,run3.png,run4.png分別放到res的三個(gè)drawable文件夾中
② 在res的drawable-ldpi目錄下創(chuàng)建一個(gè)anim_run.xml文件:
③ 在Activity中使用xml文件設(shè)置ImageView控件imageView的背景源,并獲取AnimationDrawable進(jìn)行顯示動(dòng)畫:
imageView.setBackgroundResource(R.drawable.anim_run); AnimationDrawable animationDrawable = (AnimationDrawable)imageView.getBackground(); animationDrawable.start(); 使用LayoutAnimationController設(shè)置ListView的動(dòng)畫
什么是LayoutAnimationController?
LayoutAnimationController
extends Object
A layout animation controller is used to animated a layout's, or a view group's, children. Each child uses the same animation but for every one of them, the animation starts at a different time. A layout animation controller is used by ViewGroup to compute the delay by which each child's animation start must be offset. The delay is computed by using characteristics of each child, like its index in the view group. This standard implementation computes the delay by multiplying a fixed amount of miliseconds by the index of the child in its parent view group. Subclasses are supposed to override getDelayForView(android.view.View) to implement a different way of computing the delay. For instance, aGridLayoutAnimationController will compute the delay based on the column and row indices of the child in its parent view group. Information used to compute the animation delay of each child are stored in an instance of LayoutAnimationController.AnimationParameters, itself stored in theViewGroup.LayoutParams of the view.
在使用LayoutAnimationController控制ListView控件的樣式效果的方法:
① 在res的anim文件夾中創(chuàng)建一個(gè)list_anim.xml文件用于控制ListView控件的動(dòng)畫:
② 創(chuàng)建一個(gè)布局文件item.xml用于設(shè)置ListView的item的樣式:
<?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/user_name" android:layout_width="180dip"android:layout_height="30dip"android:textSize="10pt"android:singleLine="true" /><TextView android:id="@+id/user_id" android:layout_width="fill_parent"android:layout_height="fill_parent"android:textSize="10pt"android:singleLine="true"/> </LinearLayout>③ 在主Activity的布局文件main.xml中添加一個(gè)ListView
<ListViewandroid:id="@id/android:list"android:layout_width="fill_parent"android:layout_height="wrap_content"android:scrollbars="vertical"android:layoutAnimation="@anim/anim_layout"/>④ 創(chuàng)建一個(gè)MainActivity繼承ListActivity,并在onCreate方法中添加如下代碼:
ListView listView = getListView();List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>(); HashMap<String, String> hm1 = new HashMap<String, String>(); hm1.put("user_name", "arthinking"); hm1.put("user_id", "001"); HashMap<String, String> hm2 = new HashMap<String, String>(); hm2.put("user_name", "Jason"); hm2.put("user_id", "002"); list.add(hm1); list.add(hm2);SimpleAdapter simpleAdapter = new SimpleAdapter(this, list,R.layout.item, new String[] { "user_name", "user_id" },new int[] { R.id.user_name, R.id.user_id }); listView.setAdapter(simpleAdapter);//通過Animation獲取LayoutAnimationController對ListView進(jìn)行設(shè)置 Animation animation = (Animation)AnimationUtils.loadAnimation(MainActivity.this, R.anim.list_anim); LayoutAnimationController lac = new LayoutAnimationController(animation); lac.setOrder(LayoutAnimationController.ORDER_NORMAL); lac.setDelay(0.5f); listView.setLayoutAnimation(lac);這樣,運(yùn)行程序,顯示的ListView就會(huì)按照xml文件中預(yù)置的動(dòng)畫效果顯示了。
也可以通過xml文件進(jìn)行設(shè)置動(dòng)畫:
① 在以上步驟的基礎(chǔ)之上,在res/anim文件夾下創(chuàng)建一個(gè)anim_layout.xml文件:
② main在布局文件的的ListView添加如下屬性:
android:layoutAnimation="@anim/anim_layout"這樣就在把MainActivity的onCreate()方法中的
//通過Animation獲取LayoutAnimationController對ListView進(jìn)行設(shè)置
注釋后的代碼刪除了,直接使用xml進(jìn)行動(dòng)畫的控制。
Animation.AnimationListener
android.view.animation.Animation.AnimationListener
An animation listener receives notifications from an animation. Notifications indicate animation related events, such as the end or the repetition of the animation.
包含以下的三個(gè)方法:
Notifies the end of the animation.
onAnimationRepeat(Animation animation)
Notifies the repetition of the animation.
onAnimationStart(Animation animation)
Notifies the start of the animation. AnimationListener在控件中的使用:
① 可以為一個(gè)Button添加一個(gè)事件:
button.setOnClickListener(new TestAnimationListener());② 接下來是編寫這個(gè)TestAnimationListener類,繼承AnimationListener,并覆蓋里面的三個(gè)方法:
//這里獲取控件組,R.id.layoutId為main.xml的整體布局標(biāo)簽的id屬性值 ViewGroup viewGroup = (ViewGroup)findViewById(R.id.layoutId);private class RemoveAnimationListener implements AnimationListener{//該方法在淡出效果執(zhí)行結(jié)束之后被調(diào)用@Overridepublic void onAnimationEnd(Animation animation) {//假設(shè)這里要在動(dòng)畫執(zhí)行完之后刪除一個(gè)TextViewviewGroup.removeView(textView);}@Overridepublic void onAnimationRepeat(Animation animation) {System.out.println("onAnimationRepeat");}@Overridepublic void onAnimationStart(Animation animation) {System.out.println("onAnimationStart");}}③ 同樣的,在動(dòng)畫效果中添加控件可以按照如下實(shí)現(xiàn)
ScaleAnimation scale = new ScaleAnimation(1, 0.5f, 1, 0.5f, scale.setDuration(1000); scale.setStartOffset(100); TextView textView = new TextView(MainActivity.this); textView.setText("add"); viewGroup.addView(textView, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); textView.startAnimation(scale);(特別說明:本文部分內(nèi)容是在觀看marschen的Android視頻教程時(shí)做的筆記,感謝marschen推出的視頻教程,這里也推薦給大家:http://www.marschen.com/portal.php)
除了文章中有特別說明,均為IT宅原創(chuàng)文章,轉(zhuǎn)載請以鏈接形式注明出處。本文鏈接:http://www.itzhai.com/android-animation-used-to-achieve-control-of-animation-effects-and-use-of-interpolator-and-animationlistener.html 關(guān)鍵字:?AlphaAnimation,?Android,?AnimationListener,?Interpolator,?RotateAnimation,?ScaleAnimation,?TranslateAnimation
轉(zhuǎn)載于:https://www.cnblogs.com/poorfish/p/4169345.html
總結(jié)
以上是生活随笔為你收集整理的Android 动画效果及Interpolator和AnimationListener的使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 华夏信用卡申请条件:资信条件越好通过率越
- 下一篇: 农行燃梦白金信用卡最高额度多少?如何获得