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

Android 使用XML做動畫UI的深入解析

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

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

         

第二步: 加載動畫
接著在Activity創(chuàng)建一個Animation類,然后使用AnimationUtils類加載動畫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);

// 加載動畫
animFadein = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.fade_in);



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

@Override
public void onAnimationEnd(Animation animation) {
// 在動畫結(jié)束后使用

// check for fade in animation
if (animation == animFadein) {
Toast.makeText(getApplicationContext(), "Animation Stopped",
Toast.LENGTH_SHORT).show();
}

}

@Override
public void onAnimationRepeat(Animation animation) {
//當動畫重復(fù)時使用

}

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

}

最后一步: 讓動畫動起來啦。可以使用任何UI元素調(diào)用startAnimation方法。
以下是一個Textview元素調(diào)用的。
txtMessage.startAnimation(animFadein);
完整代碼:
復(fù)制代碼 代碼如下:

FadeInActivity(淡入動畫)
?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);

        // 加載動畫
        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);

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

    }

    @Override
    public void onAnimationEnd(Animation animation) {
        // 在動畫結(jié)束后使用

        // check for fade in animation
        if (animation == animFadein) {
            Toast.makeText(getApplicationContext(), "Animation Stopped",
                    Toast.LENGTH_SHORT).show();
        }

    }

    @Override
    public void onAnimationRepeat(Animation animation) {
        //當動畫重復(fù)時使用

    }

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

    }

}

一些重要的XML屬性
重要的XML動畫屬性
android:duration 動畫持續(xù)時間,時間以毫秒為單位
android:startOffset 動畫之間的時間間隔,從上次動畫停多少時間開始執(zhí)行下個動畫
android:interpolator 指定一個動畫的插入器
android:fillAfter 當設(shè)置為true ,該動畫轉(zhuǎn)化在動畫結(jié)束后被應(yīng)用
android:repeatMode 定義重復(fù)的行為
android:repeatCount 動畫的重復(fù)次數(shù)
 
以下是一些基本的動畫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:  交叉的淡入和淡出
 同時使用Fade in和Fade out可以達到交叉的效果
復(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>

還有幾個就不再列出,有興趣下源碼看。點擊下載

相關(guān)文章

  • Android編程實現(xiàn)將壓縮數(shù)據(jù)庫文件拷貝到安裝目錄的方法

    Android編程實現(xiàn)將壓縮數(shù)據(jù)庫文件拷貝到安裝目錄的方法

    這篇文章主要介紹了Android編程實現(xiàn)將壓縮數(shù)據(jù)庫文件拷貝到安裝目錄的方法,涉及Android處理壓縮文件的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-10-10
  • 詳解Android跨進程IPC通信AIDL機制原理

    詳解Android跨進程IPC通信AIDL機制原理

    本篇文章主要介紹了詳解Android跨進程IPC通信AIDL機制原理,詳細的介紹了AIDL的概念和使用,具有一定的參考價值,有興趣的可以了解一下
    2018-01-01
  • Android Studio報錯unable to access android sdk add-on list解決方案

    Android Studio報錯unable to access android sdk add-on list解決方案

    這篇文章主要介紹了Android Studio報錯unable to access android sdk add-on list解決方案,本文通過多種方式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-03-03
  • Android Jetpack- Paging的使用詳解

    Android Jetpack- Paging的使用詳解

    這篇文章主要介紹了Android Jetpack- Paging的使用詳解,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-11-11
  • 安卓11適配攻略搶先看

    安卓11適配攻略搶先看

    這篇文章主要介紹了安卓11適配攻略搶先看,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-10-10
  • Android Dialog對話框用法實例詳解

    Android Dialog對話框用法實例詳解

    這篇文章主要介紹了Android Dialog對話框用法,結(jié)合實例形式分析了Android使用Dialog對話框過程中所涉及的創(chuàng)建、保存、回復(fù)等操作相關(guān)技巧與注意事項,需要的朋友可以參考下
    2016-07-07
  • Android開發(fā)準確獲取手機IP地址的兩種方式

    Android開發(fā)準確獲取手機IP地址的兩種方式

    這篇文章主要介紹了Android開發(fā)準確獲取手機IP地址的兩種方式,需要的朋友可以參考下
    2020-03-03
  • Android抽屜布局DrawerLayout的簡單使用

    Android抽屜布局DrawerLayout的簡單使用

    這篇文章主要為大家詳細介紹了Android抽屜布局DrawerLayout的簡單使用,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-06-06
  • Android  ListView 條目多樣式展示實例詳解

    Android ListView 條目多樣式展示實例詳解

    這篇文章主要介紹了Android ListView 條目多樣式展示的相關(guān)資料,需要的朋友可以參考下
    2017-04-04
  • android繪制幾何圖形的實例代碼

    android繪制幾何圖形的實例代碼

    這篇文章主要為大家詳細介紹了android繪制幾何圖形的實例代碼 ,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-11-11

最新評論