補間動畫

補間動畫:指定動畫資源(圖片)開始位置中間位置和結束位置,控件仍停留在原來位置(實際位置沒有改變)

(透明度、縮放、位移、旋轉)

/**
	 * 透明度變化的動畫
	 * @param view
	 */
	public void alpha(View view) {
		AlphaAnimation aa = new AlphaAnimation(0.0f, 1.0f);// 完全透明0.0f ---> 完全不透明1.0f
		aa.setDuration(2000);// 動畫播放2秒
		aa.setRepeatCount(2);//重複播放次數
		aa.setRepeatMode(Animation.REVERSE);//重複模式 倒序播
		iv.startAnimation(aa);//iv爲一張圖片資源
	}

/**
	 * 縮放動畫
	 * 
	 * @param view
	 */
	public void scale(View view) {
		//fromX,toX,fromY,toY,pivotXType,pivotXValue,pivotYType,pivotYValue
		//X,Y方向放大2倍,圍繞(0,0)座標縮放  [(0.5f,0.5f)中心點]
		ScaleAnimation sa = new ScaleAnimation(0.0f, 2.0f, 0.0f, 2.0f,
				Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
				0.5f);
		sa.setDuration(2000);
		sa.setRepeatCount(2);
		sa.setRepeatMode(Animation.REVERSE);
		iv.startAnimation(sa);
	}

/**
	 * 位移動畫
	 * 
	 * @param view
	 */
	public void Trans(View view) {
		//pivotXType,pivotXValue,toXType,toXValue,pivotYType,pivotYValue,toYType,toYValue
		//X和Y方向移動位移(相對自身)
		TranslateAnimation ta = new TranslateAnimation(
				Animation.RELATIVE_TO_SELF, -0.5f, Animation.RELATIVE_TO_SELF, 0.5f,
				Animation.RELATIVE_TO_SELF, -0.5f, Animation.RELATIVE_TO_SELF, 0.5f);//還可以相對於父類RELATIVE_TO_PARENT
		ta.setDuration(2000);
		ta.setRepeatCount(2);
		ta.setRepeatMode(Animation.REVERSE);
		iv.startAnimation(ta);
	}
ta.setRepeatCount(Animation.INFINITE);//不停止,一直播放   值爲-1
/**
	 * 旋轉動畫
	 * 
	 * @param view
	 */
	public void rotate(View view) {
		//fromDegrees,toDegrees,pivotXType,pivotXValue,pivotYType,pivotYValue
		//圍繞中心點旋轉360度
		RotateAnimation ra = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF,
				0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
		ra.setDuration(2000);
		ra.setRepeatCount(2);
		ra.setRepeatMode(Animation.REVERSE);
		iv.startAnimation(ra);
	}

//動畫合集
	public void set(View view){
		AnimationSet set = new AnimationSet(false);

		TranslateAnimation ta = new TranslateAnimation(
				Animation.RELATIVE_TO_SELF, -0.5f, Animation.RELATIVE_TO_SELF, 0.5f,
				Animation.RELATIVE_TO_SELF, -0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
		ta.setDuration(2000);
		ta.setRepeatCount(2);
		ta.setRepeatMode(Animation.REVERSE);

		RotateAnimation ra = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF,
				0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
		ra.setDuration(2000);
		ra.setRepeatCount(2);
		ra.setRepeatMode(Animation.REVERSE);

		ScaleAnimation sa = new ScaleAnimation(0.0f, 2.0f, 0.0f, 2.0f,
				Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
				0.5f);
		sa.setDuration(2000);
		sa.setRepeatCount(2);
		sa.setRepeatMode(Animation.REVERSE);

		set.addAnimation(sa);
		set.addAnimation(ta);
		set.addAnimation(ra);
		iv.startAnimation(set);
	}

補間動畫——xml形式

alpha.xml

<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromAlpha="0"
    android:toAlpha="1.0"
    android:duration="2000"
    android:repeatCount="2"
    android:repeatMode="reverse" >
</alpha>


/**
 * 透明度變化的動畫
 * 
 * @param view
 */
public void alpha(View view) {
	Animation aa = AnimationUtils.loadAnimation(this, R.anim.alpha);
	iv.startAnimation(aa);
}

scale.xml

<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="2000" 
    android:fromXScale="0.0" 
    android:fromYScale="0.0" 
    android:pivotX="50%"        50%p//相對於PARENT的50%
    android:pivotY="50%"
    android:repeatCount="2" 
    android:repeatMode="reverse"
    android:toXScale="2.0" 
    android:toYScale="2.0" > 
</scale>

/**
* 縮放動畫
* 
* @param view
*/
public void scale(View view) {
	Animation sa = AnimationUtils.loadAnimation(this, R.anim.scale);
	iv.startAnimation(sa);
}

trans.xml

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="2000"
    android:fromXDelta="-50%"
    android:fromYDelta="-50%"
    android:repeatCount="2"
    android:repeatMode="reverse"
    android:toXDelta="50%"
    android:fillAfter="true"
    android:toYDelta="50%" >
</translate>

android:fillAfter="true" 停留在最後一個動畫效果的位置,不回到原來的位置

/**
* 位移動畫
* 
* @param view
*/
public void Trans(View view) {
	Animation ta = AnimationUtils.loadAnimation(this, R.anim.trans);
	iv.startAnimation(ta);
}

rotate.xml

<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromDegrees="0"
    android:toDegrees="360"
    android:duration="2000"
    android:repeatCount="2"
    android:repeatMode="reverse"
    android:pivotX="50%"
    android:pivotY="50%" >
</rotate>

/**
* 旋轉動畫
* 
* @param view
*/
public void rotate(View view) {
	Animation ra = AnimationUtils.loadAnimation(this, R.anim.rotate);
	iv.startAnimation(ra);
}

set.xml

<?xml version="1.0" encoding="utf-8"?>
<set>

    <rotate
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="2000"
        android:fromDegrees="0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:repeatCount="2"
        android:repeatMode="reverse"
        android:toDegrees="360" >
    </rotate>

    <scale
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="2000"
        android:fromXScale="0.0"
        android:fromYScale="0.0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:repeatCount="2"
        android:repeatMode="reverse"
        android:toXScale="2.0"
        android:toYScale="2.0" >
    </scale>

    <translate
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="2000"
        android:fromXDelta="-50%"
        android:fromYDelta="-50%"
        android:repeatCount="2"
        android:repeatMode="reverse"
        android:toXDelta="50%"
        android:toYDelta="50%" >
    </translate>

</set>


	

public void set(View view){
	Animation set = AnimationUtils.loadAnimation(this, R.anim.set);
	iv.startAnimation(set);
}

例如:勻速旋轉動畫

/** 均勻旋轉動畫 */  
private RotateAnimation refreshingAnimation;  
  
refreshingAnimation = (RotateAnimation) AnimationUtils.loadAnimation(mContext, R.anim.rotating);  
refreshingAnimation.setDuration(5000);  
// 添加勻速轉動動畫  
LinearInterpolator lir = new LinearInterpolator();  
refreshingAnimation.setInterpolator(lir);  
//設置動畫(勻速旋轉動畫)  
atdp_loading_iv.startAnimation(refreshingAnimation);  


atdp_loading_iv.clearAnimation();  



<?xml version="1.0" encoding="utf-8"?>  
<rotate xmlns:android="http://schemas.android.com/apk/res/android"  
    android:duration="1000"  
    android:fillAfter="true"  
    android:fromDegrees="0"  
    android:pivotX="50%"  
    android:pivotY="50%"  
    android:repeatCount="-1"  
    android:toDegrees="359" >  
  
</rotate>


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