公告

轉載請附原始文章連結

2013年12月7日 星期六

Android AnimationSet 組合特效 動態特效

Keyword search:動畫、動態、特效、效果、旋轉、淡進、淡出、移動、放大、縮小、元件、按鈕、圖片



先在在layout放置兩個元件,我這邊的範例是使用ImageView 、Button兩個元件介紹
layout:
   <Button
      android:id="@+id/Button_start"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentLeft="true"
      android:layout_alignParentTop="true"
      android:text="Button" />

   <ImageView
      android:id="@+id/ImageView_Sample"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_centerHorizontal="true"
      android:layout_centerVertical="true"
      android:src="@drawable/ic_launcher" />


移動特效
Sample:
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.TranslateAnimation;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.AlphaAnimation;

ImageView mImage_sample;
Button mButton_start;

init()
{
  mImage_sample = (ImageView) findViewById(R.id.ImageView_Sample);
  mButton_start = (Button) findViewById(R.id.Button_start);
  mButton_start.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {

      //移動效果
      Animation amTranslate = new TranslateAnimation(0.0f, 100.0f, 0.0f, 100.0f);
      //setDuration (long durationMillis) 設定動畫開始到結束的執行時間
      amTranslate.setDuration(2000);
      //setRepeatCount (int repeatCount) 設定重複次數 -1為無限次數 0
      amTranslate.setRepeatCount(-1);

      //旋轉效果
      Animation amRotate = new RotateAnimation(0.0f, 360.0f, 0.0f, 100.0f);
      //setDuration (long durationMillis) 設定動畫開始到結束的執行時間
      amRotate.setDuration(2000);
      //setRepeatCount (int repeatCount) 設定重複次數 -1為無限次數 0
      amRotate.setRepeatCount(-1);

      //放大縮小效果
      Animation amScale = new ScaleAnimation(0.0f, 1.0f, 0.0f, 1.0f);
      //setDuration (long durationMillis) 設定動畫開始到結束的執行時間
      amRotate.setDuration(2000);
      //setRepeatCount (int repeatCount) 設定重複次數 -1為無限次數 0
      amRotate.setRepeatCount(-1);
    
      //淡進淡出效果
      Animation amAlpha = new AlphaAnimation(1.0f, 0.0f);
      //setDuration (long durationMillis) 設定動畫開始到結束的執行時間
      amAlpha.setDuration(2000);
      //setRepeatCount (int repeatCount) 設定重複次數 -1為無限次數 0
      amAlpha.setRepeatCount(-1);
    
      //特效組合
      AnimationSet amSet = new AnimationSet(false);
      amSet.addAnimation(amTranslate);
      amSet.addAnimation(amRotate);
      amSet.addAnimation(amScale);
      amSet.addAnimation(amAlpha);

      //將動畫參數設定到圖片並開始執行動畫
      mImage_sample.startAnimation(amSet);
    }
  });
}


若要使用XML的方式為在res下建立一個anim的資料夾    res/anim
Sample:animationset.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <translate
        android:duration="2000"
        android:fromXDelta="0"
        android:fromYDelta="0"
        android:repeatCount="-1"
        android:toXDelta="100"
        android:toYDelta="100" >
    </translate>

    <scale
        android:duration="2000"
        android:fromXScale="0"
        android:fromYScale="0"
        android:repeatCount="-1"
        android:toXScale="1"
        android:toYScale="1" >
    </scale>


    <rotate
        android:duration="2000"
        android:fromDegrees="0"
        android:pivotX="0"
        android:pivotY="100"
        android:repeatCount="-1"
        android:toDegrees="360" >
    </rotate>

    <alpha
        android:duration="2000"
        android:fromAlpha="1"
        android:repeatCount="-1"
        android:toAlpha="0" >
    </alpha>

</set>
Code:
import android.view.animation.AnimationUtils;

Animation am = AnimationUtils.loadAnimation(mContext, R.anim.animationset);
mImage_sample.startAnimation(am);

其他文章:
Android TranslateAnimation 移動效果 動態特效
Android ScaleAnimation 放大縮小效果 動態特效
Android RotateAnimation 旋轉效果 動態特效
Android AlphaAnimation 淡進淡出效果 動態特效
Android AnimationSet 組合特效 動態特效

沒有留言:

張貼留言