android Animations 動畫效果(三)

1.AnimationSet的使用方法

什麼是AnimationSet
1.AnimationSet是Animation的子類
2.一個AnimationSet包含瞭一系列的Animation
3.針對AnimationSet設置一些Animation的常見屬性(startOffset,duration等等),可以被包含在AnimationSet中的Animation集成

2.Interpolator的使用方法
什麼是Interpolator
Interpolator定義瞭動畫變化的速率,在Animations框架當中定義瞭一下幾種Interpolator
AccelerateDecelerateInterpolator:在動畫開始與結束的地方速率改變比較慢,在中間的時候加速
AccelerateInterpolator:在動畫開始的地方速率改變比較慢,然後開始加速
CycleInterpolator:動畫循環播放特定的次數,速率改變沿著正弦曲線
DecelerateInterpolator:在動畫開始的地方速率改變比較慢,然後開始減速
LinearInterpolator:在動畫的以均勻的速度改變

這是同時實現兩個動畫的效果:

MainActivity.java

package com.yx.animations03;

import com.yx.animations03.R;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AccelerateDecelerateInterpolator;
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.Button;
import android.widget.ImageView;

public class MainActivity extends Activity {

private Button button=null;
private ImageView imageView = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

button = (Button) findViewById(R.id.buttonId);
button.setOnClickListener(new buttonListener());

imageView = (ImageView) findViewById(R.id.imageViewId);
}

class buttonListener implements OnClickListener{
@Override
public void onClick(View v) {
/*//多個動畫效果,方法一
Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.flishes);
imageView.startAnimation(animation);
*/

//方法二
AnimationSet animationSet = new AnimationSet(true);//這裡的true表示共享一個Interpolator
animationSet.setInterpolator(new AccelerateDecelerateInterpolator());
ScaleAnimation scaleAnimation = new ScaleAnimation(1,0.1f,1,0.1f,
Animation.RELATIVE_TO_SELF,0.5f,
Animation.RELATIVE_TO_SELF,0.5f
);
RotateAnimation rotateAnimation = new RotateAnimation(0,360,
Animation.RELATIVE_TO_PARENT,1f,
Animation.RELATIVE_TO_PARENT,0f
);
animationSet.addAnimation(rotateAnimation);
animationSet.addAnimation(scaleAnimation);
animationSet.setDuration(2000);
animationSet.setStartOffset(500);
imageView.startAnimation(animationSet);
}
}
}
flishes.xml 本文件在res下新建的anim中

<set xmlns:android="https://schemas.android.com/apk/res/android"
android:interpolator=”@android:anim/accelerate_interpolator”
android:shareInterpolator=”true”
>

<!– android:interpolator="@android:anim/accelerate_interpolator":設置動畫效果
AccelerateDecelerateInterpolator:在動畫開始與結束的地方速率改變比較慢,在中間的時候加速
AccelerateInterpolator:在動畫開始的地方速率改變比較慢,然後開始加速
CycleInterpolator:動畫循環播放特定的次數,速率改變沿著正弦曲線
DecelerateInterpolator:在動畫開始的地方速率改變比較慢,然後開始減速
LinearInterpolator:在動畫的以均勻的速度改變
–>
<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=”2000″
>

android:toAlpha=”0.0″
android:startOffset=”500″
android:duration=”500″>

activity_main.xml
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools=”https://schemas.android.com/tools”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:paddingBottom=”@dimen/activity_vertical_margin”
android:paddingLeft=”@dimen/activity_horizontal_margin”
android:paddingRight=”@dimen/activity_horizontal_margin”
android:paddingTop=”@dimen/activity_vertical_margin”
tools:context=”.MainActivity” >

<Button
android:id=”@+id/buttonId”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:layout_alignParentBottom=”true”
android:text=”測試動畫效果”
/>

<LinearLayout
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
android:layout_alignLeft=”@+id/scaleButton”
android:layout_alignParentTop=”true”
android:orientation=”vertical” >

<ImageView
android:id=”@+id/imageViewId”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_margin=”50px”
android:src=”@drawable/ic_launcher” />

發佈留言