Android动画之Tween动画实战
生活随笔
收集整理的這篇文章主要介紹了
Android动画之Tween动画实战
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
Android動(dòng)畫分為Tween動(dòng)畫和Frame動(dòng)畫,上一節(jié)通過一個(gè)實(shí)例介紹了Frame動(dòng)畫,本節(jié)將介紹Tween動(dòng)畫。Tween可以把對(duì)象進(jìn)行縮小、放大、旋轉(zhuǎn)和漸變等操作。 Tween動(dòng)畫有四個(gè)主要的實(shí)現(xiàn),下面分別說(shuō)明下: 1、AlphaAnimation:漸變動(dòng)畫,主要控制透明度變化動(dòng)畫類,常使用AlphaAnimation(float fromAlpha, float toAlpha)來(lái)構(gòu)造; fromAlpha:動(dòng)畫開始時(shí)的透明度(取值范圍為0.0到1.0); toAlpha:動(dòng)畫結(jié)束時(shí)的透明度; 2、ScaleAnimation:主要控制尺度變化的動(dòng)畫類,常使用ScaleAnimation(float fromX, float toX, float fromY, float toY, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)來(lái)構(gòu)造; fromX:動(dòng)畫開始X坐標(biāo)上的伸縮尺度; toX:動(dòng)畫結(jié)束X坐標(biāo)上的伸縮尺度; fromY:動(dòng)畫開始Y坐標(biāo)上的伸縮尺度; toY:動(dòng)畫結(jié)束Y坐標(biāo)上的伸縮尺度; pivotXType:X坐標(biāo)上的伸縮模式,取值有:Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, Animation.RELATIVE_TO_PARENT; pivotXValue:X坐標(biāo)上的伸縮值; pivotYType:Y坐標(biāo)上的伸縮模式,取值有:Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, Animation.RELATIVE_TO_PARENT; pivotYValue:Y坐標(biāo)上的伸縮值; 3、TranslateAnimation:主要控制位置變換的動(dòng)畫實(shí)現(xiàn)類,常使用TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta)來(lái)構(gòu)造; fromXDelta:動(dòng)畫開始的X坐標(biāo); toXDelta:動(dòng)畫結(jié)束的X坐標(biāo); fromYDelta:動(dòng)畫開始的Y坐標(biāo); toYDelta:動(dòng)畫結(jié)束的Y坐標(biāo); 4、RotateAnimation:主要控制旋轉(zhuǎn)的動(dòng)畫實(shí)現(xiàn)類,常使用RotateAnimation(float fromDegrees, float toDegrees, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)來(lái)構(gòu)造; fromDegrees:旋轉(zhuǎn)開始角度; toDegrees:旋轉(zhuǎn)結(jié)束角度; pivotXType, pivotXValue, pivotYType, pivotYValue與尺度變化動(dòng)畫ScaleAnimation類似; 下面以一個(gè)實(shí)例來(lái)進(jìn)行這些動(dòng)畫。 1、加入將要進(jìn)行動(dòng)畫變化的MM圖片(為了避免侵犯他人肖像權(quán),這次的MM和上次的桌面背景不一樣,這個(gè)MM沒有頭像的): 2、創(chuàng)建Layout布局XML配置tween.xml文件:? <?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:apk="http://schemas.android.com/apk/res/android" apk:orientation="vertical" apk:layout_width="fill_parent" apk:layout_height="fill_parent">
<!-- 動(dòng)畫MM -->
<ImageView apk:id="@+id/TweenMM" apk:src="@drawable/tween" apk:layout_width="wrap_content" apk:layout_height="wrap_content"/>
<!-- 動(dòng)畫控制按鈕 -->
<LinearLayout apk:layout_weight="1" apk:orientation="horizontal" apk:layout_width="fill_parent" apk:layout_height="wrap_content">
<Button apk:text="ScaleAnim" apk:layout_weight="1" apk:layout_width="fill_parent" apk:layout_height="wrap_content" apk:onClick="onBtnScaleAnimClick"/>
<Button apk:text="AlphaAnim" apk:layout_weight="1" apk:layout_width="fill_parent" apk:layout_height="wrap_content" apk:onClick="onBtnAlphaAnimClick"/>
</LinearLayout>
<LinearLayout apk:layout_weight="1" apk:orientation="horizontal" apk:layout_width="fill_parent" apk:layout_height="wrap_content">
<Button apk:text="TranslateAnim" apk:layout_weight="1" apk:layout_width="wrap_content" apk:layout_height="wrap_content" apk:onClick="onBtnTranslateAnimClick"/>
<Button apk:text="RotateAnim" apk:layout_weight="1" apk:layout_width="wrap_content" apk:layout_height="wrap_content" apk:onClick="onBtnRotateAnimClick"/>
</LinearLayout>
</LinearLayout> 一張圖片和4個(gè)按鈕,這4個(gè)按鈕就是控制圖片動(dòng)畫的。 3、對(duì)于每個(gè)動(dòng)畫,我們都以一個(gè)XML配置文件來(lái)設(shè)置各自動(dòng)畫的屬性。 AlphaAnimation(tween_alpha.xml)的配置文件內(nèi)容:
<?xml version="1.0" encoding="UTF-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha android:fromAlpha="0.1" android:toAlpha="1.0" android:duration="5000" />
</set> android:duration指示該動(dòng)畫持續(xù)的時(shí)間,以毫秒為單位。 RotateAnimation(tween_rotate.xml)的配置文件內(nèi)容: <?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="5000" />
</set> ScaleAnimation(tween_scale.xml)的配置文件內(nèi)容: <?xml version="1.0" encoding="UTF-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale android:fromXScale="0.0" android:toXScale="1.0" android:fromYScale="0.0" android:toYScale="1.0" android:pivotX="50%" android:pivotY="50%" android:duration="5000" />
</set> TranslateAnimation(tween_translate.xml)的配置文件內(nèi)容: <?xml version="1.0" encoding="UTF-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="10" android:toXDelta="100" android:fromYDelta="10" android:toYDelta="100" />
</set>
4、Activity界面類:
/*** Copyright (c) 2004-2011 All Rights Reserved.*/ package com.aboy.android.study.animation;import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.animation.Animation; import android.view.animation.AnimationUtils; import android.widget.ImageView;import com.aboy.android.study.R;/*** 通過XML配置文件的方式實(shí)現(xiàn)Tween動(dòng)畫** @author obullxl@gmail.com* @version $Id: TweenXMLActivity.java, v 0.1 2011-6-11 下午01:36:39 oldbulla Exp $*/ public class TweenXMLActivity extends Activity {public static final String TAG = "TweenActivity";// 動(dòng)畫圖片private ImageView tweenMM;/** * @see android.app.Activity#onCreate(android.os.Bundle)*/public void onCreate(Bundle cycle) {super.onCreate(cycle);super.setContentView(R.layout.tween);// 取得動(dòng)畫圖片this.tweenMM = (ImageView) super.findViewById(R.id.TweenMM);}/*** 按鈕:尺寸變化動(dòng)畫*/public void onBtnScaleAnimClick(View view) {// 動(dòng)畫開始this.doStartAnimation(R.anim.tween_scale);}/*** 按鈕:漸變動(dòng)畫*/public void onBtnAlphaAnimClick(View view) {// 動(dòng)畫開始this.doStartAnimation(R.anim.tween_alpha);}/*** 按鈕:位置變化動(dòng)畫*/public void onBtnTranslateAnimClick(View view) {// 動(dòng)畫開始this.doStartAnimation(R.anim.tween_translate);}/*** 按鈕:旋轉(zhuǎn)動(dòng)畫*/public void onBtnRotateAnimClick(View view) {// 動(dòng)畫開始this.doStartAnimation(R.anim.tween_rotate);}/*** 開始動(dòng)畫*/private void doStartAnimation(int animId) {// 加載動(dòng)畫Animation animation = AnimationUtils.loadAnimation(this, animId);// 動(dòng)畫開始this.tweenMM.startAnimation(animation);}} 使用AnimationUtils類,可以非常方便的構(gòu)造動(dòng)畫類; 開始動(dòng)畫,只要ImageView.startAnimation即可。?
<LinearLayout xmlns:apk="http://schemas.android.com/apk/res/android" apk:orientation="vertical" apk:layout_width="fill_parent" apk:layout_height="fill_parent">
<!-- 動(dòng)畫MM -->
<ImageView apk:id="@+id/TweenMM" apk:src="@drawable/tween" apk:layout_width="wrap_content" apk:layout_height="wrap_content"/>
<!-- 動(dòng)畫控制按鈕 -->
<LinearLayout apk:layout_weight="1" apk:orientation="horizontal" apk:layout_width="fill_parent" apk:layout_height="wrap_content">
<Button apk:text="ScaleAnim" apk:layout_weight="1" apk:layout_width="fill_parent" apk:layout_height="wrap_content" apk:onClick="onBtnScaleAnimClick"/>
<Button apk:text="AlphaAnim" apk:layout_weight="1" apk:layout_width="fill_parent" apk:layout_height="wrap_content" apk:onClick="onBtnAlphaAnimClick"/>
</LinearLayout>
<LinearLayout apk:layout_weight="1" apk:orientation="horizontal" apk:layout_width="fill_parent" apk:layout_height="wrap_content">
<Button apk:text="TranslateAnim" apk:layout_weight="1" apk:layout_width="wrap_content" apk:layout_height="wrap_content" apk:onClick="onBtnTranslateAnimClick"/>
<Button apk:text="RotateAnim" apk:layout_weight="1" apk:layout_width="wrap_content" apk:layout_height="wrap_content" apk:onClick="onBtnRotateAnimClick"/>
</LinearLayout>
</LinearLayout> 一張圖片和4個(gè)按鈕,這4個(gè)按鈕就是控制圖片動(dòng)畫的。 3、對(duì)于每個(gè)動(dòng)畫,我們都以一個(gè)XML配置文件來(lái)設(shè)置各自動(dòng)畫的屬性。 AlphaAnimation(tween_alpha.xml)的配置文件內(nèi)容:
<?xml version="1.0" encoding="UTF-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha android:fromAlpha="0.1" android:toAlpha="1.0" android:duration="5000" />
</set> android:duration指示該動(dòng)畫持續(xù)的時(shí)間,以毫秒為單位。 RotateAnimation(tween_rotate.xml)的配置文件內(nèi)容: <?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="5000" />
</set> ScaleAnimation(tween_scale.xml)的配置文件內(nèi)容: <?xml version="1.0" encoding="UTF-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale android:fromXScale="0.0" android:toXScale="1.0" android:fromYScale="0.0" android:toYScale="1.0" android:pivotX="50%" android:pivotY="50%" android:duration="5000" />
</set> TranslateAnimation(tween_translate.xml)的配置文件內(nèi)容: <?xml version="1.0" encoding="UTF-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="10" android:toXDelta="100" android:fromYDelta="10" android:toYDelta="100" />
</set>
4、Activity界面類:
/*** Copyright (c) 2004-2011 All Rights Reserved.*/ package com.aboy.android.study.animation;import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.animation.Animation; import android.view.animation.AnimationUtils; import android.widget.ImageView;import com.aboy.android.study.R;/*** 通過XML配置文件的方式實(shí)現(xiàn)Tween動(dòng)畫** @author obullxl@gmail.com* @version $Id: TweenXMLActivity.java, v 0.1 2011-6-11 下午01:36:39 oldbulla Exp $*/ public class TweenXMLActivity extends Activity {public static final String TAG = "TweenActivity";// 動(dòng)畫圖片private ImageView tweenMM;/** * @see android.app.Activity#onCreate(android.os.Bundle)*/public void onCreate(Bundle cycle) {super.onCreate(cycle);super.setContentView(R.layout.tween);// 取得動(dòng)畫圖片this.tweenMM = (ImageView) super.findViewById(R.id.TweenMM);}/*** 按鈕:尺寸變化動(dòng)畫*/public void onBtnScaleAnimClick(View view) {// 動(dòng)畫開始this.doStartAnimation(R.anim.tween_scale);}/*** 按鈕:漸變動(dòng)畫*/public void onBtnAlphaAnimClick(View view) {// 動(dòng)畫開始this.doStartAnimation(R.anim.tween_alpha);}/*** 按鈕:位置變化動(dòng)畫*/public void onBtnTranslateAnimClick(View view) {// 動(dòng)畫開始this.doStartAnimation(R.anim.tween_translate);}/*** 按鈕:旋轉(zhuǎn)動(dòng)畫*/public void onBtnRotateAnimClick(View view) {// 動(dòng)畫開始this.doStartAnimation(R.anim.tween_rotate);}/*** 開始動(dòng)畫*/private void doStartAnimation(int animId) {// 加載動(dòng)畫Animation animation = AnimationUtils.loadAnimation(this, animId);// 動(dòng)畫開始this.tweenMM.startAnimation(animation);}} 使用AnimationUtils類,可以非常方便的構(gòu)造動(dòng)畫類; 開始動(dòng)畫,只要ImageView.startAnimation即可。?
轉(zhuǎn)載于:https://www.cnblogs.com/obullxl/archive/2011/06/13/android-animation-tween-lady.html
總結(jié)
以上是生活随笔為你收集整理的Android动画之Tween动画实战的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 新手如何买房?
- 下一篇: mathematica实现闭包