Tween補間動畫

主AC包含各類API: java


package com.example.tween;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
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.ImageView;

public class MainActivity extends Activity {

    private ImageView iv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_main);
	iv=(ImageView) findViewById(R.id.iv);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
	// Inflate the menu; this adds items to the action bar if it is present.
	getMenuInflater().inflate(R.menu.main, menu);
	return true;
    }

    /**
     * 透明度
     * @param v
     */
    public void click1(View v)
    {
	AlphaAnimation animation=new AlphaAnimation(0.0f,1.0f);
	//動畫播放時間
	animation.setDuration(2000);
	//重複幾回 一個3次
	animation.setRepeatCount(2);
	animation.setRepeatMode(AlphaAnimation.REVERSE);
	iv.startAnimation(animation);
    }
    
    /**
     * 縮放
     * @param v
     */
    public void click2(View v)
    {
        //參數含義很簡單 開始x比例 最終x比例 開始Y比例……縮放中心的類型,縮放中心位置(比例)
	ScaleAnimation animation=new ScaleAnimation(0.2f, 2.0f, 0.2f, 2.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
	
	//動畫播放時間
	animation.setDuration(2000);
	//重複幾回 一個3次
	animation.setRepeatCount(2);
	animation.setRepeatMode(Animation.REVERSE);
	iv.startAnimation(animation);
    }
    
    /**
     * 旋轉
     * @param v
     */
    public void click3(View v)
    {
	RotateAnimation animation=new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
	//動畫播放時間
	animation.setDuration(2000);
	//重複幾回 一個3次
	animation.setRepeatCount(2);
	animation.setRepeatMode(Animation.REVERSE);
	iv.startAnimation(animation);
    }
    
    /**
     * 平移
     * @param v
     */
    public void click4(View v)
    {
	TranslateAnimation animation=
		new TranslateAnimation
		(Animation.RELATIVE_TO_PARENT, 0.2f, Animation.RELATIVE_TO_PARENT, 1.0f, Animation.RELATIVE_TO_PARENT, 0.2f, Animation.RELATIVE_TO_PARENT, 1.0f);
	//動畫播放時間
	animation.setDuration(2000);
	//重複幾回 一個3次
	animation.setRepeatCount(2);
	animation.setRepeatMode(Animation.REVERSE);
	iv.startAnimation(animation);
    }
    
    /**
     * 組合
     * @param v
     */
    public void click5(View v)
    {
	AnimationSet animationSet=new AnimationSet(false);
	
	RotateAnimation ra=new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
	//動畫播放時間
		ra.setDuration(2000);
		//重複幾回 一個3次
		ra.setRepeatCount(2);
		ra.setRepeatMode(Animation.REVERSE);
	TranslateAnimation ta=
		new TranslateAnimation
		(Animation.RELATIVE_TO_PARENT, 0.2f, Animation.RELATIVE_TO_PARENT, 0.5f, Animation.RELATIVE_TO_PARENT, 0.2f, Animation.RELATIVE_TO_PARENT, 0.5f);
	//動畫播放時間
		ta.setDuration(2000);
		//重複幾回 一個3次
		ta.setRepeatCount(2);
		ta.setRepeatMode(Animation.REVERSE);
	ScaleAnimation sa=new ScaleAnimation(0.2f, 2.0f, 0.2f, 2.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
	//動畫播放時間
		sa.setDuration(2000);
		//重複幾回 一個3次
		sa.setRepeatCount(2);
		sa.setRepeatMode(Animation.REVERSE);
		animationSet.addAnimation(ra);
		animationSet.addAnimation(ta);
		animationSet.addAnimation(sa);
	iv.startAnimation(animationSet);
    }
    
    /**
     * XMl文件指定動畫
     * @param v
     */
    public void click6(View v)
    {
	iv.startAnimation(AnimationUtils.loadAnimation(this, R.anim.alpha_anim));
	
    }
}

也可以使用XMl文件定義:其中有p的表示parent android




很簡單  app

相關文章
相關標籤/搜索