欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android 使用XML做動(dòng)畫UI的深入解析

 更新時(shí)間:2013年07月11日 08:57:24   作者:  
在Android應(yīng)用程序,使用動(dòng)畫效果,能帶給用戶更好的感覺。做動(dòng)畫可以通過XML或Android代碼。本教程中,介紹使用XML來做動(dòng)畫。在這里,介紹基本的動(dòng)畫,如淡入,淡出,旋轉(zhuǎn)等,需要的朋友可以參考下

效果: http://www.56.com/u82/v_OTM4MDk5MTk.html
第一步: 創(chuàng)建anim文件夾放置動(dòng)畫xml文件
在res文件夾下,創(chuàng)建一個(gè)anim的子文件夾。

         

第二步: 加載動(dòng)畫
接著在Activity創(chuàng)建一個(gè)Animation類,然后使用AnimationUtils類加載動(dòng)畫xml

復(fù)制代碼 代碼如下:

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)畫
animFadein = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.fade_in);



第三步: 設(shè)置動(dòng)畫監(jiān)聽器
如果你要監(jiān)聽動(dòng)畫的事件,如開始,結(jié)束等,你需要實(shí)現(xiàn)AnimationListener監(jiān)聽器,重寫以下方法。
onAnimationEnd(Animation animation) - 當(dāng)動(dòng)畫結(jié)束時(shí)調(diào)用
onAnimationRepeat(Animation animation) - 當(dāng)動(dòng)畫重復(fù)時(shí)調(diào)用
onAniamtionStart(Animation animation) - 當(dāng)動(dòng)畫啟動(dòng)時(shí)調(diào)用
復(fù)制代碼 代碼如下:

@Override
public void onAnimationEnd(Animation animation) {
// 在動(dòng)畫結(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)畫重復(fù)時(shí)使用

}

@Override
public void onAnimationStart(Animation animation) {
//當(dāng)動(dòng)畫開始使用

}

最后一步: 讓動(dòng)畫動(dòng)起來啦??梢允褂萌魏蜺I元素調(diào)用startAnimation方法。
以下是一個(gè)Textview元素調(diào)用的。
txtMessage.startAnimation(animFadein);
完整代碼:
復(fù)制代碼 代碼如下:

FadeInActivity(淡入動(dòng)畫)
?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)畫
        animFadein = AnimationUtils.loadAnimation(getApplicationContext(),
                R.anim.fade_in);

        // 設(shè)置監(jiān)聽
        animFadein.setAnimationListener(this);

        // 按鈕
        btnStart.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                txtMessage.setVisibility(View.VISIBLE);

                // 開始動(dòng)畫
                txtMessage.startAnimation(animFadein);
            }
        });

    }

    @Override
    public void onAnimationEnd(Animation animation) {
        // 在動(dòng)畫結(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)畫重復(fù)時(shí)使用

    }

    @Override
    public void onAnimationStart(Animation animation) {
        //當(dāng)動(dòng)畫開始使用

    }

}

一些重要的XML屬性
重要的XML動(dòng)畫屬性
android:duration 動(dòng)畫持續(xù)時(shí)間,時(shí)間以毫秒為單位
android:startOffset 動(dòng)畫之間的時(shí)間間隔,從上次動(dòng)畫停多少時(shí)間開始執(zhí)行下個(gè)動(dòng)畫
android:interpolator 指定一個(gè)動(dòng)畫的插入器
android:fillAfter 當(dāng)設(shè)置為true ,該動(dòng)畫轉(zhuǎn)化在動(dòng)畫結(jié)束后被應(yīng)用
android:repeatMode 定義重復(fù)的行為
android:repeatCount 動(dòng)畫的重復(fù)次數(shù)
 
以下是一些基本的動(dòng)畫XML.
Fade In:  淡入
alpha是漸變透明度效果,值由0到1
fade_in.xml 
復(fù)制代碼 代碼如下:

<?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
復(fù)制代碼 代碼如下:

 <?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á)到交叉的效果
復(fù)制代碼 代碼如下:

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
復(fù)制代碼 代碼如下:

 <?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 
復(fù)制代碼 代碼如下:

<?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 
復(fù)制代碼 代碼如下:

<?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
復(fù)制代碼 代碼如下:

 <?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)擊下載

相關(guān)文章

最新評論