android Animations 動畫效果(三)

來源:互聯網
上載者:User

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中

android:interpolator="@android:anim/accelerate_interpolator"
android:shareInterpolator="true"
>


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
xmlns:tools="http://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" >

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


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

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





聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.