android 漸變動畫

      此demo實現了多種 android 漸變動畫,主要包括:漸隱,旋轉,縮放,移動和這些動畫的綜合效果。


demo:下載地址


源碼:

package com.bobo.study.study_2_4;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.CycleInterpolator;
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 MainActivity extends Activity implements View.OnClickListener {
    Button scaleBut,alphaBut,translateBut,rotateBut,animsBut;
    ImageView imageView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        imageView=(ImageView)findViewById(R.id.imageView);
        scaleBut=(Button)findViewById(R.id.scaleBut);
        scaleBut.setOnClickListener(this);
        rotateBut=(Button)findViewById(R.id.rotateBut);
        rotateBut.setOnClickListener(this);
        translateBut=(Button)findViewById(R.id.translateBut);
        translateBut.setOnClickListener(this);
        alphaBut=(Button)findViewById(R.id.alphaBut);
        alphaBut.setOnClickListener(this);
        animsBut=(Button)findViewById(R.id.animsBut);
        animsBut.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        //1,創建AnimationSet對象(可以包含多個Animation對象,是Animation的子類)
        //可以當作animation的集合,能用來設置animation們的一些公共屬性
        //參數true:可以爲animationSet設置Interpolator,否則只能分別分別爲animation設置Interpolator
		AnimationSet animationSet=new AnimationSet(true);
        //設置動畫簡便的速率變化(AccelerateInterpolator逐漸加速)
        //DecelerateInterpolator:逐漸減速
        //AccelerateDecelerateInterpolator:中間快,兩頭慢
        //LinearInterpolator:勻速
        //CycleInterpolator(n):動畫連續播放n次,速率沿着正弦曲線變化
        animationSet.setInterpolator(new CycleInterpolator(3));
        //2,創建Animation對象
		Animation animation=null;
		if (v == alphaBut) {
			System.out.println("alpha淡入淡出");
            //3,爲Animation對象設置屬性
			animation=new AlphaAnimation(0,1);//(從完全透明0到不透明1)
		} else if (v == rotateBut) {
			System.out.println("rotate旋轉");
			animation=new RotateAnimation(0,360,//從0度到360度
                    //下面幾個參數是用來設置旋轉中心點的座標
                    Animation.RELATIVE_TO_PARENT, 0.5f,//橫座標類型(相對父控件的),0.5個父控件X軸長
                    Animation.RELATIVE_TO_PARENT,0.5f);//縱座標類型(相對父控件的),0.5個父控件Y軸長
		} else if (v == scaleBut) {
			System.out.println("scale縮放");
			animation=new ScaleAnimation(1,0.2f,1,0.2f,//X軸從1縮放到0.2,Y軸從1縮放到0.2(相對自身)
                    //下面是縮放中心點的座標
                    Animation.RELATIVE_TO_SELF, 1.0f,
                    Animation.RELATIVE_TO_SELF,1.0f);
		} else if (v == translateBut) {
			System.out.println("translate移動");
			animation=new TranslateAnimation(
					Animation.RELATIVE_TO_SELF,0f,//X軸開始位置
					Animation.RELATIVE_TO_SELF,1f,//X軸結束位置
					Animation.RELATIVE_TO_SELF,0f,//Y軸開始位置
					Animation.RELATIVE_TO_SELF,1f//Y軸結束位置
					);
		}else if(v==animsBut){
            System.out.println("多animation綜合效果");
            animation=new AlphaAnimation(0,1);
            animation.setDuration(2000);
            animation.setRepeatCount(1);
            animationSet.addAnimation(animation);
            //animationSet可以添加多個animation來實現綜合效果
            animation=new RotateAnimation(0,360,
                    Animation.RELATIVE_TO_PARENT, 0.5f,
                    Animation.RELATIVE_TO_PARENT,0.5f);
        }
		animation.setDuration(2000);//動畫執行時間
		animation.setRepeatCount(1);//動畫重複執行的次數(重複一次,共兩次)
        //注意animationSet設置的屬性會應用到其內所有animation上
		animationSet.setFillAfter(true);//爲true,動畫結束後停留在結束的狀態
		animationSet.setFillBefore(false);//爲true,動畫結束後回到最初的狀態
		animationSet.setStartOffset(1000);//動畫開始前的等待時間
        //4,爲animationSet添加animation
		animationSet.addAnimation(animation);
        //5,通過控件執行animationSet
		imageView.startAnimation(animationSet);

        //動畫效果也可以提前寫好:1,在res創建anim文件夾,裏面創建各種anim效果xml文件
        //2,動畫效果的xml文件格式參考例子中的
        //3,用AnimationUtils.loadAnimation()方法調用這些xml文件
/*        Animation animation=null;
        if (v == alphaBut) {
            System.out.println("alpha");
            animation= AnimationUtils.loadAnimation(this, R.anim.alpha);
        } else if (v == rotateBut) {
            System.out.println("translate");
            animation=AnimationUtils.loadAnimation(this,R.anim.rotate);
        } else if (v == scaleBut) {
            System.out.println("scale");
            animation=AnimationUtils.loadAnimation(this,R.anim.scale);
        } else if (v == translateBut) {
            animation = AnimationUtils.loadAnimation(this, R.anim.translate);
            System.out.println("rotate");
        } else if (v == animsBut) {
            animation = AnimationUtils.loadAnimation(this, R.anim.anims);
            System.out.println("anims");
        }
        imageView.startAnimation(animation);*/

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}


anim效果xml文件(所有):

<pre name="code" class="html"><?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator">
    
    <translate
        android:fromXDelta="50%"
        android:fromYDelta="50%"
        android:toXDelta="100%"
        android:toYDelta="100%"
        android:duration="2000"/>

</set>

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator">
    <!--pivotX="180"是絕對定位,"50%"是相對自身定位,"50%p"是相對父控件定位-->
    <scale
        android:fromXScale="1.0"
        android:fromYScale="1.0"
        android:toXScale="0.0"
        android:toYScale="0.0"
        android:pivotX="180"
        android:pivotY="180"
        android:duration="2000"/>

</set>

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator">
    <!--pivotX="180"是絕對定位,"50%"是相對自身定位,"50%p"是相對父控件定位-->
    <rotate
        android:fromDegrees="0"
        android:toDegrees="-360"
        android:pivotX="180"
        android:pivotY="180"
        android:duration="2000"/>

</set>

<?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:duration="2000"/>

</set>

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator"
    android:shareInterpolator="false">
    <!--如果上面的shareInterpolator爲true,就不用在下面分別爲每個動畫效果添加interpolator-->
    <alpha
        android:interpolator="@android:anim/decelerate_interpolator"
        android:fromAlpha="1.0"
        android:toAlpha="0.0"
        android:duration="2000"/>
    <!--pivotX="180"是絕對定位,"50%"是相對自身定位,"50%p"是相對父控件定位-->
    <rotate
        android:interpolator="@android:anim/accelerate_interpolator"
        android:fromDegrees="0"
        android:toDegrees="-360"
        android:pivotX="50%"
        android:pivotY="50%"
        android:duration="2000"/>

</set>



運行截圖:


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