一對一語音聊天Andorid動畫——補間動畫與幀動畫

補間動畫——Tween
補間動畫(英譯也可稱爲漸變動畫)主要包括淡入淡出(透明度)——alpha、移動——translation、旋轉——rotation、縮放——scale。

從字面意思我們很容易看出各自代表的意思。

(1)淡入淡出效果就是透明度的設置,可以設置範圍是0到1.。1代表不透明,0代表透明;

(2)移動代表的是位移,從界面上A點移動到B點;

(3)旋轉代表的是圖片在界面上的旋轉,廣角度一般設置-360到360;

(4)縮放是對本圖片的縮放,放大縮小都可。

補間動畫的實現
補間動畫可以用代碼實現,也可以使用xml配置方式實現。
現在先看用代碼如何實現。
代碼實現
想要用代碼實現,首先要了解動畫實現,會涉及哪些類,如何使用這些類。
Animation類是動畫的基類,繼承它的子類有AnimationSet、Animation等。而具體到各個補間動畫,比如淡入淡出,也有AlphaAnimation類(Animation的子類),其他各個具體補間動畫也是類似的。
通俗的做法是:
(1)新建一個AnimationSet類對象,作爲容器。
(2)新建AlphaAnimation類對象,並將該對象加入到AnimationSet對象中。
(3)將要實現動畫效果的圖片,綁定AnimationSet,並啓動。
注意:
(1)你也可以在代碼中添加監聽,用於監聽動畫執行的狀態,開啓,結束等。
(2)動畫效果是可以混合使用的,代碼中有混合的介紹和範例。
這裏貼上補間動畫的四種實現代碼:

package com.example.animation_csdn;
 
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
 
public class TweenActivity extends Activity implements OnClickListener{
 
	Button alpha,rotation,transtion,scale,hunhe,config;
	ImageView img;
	AnimationSet animationSet;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_tween);
		 initView();
	}
	/**
	 * 初始化控件
	 */
	public void initView()
	{
		alpha=(Button)findViewById(R.id.alpha);
		rotation=(Button)findViewById(R.id.rotation);
		transtion=(Button)findViewById(R.id.translation);
		scale=(Button)findViewById(R.id.scale);
		hunhe=(Button)findViewById(R.id.hunhe);
		img=(ImageView)findViewById(R.id.img);
		config=(Button)findViewById(R.id.code);
		
		alpha.setOnClickListener(this);
		rotation.setOnClickListener(this);
		transtion.setOnClickListener(this);
		scale.setOnClickListener(this);
		hunhe.setOnClickListener(this);
		config.setOnClickListener(this);
	}
	
	/**
	 * 具體執行的動畫
	 * @param tag 標誌 
	 * 1 淡入淡出,
	 * 2 旋轉,
	 * 3 移動,
	 * 4 縮放,
	 * 5 混合動畫,
	 * 6 xml實現
	 */
	public void animationDo(int tag)
	{
		switch (tag) {
		case 1:
			animationSet=new AnimationSet(true);
			AlphaAnimation alphaAnimation=new AlphaAnimation(0f,1f);
			alphaAnimation.setDuration(1000);
			animationSet.addAnimation(alphaAnimation);
			animationSet.setAnimationListener(new listener());
			img.startAnimation(animationSet);
			break;
		case 2:
			animationSet=new AnimationSet(true);
			RotateAnimation rotateAnimation=new RotateAnimation(0f, 360f);
			rotateAnimation.setDuration(1000);
			animationSet.addAnimation(rotateAnimation);
			img.startAnimation(animationSet);
			break;
		case 3:
			animationSet=new AnimationSet(true);
			TranslateAnimation translateAnimation=new TranslateAnimation(0f, 180f, 0f, 90f);
			translateAnimation.setDuration(1000);
			animationSet.addAnimation(translateAnimation);
			img.startAnimation(animationSet);
			break;
		case 4:
			animationSet=new AnimationSet(true);
			ScaleAnimation scaleAnimation=new ScaleAnimation(Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
			scaleAnimation.setDuration(1000);
			animationSet.addAnimation(scaleAnimation);
			img.startAnimation(animationSet);
			break;
		case 5:
			animationSet=new AnimationSet(true);
			ScaleAnimation scaleAnimation1=new ScaleAnimation(Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
			scaleAnimation1.setDuration(1000);
			animationSet.addAnimation(scaleAnimation1);
			
			AlphaAnimation alphaAnimation2=new AlphaAnimation(1f, 0f);
			alphaAnimation2.setDuration(1000);
			animationSet.addAnimation(alphaAnimation2);
			img.startAnimation(animationSet);
			
			break;
		case 6:
			Animation animation = AnimationUtils.loadAnimation(
	                  this, R.anim.settween);
	           // 啓動動畫
	           img.startAnimation(animation);
			break;
 
		default:
			break;
		}
	}
	
	/**
	 * 動畫狀態監聽
	 * @author 戰國
	 *
	 */
	class listener implements AnimationListener
	{
 
		@Override
		public void onAnimationEnd(Animation animation) {
			// TODO Auto-generated method stub
			System.out.println("動畫結束");
		}
 
		@Override
		public void onAnimationRepeat(Animation animation) {
			// TODO Auto-generated method stub
			System.out.println("動畫重複播放");
		}
 
		@Override
		public void onAnimationStart(Animation animation) {
			// TODO Auto-generated method stub
			System.out.println("動畫開始");
		}
		
	}
	
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.alpha:
			animationDo(1);
			break;
		case R.id.rotation:
			animationDo(2);
			break;
		case R.id.translation:
			animationDo(3);
			break;
		case R.id.scale:
			animationDo(4);
			break;
		case R.id.hunhe:
			animationDo(5);
			break;
		case R.id.code:
			animationDo(6);
			break;
 
		default:
			break;
		}
	}
   
	
}

可以看到,具體執行動畫時,我們就只簡單的設置了幾個參數,一般是起始位置,結束位置,動畫時長。
那具體它們各自的參數有哪些講究,如下:

 1、setDuration(long durationMills)

設置動畫持續時間(單位:毫秒)
  2、setFillAfter(Boolean fillAfter)
  如果fillAfter的值爲true,則動畫執行後,控件將停留在執行結束的狀態
  3、setFillBefore(Boolean fillBefore)
  如果fillBefore的值爲true,則動畫執行後,控件將回到動畫執行之前的狀態
  4、setStartOffSet(long startOffSet)
  設置動畫執行之前的等待時間
  5、setRepeatCount(int repeatCount)
  設置動畫重複執行的次數

xml配置實現
這個的配置實現,是在res目錄下新建一個anim文件夾,並設置相應的xml。
xml格式如下:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator">
    <alpha 
        android:fromAlpha="1.0"
        android:toAlpha="0.0"
        android:startOffset="500"
        android:duration="1000"
        />
 
    <rotate
        android:fromDegrees="0"
        android:toDegrees="-360"
        android:duration="1000"
        ></rotate>
    
    <scale
        android:fromXScale="1.0"
        android:toXScale="0.0"
        android:fromYScale="1.0"
        android:toYScale="0.0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:duration="1000"
        ></scale>
    
    <translate
        android:fromXDelta="0%"
        android:toXDelta="100%"
        android:fromYDelta="0%"
        android:toYDelta="100%"
        android:duration="1000"
        ></translate>
</set>

在代碼中,引用該xml,並啓動動畫:

Animation animation = AnimationUtils.loadAnimation(
                  this, R.anim.settween);
           // 啓動動畫
           img.startAnimation(animation);

以上,我們就實現了補間動畫的代碼實現和配置實現,效果可以下載源碼觀看。

幀動畫——Frame
幀動畫在實際應用中,運用也是很多的。在很多app的過場動畫都使用的是幀動畫。
幀動畫同樣有兩種實現方式,代碼實現和配置實現。日常中我們使用更多的是配置實現。
先看看配置是如何實現的幀動畫。
xml配置實現
同樣的,在res/anim下新建xml文件,格式如下:

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:oneshot="false" >
    
    <item android:drawable="@drawable/house1" android:duration="50" />
<item android:drawable="@drawable/house2" android:duration="50" />
<item android:drawable="@drawable/house3" android:duration="50" />
 
</animation-list>

在代碼中引用該配置:

 img.setBackgroundResource(R.anim.list);
	            animationDrawable = (AnimationDrawable) img.getBackground();
	            animationDrawable.start();

簡單實現,主要區別於補間動畫的是,我們這裏使用了一個AnimationDrawable。

代碼實現

animationDrawable = new AnimationDrawable();
			animationDrawable.setOneShot(false);
	        animationDrawable.addFrame(getResources().getDrawable(R.drawable.house1), 100);
	        animationDrawable.addFrame(getResources().getDrawable(R.drawable.house2), 100);
	        animationDrawable.addFrame(getResources().getDrawable(R.drawable.house3), 100);
	        img.setBackground(animationDrawable);
	        animationDrawable.start();

與xml配置相比,這樣顯的麻煩一些,因此一般不這樣使用。

以上就是幀動畫。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章