1.什么是Animations
- Animations提供了一系列的動(dòng)畫(huà)效果,這些效果可以應(yīng)用在絕大多數(shù)的控件.
2.Animations的分類(lèi)
- 代碼中實(shí)現(xiàn)動(dòng)畫(huà)
- xml中使用動(dòng)畫(huà)
總體分兩大類(lèi)
第一類(lèi):
Tweened Animations
該類(lèi)Animations提供了旋轉(zhuǎn),移動(dòng),伸展和淡出等等效果1.
Alpha
:淡入淡出效果2.
Scale
:縮放效果3.
Rotate
:旋轉(zhuǎn)效果4.
Translate
:移動(dòng)效果第二類(lèi):
Frame-by-Frame Animations
這一類(lèi)Animations
可以創(chuàng)建一個(gè)Drawable
序列婴削,這些Drawable
可以按照指定的時(shí)間間歇一個(gè)一個(gè)的顯示偿枕;-
使用
Tweened Animations
的步驟
1.創(chuàng)建一個(gè)AnimationSet
對(duì)象
2.根據(jù)需要?jiǎng)?chuàng)建相應(yīng)的Animation
對(duì)象
3.根據(jù)軟件動(dòng)畫(huà)的需求,為Animation
對(duì)象設(shè)置相應(yīng)的數(shù)據(jù)
4.將Animation對(duì)象添加到AnimationSet
對(duì)象當(dāng)中
5.使用空間對(duì)象開(kāi)始執(zhí)行AnimationSet
-
Tween Animation的通用屬性
1.setDuration(long durationMils)
設(shè)置動(dòng)畫(huà)持續(xù)時(shí)間(單位毫秒)
2.setFillAfter(boolean fillAfter)
如果fillAfter的值為true,則動(dòng)畫(huà)執(zhí)行后,控件將停留在執(zhí)行結(jié)束的狀態(tài)
3.setFillBefore(boolean fillBefore)
如果fillBefore的值為true;則動(dòng)畫(huà)執(zhí)行后嗅绸,控件將回到動(dòng)畫(huà)執(zhí)行之前的狀態(tài);
4.setStartOffset(long startOffSet)
設(shè)置動(dòng)畫(huà)執(zhí)行之前的等待時(shí)間
5.setRepeatCount(int repearCount)
設(shè)置動(dòng)畫(huà)重復(fù)執(zhí)行的次數(shù)
(在代碼中實(shí)現(xiàn)動(dòng)畫(huà))實(shí)例代碼:
<pre><code>package mars.animations01;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
private ImageView imageView = null;
private Button rotateButton = null;
private Button scaleButton = null;
private Button alphaButton = null;
private Button translateButton = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imageView = (ImageView) findViewById(R.id.imageViewId);
rotateButton = (Button) findViewById(R.id.rotateButtonId);
rotateButton.setOnClickListener(new RotateButtonListener());
scaleButton = (Button) findViewById(R.id.scaleButtonId);
scaleButton.setOnClickListener(new ScaleButtonListener());
alphaButton = (Button) findViewById(R.id.alphaButtonId);
alphaButton.setOnClickListener(new AlphaButtonListener());
translateButton = (Button) findViewById(R.id.translateButtonId);
translateButton.setOnClickListener(new TranslateButtonListener());
}
private class RotateButtonListener implements OnClickListener {
@Override
public void onClick(View view) {
AnimationSet animationSet = new AnimationSet(true);
RotateAnimation rotateAnimation = new RotateAnimation(0, 360,
Animation.RELATIVE_TO_PARENT, 1f,
Animation.RELATIVE_TO_PARENT, 0f);
rotateAnimation.setDuration(5000);
animationSet.addAnimation(rotateAnimation);
imageView.startAnimation(animationSet);
}
}
private class ScaleButtonListener implements OnClickListener {
@Override
public void onClick(View view) {
AnimationSet animationSet = new AnimationSet(true);
ScaleAnimation scaleAnimation = new ScaleAnimation(1, 0.1f, 1, 0.1f,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
animationSet.addAnimation(scaleAnimation);
animationSet.setStartOffset(1000);
animationSet.setFillAfter(true);
animationSet.setFillBefore(false);
animationSet.setDuration(2000);
imageView.startAnimation(animationSet);
}
}
private class AlphaButtonListener implements OnClickListener {
@Override
public void onClick(View view) {
//創(chuàng)建一個(gè)AnimationSet對(duì)象
AnimationSet animationSet = new AnimationSet(true);
//創(chuàng)建一個(gè)AlphaAnimation對(duì)象
AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);
//設(shè)置動(dòng)畫(huà)執(zhí)行的時(shí)間(單位:毫秒)
alphaAnimation.setDuration(1000);
//將AlphaAnimation對(duì)象添加到AnimationSet當(dāng)中
animationSet.addAnimation(alphaAnimation);
//使用ImageView的startAnimation方法開(kāi)始執(zhí)行動(dòng)畫(huà)
imageView.startAnimation(animationSet);
}
}
private class TranslateButtonListener implements OnClickListener {
@Override
public void onClick(View view) {
AnimationSet animationSet = new AnimationSet(true);
TranslateAnimation translateAnimation = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0f,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0f,
Animation.RELATIVE_TO_SELF, 1.0f);
translateAnimation.setDuration(1000);
animationSet.addAnimation(translateAnimation);
imageView.startAnimation(animationSet);
}
}
}</pre></code>
(在xml文件中實(shí)現(xiàn)):
首先需要在res文件夾下新建anim文件夾
然后可以在anim文件夾中新建xml文件
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">
<alpha
android:fromAlpha="1.0"
android:toAlpha="0.0"
android:startOffset="500"
android:duration="500" />
</set>
rotate.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator">
<rotate android:fromDegrees="0"
android:toDegrees="360"
android:pivotX="50%"
android:pivotY="50%"
android:duration="5000" />
</set>
-
android:pivotX
="50" 這種方法是用絕對(duì)位置定位撕彤; -
android:pivotX
="50%"這種方法相對(duì)于控件本身定位鱼鸠; -
android:pivotX
="50%p"這種方法相對(duì)于控件的父控件定位
scale.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator">
<scale android:fromXScale="1.0"
android:toXScale="0.0"
android:fromYScale="1.0"
android:toYScale="0.0"
android:pivotX="50%"
android:pivotY="50%"
android:duration="2000" />
</set>
translate.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator">
<translate
android:fromXDelta="50%"
android:toXDelta="100%"
android:fromYDelta="0%"
android:toYDelta="100%"
android:duration="2000" />
</set>
如何在java代碼中使用anim:
Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.alpha); imageView.startAnimation(animation);
AnimationSet的使用方法
什么是AnimationSet
1.AnimationSet是Animation的子類(lèi),可以整合各種動(dòng)畫(huà)效果
2.一個(gè)AnimationSet包含了一系列的Animation
3.針對(duì)AnimationSet設(shè)置一些Animation的常見(jiàn)屬性羹铅,可以被包含在AnimationSet當(dāng)中的Animation集成
實(shí)例瞧柔,實(shí)現(xiàn)漸變和旋轉(zhuǎn)
public class MainActivity extends Activity {
private Button button = null;
private ImageView imageView = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imageView = (ImageView) findViewById(R.id.imageViewId);
button = (Button) findViewById(R.id.scaleButtonId);
button.setOnClickListener(new AnimationButtonListener());
}
private class AnimationButtonListener implements OnClickListener {
@Override
public void onClick(View v) {
/**
* Animation animation =
* AnimationUtils.loadAnimation(MainActivity.this, R.anim.alpha);
* imageView.startAnimation(animation);
*/
// 聲明一個(gè)AnimationSet對(duì)象
AnimationSet animationSet = new AnimationSet(false);
//false 設(shè)置android:shareInterpolator為false,true設(shè)置android:shareInterpolator為true
//animationSet.setInterpolator(new AccelerateInterpolator());
AlphaAnimation alpha = new AlphaAnimation(1.0f, 0.0f);
alpha.setInterpolator(new DecelerateInterpolator());
RotateAnimation rotate = new RotateAnimation(0, 360,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
rotate.setInterpolator(new AccelerateInterpolator());
animationSet.addAnimation(alpha);
animationSet.addAnimation(rotate);
animationSet.setDuration(2000);
animationSet.setStartOffset(500);
imageView.startAnimation(animationSet);
}
}
}
Interpolator 的使用方法-控制動(dòng)畫(huà)使用效果(ex:前慢后快)
Interpolator
定義了動(dòng)畫(huà)變化的速率睦裳,在Animations
框架當(dāng)中定義了以下幾種
Interpolator
- AccelerateDecelerateInterpolator:在動(dòng)畫(huà)開(kāi)始與結(jié)束的地方速率改變比較慢造锅,在中間的時(shí)候加速
- AccelerateInterpolator:在動(dòng)畫(huà)開(kāi)始的地方速率改變比較慢,然后開(kāi)始加速
- CycleInterpolator:動(dòng)畫(huà)循環(huán)播放特定的次數(shù)廉邑,速率改變沿著正弦曲線(xiàn)
- DecelerateInterpolator:在動(dòng)畫(huà)開(kāi)始的地方速率改變比較慢哥蔚,然后開(kāi)始減速
-
LinearInterpolator:動(dòng)畫(huà)以均勻的速率改變
在xml文件中設(shè)置Interpolator
Paste_Image.png
<?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">
<alpha
android:fromAlpha="1.0"
android:toAlpha="0.0"
android:startOffset="500"
android:duration="2000" />
<rotate android:fromDegrees="0"
android:toDegrees="360"
android:pivotX="50%"
android:pivotY="50%"
android:duration="2000" />
</set>
如果shereInterpolator
= "false" 則需要對(duì)AnimationSet
中的每一個(gè)動(dòng)畫(huà)設(shè)置Interpolator
屬性
Frame-By-Frame Animations的使用方法
在res/drawable當(dāng)中創(chuàng)建一個(gè)xml文件倒谷,用于定義Animations的動(dòng)畫(huà)序列
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item android:drawable="@drawable/nv1" android:duration="500" />
<item android:drawable="@drawable/nv2" android:duration="500" />
<item android:drawable="@drawable/nv3" android:duration="500" />
<item android:drawable="@drawable/nv4" android:duration="500" />
</animation-list>
使用方法
1.為imageView設(shè)置背景資源
imageView.setBackgroundResource(R.drawable.anim_nv);
2.通過(guò)ImageView得到AnimationDrawable
AnimationDrawable animationDrawable = (AnimationDrawable)imageView.getBackground();
3.開(kāi)始執(zhí)行動(dòng)畫(huà)
animationDrawable.start();
LayoutAnimationController的使用方法
-
什么是LayoutAnimationController
- LayoutAnimationController用于為一個(gè)layout里面的控件,或者是一個(gè)ViewGroup里面的控件設(shè)置動(dòng)畫(huà)效果;
2.每一個(gè)控件都有相同的動(dòng)畫(huà)效果
3.這些控件的動(dòng)畫(huà)效果在不同的時(shí)間顯示出來(lái)
4.LayoutAnimationController可以在xml文件當(dāng)中設(shè)置糙箍,也可以在代碼當(dāng)中進(jìn)行設(shè)置渤愁。
在xml文件中使用LayoutAnimationController
在anim文件夾下創(chuàng)建一個(gè)新文件,名為list_anim_layout.xml文件
<?xml version="1.0" encoding="utf-8"?>
<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
android:delay="2"
android:animationOrder="normal" //有三個(gè)值random,normal,reverse
android:animation="@anim/list_anim" />
list_anim.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">
<alpha
android:fromAlpha="0.0"
android:toAlpha="1.0"
android:duration="2000" />
</set>
main.xml文件
<?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="vertical" >
<ListView
android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:layoutAnimation="@anim/list_anim_layout"
//當(dāng)在代碼中使用LayoutAnimationController時(shí)深夯,去掉此屬性
/>
<Button
android:id="@+id/buttonId"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="測(cè)試" />
</LinearLayout>
代碼中使用LayoutAnimationController
1.創(chuàng)建一個(gè)Animation對(duì)象
2.使用如下代碼創(chuàng)建LayoutAnimationController對(duì)象:
LayoutAnimationController lac = new LayoutAnimationController(animation);
3.設(shè)置控件顯示的順序:
lac.setOrder(LayoutAnimationController.ORDER_NORMAL);
4.為L(zhǎng)istView設(shè)置LayoutAnimationController屬性
listView.setLayoutAnimation(lac);
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);
AnimationListenter的使用方法
1.AnimationListenter是一個(gè)監(jiān)聽(tīng)器
2.該監(jiān)聽(tīng)器在動(dòng)畫(huà)執(zhí)行的各個(gè)階段會(huì)得到通知抖格,而調(diào)用相應(yīng)的方法
3.主要包含以下的三個(gè)方法
1.onAnimationEnd(Animation animation)
2.onAnimationRepeat(Animation animation)
3.onAnimationStart(Animation animation)
代碼示例:
public class MainActivity extends Activity {
/** Called when the activity is first created. */
private Button removeButton = null;
private Button addButton = null;
private ImageView imageView = null;
private ViewGroup viewGroup = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
removeButton = (Button) findViewById(R.id.removeButtonId);
imageView = (ImageView) findViewById(R.id.imageViewId);
removeButton.setOnClickListener(new RemoveButtonListener());
viewGroup = (ViewGroup) findViewById(R.id.layoutId);
addButton = (Button) findViewById(R.id.addButtonId);
addButton.setOnClickListener(new AddButtonListener());
}
private class AddButtonListener implements OnClickListener {
@Override
public void onClick(View v) {
// 創(chuàng)建了一個(gè)淡入效果的Animation對(duì)象
AlphaAnimation animation = new AlphaAnimation(0.0f, 1.0f);
animation.setDuration(1000);
animation.setStartOffset(500);
// 創(chuàng)建一個(gè)新的ImageView
ImageView imageViewAdd = new ImageView(MainActivity.this);
imageViewAdd.setImageResource(R.drawable.icon);
// 將新的ImageView添加到viewGroup當(dāng)中
viewGroup.addView(imageViewAdd, new LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
// 啟動(dòng)動(dòng)畫(huà)
imageViewAdd.startAnimation(animation);
}
}
private class RemoveButtonListener implements OnClickListener {
@Override
public void onClick(View v) {
// 創(chuàng)建一個(gè)淡出效果的Animation對(duì)象
AlphaAnimation animation = new AlphaAnimation(1.0f, 0.0f);
// 為Animation對(duì)象設(shè)置屬性
animation.setDuration(1000);
animation.setStartOffset(500);
// 為Animation對(duì)象設(shè)置監(jiān)聽(tīng)器
animation.setAnimationListener(new RemoveAnimationListener());
imageView.startAnimation(animation);
}
}
private class RemoveAnimationListener implements AnimationListener {
// 該方法在淡出效果執(zhí)行結(jié)束之后被調(diào)用
@Override
public void onAnimationEnd(Animation animation) {
System.out.println("end");
// 從viewGroup當(dāng)中刪除掉imageView控件
viewGroup.removeView(imageView);
}
@Override
public void onAnimationRepeat(Animation animation) {
System.out.println("repeat");
}
@Override
public void onAnimationStart(Animation animation) {
System.out.println("start");
}
}
}