animation 動(dòng)畫。
android 自帶的動(dòng)畫由四個(gè)屬性控制,alpha,scale,translate,rotate;

alpha:漸變透明度動(dòng)畫效果;
scale:漸變尺寸伸縮動(dòng)畫效果;
translate:畫面位置移動(dòng)動(dòng)畫效果‘
rotate:畫面轉(zhuǎn)移旋轉(zhuǎn)動(dòng)畫效果;
動(dòng)畫存放位置,res/anim
關(guān)于后面講解的動(dòng)畫執(zhí)行的位置圖片解,

屬性講解:
scale 調(diào)節(jié)尺寸

第一個(gè)Demo
在res--anim創(chuàng)建一個(gè)xml,這里我命名為start.xml
<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
<!--動(dòng)畫執(zhí)行時(shí)初始化的長(zhǎng)度-->
android:fromXScale="0.0"
<!--動(dòng)畫執(zhí)行后x軸的長(zhǎng)度,以1.0為不變,比這個(gè)值大就放大,反之縮小-->
android:toXScale="1.0"
<!--動(dòng)畫執(zhí)行時(shí)初始化的高度-->
android:fromYScale="0.0"
<!--動(dòng)畫執(zhí)行后Y軸的長(zhǎng)度,以1.0為不變,比這個(gè)值大就是方法,反之縮小-->
android:toYScale="1.0"
<!--百分比是控件本身長(zhǎng)度的百分比,X是指動(dòng)畫從X軸的那個(gè)位置開始展示-->
android:pivotX="100%"
<!--百分比是控件本身高度度的百分比,Y是指動(dòng)畫從Y軸的那個(gè)位置開始展示-->
android:pivotY="50%"
<!--動(dòng)畫執(zhí)行后是否保持當(dāng)前狀態(tài)-->
android:fillAfter="true"
<!-- 動(dòng)畫持續(xù)時(shí)間,以毫秒為單位 -->
android:duration="700"
<!-- 動(dòng)畫重復(fù)執(zhí)行多少次 ,注意:是重復(fù)3次,不是執(zhí)行三次,這里選擇3,就會(huì)執(zhí)行4次,一次是你點(diǎn)擊的 -->
android:repeatCount="3"
<!-- 動(dòng)畫重復(fù)的類型,reverse是執(zhí)行后按照原來(lái)的路徑回放,,restart是默認(rèn)的類型,就是執(zhí)行完后又重新開始。注意:選擇reverse的類型時(shí)候,回放而是算repeatCount的次數(shù)的,上面重復(fù)執(zhí)行3次,選reverse就會(huì)放大兩次,回放兩次-->
android:repeatMode="reverse/restart"
/>
下面是布局
<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"
>
<Button android:id="@+id/btn_animation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dip"
android:text="scale animation"/>
<TextView
android:id="@+id/tv"
android:layout_width="100dip"
android:layout_height="200dip"
android:background="#ff00ff"
android:text="hello_world"
android:layout_gravity="center_horizontal"/>
</LinearLayout>
MianActivity里的代碼,后面的demo的就不貼了,只是改了動(dòng)畫文件
public class MainActivity extends Activity {
Animation scaleAnimation;
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
scaleAnimation = AnimationUtils.loadAnimation(this, R.anim.start);
tv =(TextView)findViewById(R.id.tv);
findViewById(R.id.btn_animation).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
tv.startAnimation(scaleAnimation);
}
});
}
}
說(shuō)了scale,下面來(lái)說(shuō)alpha 透明度
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
//這是開始執(zhí)行動(dòng)畫時(shí)的透明度 從0.0 --1.0 ,0.0表示全透明,1.0表示完全不透明
android:fromAlpha="1.0"
//這是動(dòng)畫執(zhí)行后的透明度
android:toAlpha="0.1"
//動(dòng)畫執(zhí)行的時(shí)間
android:duration="1000"
//重復(fù)次數(shù)
android:repeatCount ="2"
//重復(fù)類型,reverse/restar(解釋看之前的start.xml)
android:repeatMode="restart"
android:fillAfter="true"
//動(dòng)畫執(zhí)行后是否還愿,這是默認(rèn)值,可以不用設(shè)置。如果設(shè)置了fillAfter這個(gè)效果就會(huì)失效
android:fillBefore="true">
</alpha>
rotate 旋轉(zhuǎn)
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
//初始角度
android:fromDegrees="0"
//旋轉(zhuǎn)角度
android:toDegrees="-380"
//X軸中心
android:pivotX="50%"
//Y軸中心
android:pivotY="50%"
//執(zhí)行動(dòng)畫時(shí)間
android:duration="200"
//執(zhí)行多少次
android:repeatCount ="10"
//重復(fù)類型
android:repeatMode="reverse"
//執(zhí)行后是否返回原來(lái)狀態(tài)
android:fillAfter="true">
</rotate>
translate 平移
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
//x軸動(dòng)畫執(zhí)行時(shí)的位置 就是第一張圖片的 x:0 y:0。
android:fromXDelta="0"
//X軸執(zhí)行的方向
android:toXDelta="-80"
//y軸動(dòng)畫執(zhí)行時(shí)的位置
android:fromYDelta="0"
//Y軸執(zhí)行的方向
android:toYDelta="-80"
//動(dòng)畫執(zhí)行的時(shí)間
android:duration="2000"
//是否恢復(fù)默認(rèn)狀態(tài)
android:fillBefore="true"
>
</translate>

這里需要對(duì)toXDelta/toYDelta的進(jìn)行講解一下,這里x軸是-80;y軸也是-80,在的紅色框就是你的控件初始化位置,從圖中可以看出,控件默認(rèn)的位置都是X軸為正,Y軸為負(fù),本身處于第四象限,根據(jù)初中知識(shí),當(dāng)你給X軸一個(gè)-80的時(shí)候,就代表你要X向負(fù)數(shù)方向移動(dòng);而y軸為本身就是負(fù)數(shù),負(fù)負(fù)為正,于是本來(lái)是負(fù)數(shù)的Y軸現(xiàn)在變成正數(shù),向上移動(dòng)。X軸為負(fù),Y軸為正,那么這段動(dòng)畫該向第二象限移動(dòng)。
可以改變著兩個(gè)值進(jìn)行測(cè)試一下。
