Android使用Animation技巧講解
Android 使用Animation的具體操作方法我們將會(huì)在這篇文章中做一個(gè)詳細(xì)的介紹。大家可以通過(guò)這里舉出的代碼進(jìn)行解讀,并從中了解到相關(guān)操作技巧,方便我們將來(lái)開(kāi)發(fā)應(yīng)用,并且加深對(duì)這一操作系統(tǒng)的理解程度。#t#
在Android中,分別可以在xml中定義Animation,也可以在程序代碼中定義,下面的小例子是利用RotateAnimation簡(jiǎn)單展示一下兩種Android使用Animation的方法,對(duì)于其他動(dòng)畫(huà),如ScaleAnimation,AlphaAnimation,原理是一樣的。
Android使用Animation方法一:在xml中定義動(dòng)畫(huà):
Xml代碼
- < ?xml version="1.0" encoding="utf-8"?>
- < set xmlns:android=
"http://schemas.android.com/apk/res/android">- < rotate
- android:interpolator="@android:anim/accelerate_
decelerate_interpolator"- android:fromDegrees="0"
- android:toDegrees="+360"
- android:duration="3000" />
- < !-- rotate 旋轉(zhuǎn)動(dòng)畫(huà)效果
- 屬性:interpolator 指定一個(gè)動(dòng)畫(huà)的插入器,用來(lái)控制動(dòng)畫(huà)的速度變化
- fromDegrees 屬性為動(dòng)畫(huà)起始時(shí)物件的角度
- toDegrees 屬性為動(dòng)畫(huà)結(jié)束時(shí)物件旋轉(zhuǎn)的角度,+代表順時(shí)針
- duration 屬性為動(dòng)畫(huà)持續(xù)時(shí)間,以毫秒為單位
- -->
- < /set>
- < ?xml version="1.0" encoding="utf-8"?>
- < set xmlns:android=
"http://schemas.android.com/apk/res/android">- < rotate
- android:interpolator=
"@android:anim/accelerate_decelerate_interpolator"- android:fromDegrees="0"
- android:toDegrees="+360"
- android:duration="3000" />
- < !-- rotate 旋轉(zhuǎn)動(dòng)畫(huà)效果
- 屬性:interpolator 指定一個(gè)動(dòng)畫(huà)的插入器,用來(lái)控制動(dòng)畫(huà)的速度變化
- fromDegrees 屬性為動(dòng)畫(huà)起始時(shí)物件的角度
- toDegrees 屬性為動(dòng)畫(huà)結(jié)束時(shí)物件旋轉(zhuǎn)的角度,+代表順時(shí)針
- duration 屬性為動(dòng)畫(huà)持續(xù)時(shí)間,以毫秒為單位
- -->
- < /set>
使用動(dòng)畫(huà)的Java代碼,程序的效果是點(diǎn)擊按鈕,TextView旋轉(zhuǎn)一周:
Java代碼
- package com.ray.animation;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.view.animation.Animation;
- import android.view.animation.AnimationUtils;
- import android.widget.Button;
- import android.widget.TextView;
- public class TestAnimation extends Activity
implements OnClickListener{- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- Button btn = (Button)findViewById(R.id.Button01);
- btn.setOnClickListener(this);
- }
- @Override
- public void onClick(View v) {
- Animation anim = AnimationUtils.loadAnimation(this,
R.anim.my_rotate_action);- findViewById(R.id.TextView01).startAnimation(anim);
- }
- }
- package com.ray.animation;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.view.animation.Animation;
- import android.view.animation.AnimationUtils;
- import android.widget.Button;
- import android.widget.TextView;
- public class TestAnimation extends Activity
implements OnClickListener{- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- Button btn = (Button)findViewById(R.id.Button01);
- btn.setOnClickListener(this);
- }
- @Override
- public void onClick(View v) {
- Animation anim = AnimationUtils.loadAnimation(this,
R.anim.my_rotate_action);- findViewById(R.id.TextView01).startAnimation(anim);
- }
- }
Android使用Animation方法二:直接在代碼中定義動(dòng)畫(huà)(效果跟方法一類似):
Java代碼
- package com.ray.animation;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.view.animation.AccelerateDecelerateInterpolator;
- import android.view.animation.Animation;
- import android.view.animation.RotateAnimation;
- import android.widget.Button;
- public class TestAnimation extends Activity
implements OnClickListener{- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- Button btn = (Button)findViewById(R.id.Button);
- btn.setOnClickListener(this);
- }
- public void onClick(View v) {
- Animation anim = null;
- anim = new RotateAnimation(0.0f,+360.0f);
- anim.setInterpolator(new AccelerateDecelerateInterpolator());
- anim.setDuration(3000);
- findViewById(R.id.TextView01).startAnimation(anim);
- }
- }
Android使用Animation相關(guān)實(shí)現(xiàn)方法就為大家介紹到這里。