Android中TweenAnimation四種動畫轉場效果

來源:互聯網
上載者:User

點擊每個按鈕都會有對應的動畫顯示

 


activity代碼:

package com.tmacsky;import android.app.Activity;import android.os.Bundle;import android.util.Log;import android.view.View;import android.view.View.OnClickListener;import android.view.animation.AlphaAnimation;import android.view.animation.Animation;import android.view.animation.AnimationSet;import android.view.animation.AnimationUtils;import android.view.animation.ScaleAnimation;import android.view.animation.TranslateAnimation;import android.view.animation.Animation.AnimationListener;import android.view.animation.RotateAnimation;import android.widget.Button;import android.widget.ImageView;public class AnimationDemoActivity extends Activity {private ImageView imageView;/** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        //定義四個動畫的button屬性        imageView = (ImageView)findViewById(R.id.imageView);        Button alpha = (Button)findViewById(R.id.Alpha);        alpha.setOnClickListener(new AnimationClickListener(AnimationType.Alpha));        Button rotate = (Button)findViewById(R.id.Rotate);        rotate.setOnClickListener(new AnimationClickListener(AnimationType.Rotate));        Button scale = (Button)findViewById(R.id.Scale);        scale.setOnClickListener(new AnimationClickListener(AnimationType.Scale));        Button translate = (Button)findViewById(R.id.Translate);        translate.setOnClickListener(new AnimationClickListener(AnimationType.Translate));        Button complex = (Button)findViewById(R.id.Complex);        complex.setOnClickListener(new AnimationClickListener(AnimationType.Complex));    }    //定義animationType屬性    enum AnimationType{    Alpha,    Rotate,    Scale,    Translate,    Complex    }    //定義一個函數    class AnimationClickListener implements OnClickListener{    private AnimationType animationType;    public AnimationClickListener(AnimationType animType){    animationType = animType;    }public void onClick(View v) {// TODO Auto-generated method stubswitch (animationType) {case Alpha://定義漸層動畫,重複5次,持續1分鐘/*AlphaAnimation _animation = new AlphaAnimation(1f, 0.1f);_animation.setDuration(3000);_animation.setRepeatCount(5);//設定迴圈_animation.setRepeatMode(Animation.REVERSE);_animation.setAnimationListener(new AnimationListener() {//設定animation監聽,依次是啟動,重複,然後結束public void onAnimationStart(Animation animation) {// TODO Auto-generated method stubLog.i("log", "animation Start");}public void onAnimationRepeat(Animation animation) {// TODO Auto-generated method stubLog.i("log", "animation Repeat");}public void onAnimationEnd(Animation animation) {// TODO Auto-generated method stubLog.i("log", "animation End");}});//啟動動畫imageView.startAnimation(_animation);*/AlphaAnimation alphaAnimation = (AlphaAnimation)AnimationUtils.loadAnimation(AnimationDemoActivity.this, R.anim.alpha);imageView.startAnimation(alphaAnimation);break;case Rotate://定義旋轉動畫,旋轉一周持續1分鐘,重複三次,在物體的中心位置RotateAnimation rotateAnimation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);rotateAnimation.setDuration(3000);rotateAnimation.setRepeatCount(3);//啟動動畫imageView.startAnimation(rotateAnimation);break;case Scale://定義縮放動畫,從中心座標開始,縮放1.5倍大小,持續1分鐘,重複三次ScaleAnimation scaleAnimation = new ScaleAnimation(1, 1.5f, 1, 1.5f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);scaleAnimation.setDuration(3000);scaleAnimation.setRepeatCount(3);//啟動動畫imageView.startAnimation(scaleAnimation);break;case Translate://定義移動動畫,都從自身座標開始,移動2個位置,持續1分鐘,重複三次TranslateAnimation translateAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 2, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 2);translateAnimation.setDuration(3000);translateAnimation.setRepeatCount(3);//啟動動畫imageView.startAnimation(translateAnimation);break;case Complex://設定複雜的操作步驟,點擊按鈕complex後,會運行四種動畫效果疊加AnimationSet sets = new AnimationSet(false);//定義漸層動畫AlphaAnimation _animation1 = new AlphaAnimation(1f, 0.1f);_animation1.setDuration(3000);//定義旋轉動畫,在物體的中心位置RotateAnimation rotateAnimation1 = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);rotateAnimation1.setDuration(3000);//定義縮放動畫,從中心座標開始,縮放1.5倍大小ScaleAnimation scaleAnimation1 = new ScaleAnimation(1, 1.5f, 1, 1.5f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);scaleAnimation1.setDuration(3000);//定義移動動畫,都從自身座標開始,移動2個位置TranslateAnimation translateAnimation1 = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 2, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 2);translateAnimation1.setDuration(3000);//啟動動畫                                sets.addAnimation(_animation1);sets.addAnimation(rotateAnimation1);sets.addAnimation(scaleAnimation1);sets.addAnimation(translateAnimation1);imageView.startAnimation(sets);break;default:break;}}    }}
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <TextView        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="@string/hello" />    <ImageView        android:id="@+id/imageView"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:src="@drawable/qa" />    <Button        android:id="@+id/Alpha"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Alpha" />    <Button        android:id="@+id/Rotate"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Rotate" />    <Button        android:id="@+id/Scale"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Scale" />    <Button        android:id="@+id/Translate"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Translate" />    <Button        android:id="@+id/Complex"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Complex" /></LinearLayout>
<?xml version="1.0" encoding="utf-8"?><alpha     xmlns:android="http://schemas.android.com/apk/res/android"    android:fromAlpha="1" android:toAlpha="0.3" android:duration="2000" android:repeatCount="3"></alpha>


 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.