Android 使用XML做動(dòng)畫(huà)UI的深入解析
效果: http://www.56.com/u82/v_OTM4MDk5MTk.html
第一步: 創(chuàng)建anim文件夾放置動(dòng)畫(huà)xml文件
在res文件夾下,創(chuàng)建一個(gè)anim的子文件夾。

第二步: 加載動(dòng)畫(huà)
接著在Activity創(chuàng)建一個(gè)Animation類(lèi),然后使用AnimationUtils類(lèi)加載動(dòng)畫(huà)xml
Animation animFadein;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fadein);
txtMessage = (TextView) findViewById(R.id.txtMessage);
btnStart = (Button) findViewById(R.id.btnStart);
// 加載動(dòng)畫(huà)
animFadein = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.fade_in);
}
第三步: 設(shè)置動(dòng)畫(huà)監(jiān)聽(tīng)器
如果你要監(jiān)聽(tīng)動(dòng)畫(huà)的事件,如開(kāi)始,結(jié)束等,你需要實(shí)現(xiàn)AnimationListener監(jiān)聽(tīng)器,重寫(xiě)以下方法。
onAnimationEnd(Animation animation) - 當(dāng)動(dòng)畫(huà)結(jié)束時(shí)調(diào)用
onAnimationRepeat(Animation animation) - 當(dāng)動(dòng)畫(huà)重復(fù)時(shí)調(diào)用
onAniamtionStart(Animation animation) - 當(dāng)動(dòng)畫(huà)啟動(dòng)時(shí)調(diào)用
@Override
public void onAnimationEnd(Animation animation) {
// 在動(dòng)畫(huà)結(jié)束后使用
// check for fade in animation
if (animation == animFadein) {
Toast.makeText(getApplicationContext(), "Animation Stopped",
Toast.LENGTH_SHORT).show();
}
}
@Override
public void onAnimationRepeat(Animation animation) {
//當(dāng)動(dòng)畫(huà)重復(fù)時(shí)使用
}
@Override
public void onAnimationStart(Animation animation) {
//當(dāng)動(dòng)畫(huà)開(kāi)始使用
}
最后一步: 讓動(dòng)畫(huà)動(dòng)起來(lái)啦??梢允褂萌魏蜺I元素調(diào)用startAnimation方法。
以下是一個(gè)Textview元素調(diào)用的。
txtMessage.startAnimation(animFadein);
完整代碼:
FadeInActivity(淡入動(dòng)畫(huà))
?package com.chaowen.androidanimations;
import info.androidhive.androidanimations.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.Animation.AnimationListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
/**
*
* @author chaowen
*
*/
public class FadeInActivity extends Activity implements AnimationListener {
TextView txtMessage;
Button btnStart;
Animation animFadein;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fadein);
txtMessage = (TextView) findViewById(R.id.txtMessage);
btnStart = (Button) findViewById(R.id.btnStart);
// 加載動(dòng)畫(huà)
animFadein = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.fade_in);
// 設(shè)置監(jiān)聽(tīng)
animFadein.setAnimationListener(this);
// 按鈕
btnStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
txtMessage.setVisibility(View.VISIBLE);
// 開(kāi)始動(dòng)畫(huà)
txtMessage.startAnimation(animFadein);
}
});
}
@Override
public void onAnimationEnd(Animation animation) {
// 在動(dòng)畫(huà)結(jié)束后使用
// check for fade in animation
if (animation == animFadein) {
Toast.makeText(getApplicationContext(), "Animation Stopped",
Toast.LENGTH_SHORT).show();
}
}
@Override
public void onAnimationRepeat(Animation animation) {
//當(dāng)動(dòng)畫(huà)重復(fù)時(shí)使用
}
@Override
public void onAnimationStart(Animation animation) {
//當(dāng)動(dòng)畫(huà)開(kāi)始使用
}
}
一些重要的XML屬性
重要的XML動(dòng)畫(huà)屬性
android:duration 動(dòng)畫(huà)持續(xù)時(shí)間,時(shí)間以毫秒為單位
android:startOffset 動(dòng)畫(huà)之間的時(shí)間間隔,從上次動(dòng)畫(huà)停多少時(shí)間開(kāi)始執(zhí)行下個(gè)動(dòng)畫(huà)
android:interpolator 指定一個(gè)動(dòng)畫(huà)的插入器
android:fillAfter 當(dāng)設(shè)置為true ,該動(dòng)畫(huà)轉(zhuǎn)化在動(dòng)畫(huà)結(jié)束后被應(yīng)用
android:repeatMode 定義重復(fù)的行為
android:repeatCount 動(dòng)畫(huà)的重復(fù)次數(shù)
以下是一些基本的動(dòng)畫(huà)XML.
Fade In: 淡入
alpha是漸變透明度效果,值由0到1
fade_in.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true" >
<alpha
android:duration="1000"
android:fromAlpha="0.0"
android:interpolator="@android:anim/accelerate_interpolator"
android:toAlpha="1.0" />
</set>
Fade Out : 淡出
以Fade In剛好相反,值由1到0.
fade_out.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true" >
<alpha
android:duration="1000"
android:fromAlpha="1.0"
android:interpolator="@android:anim/accelerate_interpolator"
android:toAlpha="0.0" />
</set>
Cross Fading: 交叉的淡入和淡出
同時(shí)使用Fade in和Fade out可以達(dá)到交叉的效果
public class CrossfadeActivity extends Activity implements AnimationListener {
TextView txtMessage1, txtMessage2;
Button btnStart;
Animation animFadeIn, animFadeOut;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_crossfade);
txtMessage1 = (TextView) findViewById(R.id.txtMessage1);
txtMessage2 = (TextView) findViewById(R.id.txtMessage2);
btnStart = (Button) findViewById(R.id.btnStart);
// load animations
animFadeIn = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.fade_in);
animFadeOut = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.fade_out);
// set animation listeners
animFadeIn.setAnimationListener(this);
animFadeOut.setAnimationListener(this);
// button click event
btnStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
txtMessage2.setVisibility(View.VISIBLE);
txtMessage2.startAnimation(animFadeIn);
txtMessage1.startAnimation(animFadeOut);
}
});
}
@Override
public void onAnimationEnd(Animation animation) {
if (animation == animFadeOut) {
txtMessage1.setVisibility(View.GONE);
}
if(animation == animFadeIn){
txtMessage2.setVisibility(View.VISIBLE);
}
}
@Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
}
BLink: 若隱若現(xiàn),酷
blink.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha android:fromAlpha="0.0"
android:toAlpha="1.0"
android:interpolator="@android:anim/accelerate_interpolator"
android:duration="600"
android:repeatMode="reverse"
android:repeatCount="infinite"/>
</set>
Zoom In : 放大
zoom_in.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true" >
<scale
xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:fromXScale="1"
android:fromYScale="1"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="3"
android:toYScale="3" >
</scale>
</set>
Zoom Out: 縮小
zoom_out.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true" >
<scale
xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:fromXScale="1.0"
android:fromYScale="1.0"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="0.5"
android:toYScale="0.5" >
</scale>
</set>
Rotate: 旋轉(zhuǎn)
rotate.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate android:fromDegrees="0"
android:toDegrees="360"
android:pivotX="50%"
android:pivotY="50%"
android:duration="600"
android:repeatMode="restart"
android:repeatCount="infinite"
android:interpolator="@android:anim/cycle_interpolator"/>
</set>
還有幾個(gè)就不再列出,有興趣下源碼看。點(diǎn)擊下載
- Android Tween動(dòng)畫(huà)之RotateAnimation實(shí)現(xiàn)圖片不停旋轉(zhuǎn)效果實(shí)例介紹
- Android 動(dòng)畫(huà)之RotateAnimation應(yīng)用詳解
- Android 動(dòng)畫(huà)之TranslateAnimation應(yīng)用詳解
- Android 動(dòng)畫(huà)之ScaleAnimation應(yīng)用詳解
- Android 動(dòng)畫(huà)之AlphaAnimation應(yīng)用詳解
- 三款A(yù)ndroid炫酷Loading動(dòng)畫(huà)組件推薦
- Android實(shí)現(xiàn)Activity界面切換添加動(dòng)畫(huà)特效的方法
- Android啟動(dòng)畫(huà)面的實(shí)現(xiàn)方法
- Android 吸入動(dòng)畫(huà)效果實(shí)現(xiàn)分解
- Android動(dòng)畫(huà)之3D翻轉(zhuǎn)效果實(shí)現(xiàn)函數(shù)分析
- Android編程實(shí)現(xiàn)RotateAnimation設(shè)置中心點(diǎn)旋轉(zhuǎn)動(dòng)畫(huà)效果
相關(guān)文章
Android下2d物理引擎Box2d用法簡(jiǎn)單實(shí)例
這篇文章主要介紹了Android下2d物理引擎Box2d用法,實(shí)例分析了在Android平臺(tái)上使用Box2d的基本技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07
android 仿微信聊天氣泡效果實(shí)現(xiàn)思路
微信聊天窗口的信息效果類(lèi)似iphone上的短信效果,以氣泡的形式展現(xiàn),實(shí)現(xiàn)這種效果主要用到ListView和BaseAdapter,配合布局以及相關(guān)素材,接下來(lái)為大家介紹下如何實(shí)現(xiàn)2013-04-04
HttpClient通過(guò)Post上傳文件的實(shí)例代碼
這篇文章主要介紹了HttpClient通過(guò)Post上傳文件的實(shí)例代碼的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-08-08
Android音頻錄制MediaRecorder之簡(jiǎn)易的錄音軟件實(shí)現(xiàn)代碼
這篇文章主要介紹了Android音頻錄制MediaRecorder之簡(jiǎn)易的錄音軟件實(shí)現(xiàn)代碼,有需要的朋友可以參考一下2014-01-01
Android編程調(diào)用Camera和相冊(cè)功能詳解
這篇文章主要介紹了Android編程調(diào)用Camera和相冊(cè)功能,結(jié)合實(shí)例形式分析了Android的拍照及相冊(cè)調(diào)用功能相關(guān)實(shí)現(xiàn)技巧與操作注意事項(xiàng),需要的朋友可以參考下2017-02-02

