通過AnimationSet設定動畫

來源:互聯網
上載者:User

標籤:android   style   blog   http   color   io   os   ar   strong   

在代碼中可以通過set來設定多個動畫屬性,這裡分開來設定不同的屬性。

首先先貼上布局檔案,裡面的imageview是用來做動畫的控制項

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="${relativePackage}.${activityClass}" >    <LinearLayout        android:id="@+id/linearLayout1"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_alignParentTop="true"        android:layout_centerHorizontal="true"        android:layout_marginTop="28dp"        android:orientation="vertical" >        <Button            android:id="@+id/alpha_button"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:onClick="buttonListener"            android:text="透明度改變" />        <Button            android:id="@+id/rotate_button"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:onClick="buttonListener"            android:text="旋轉動畫" />        <Button            android:id="@+id/scale_button"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:onClick="buttonListener"            android:text="縮放動畫" />        <Button            android:id="@+id/translate_button"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:onClick="buttonListener"            android:text="移動動畫" />    </LinearLayout>    <ImageView        android:id="@+id/imageView"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_below="@+id/linearLayout1"        android:layout_centerHorizontal="true"        android:layout_marginTop="56dp"        android:src="@drawable/ic_launcher" /></RelativeLayout>

 

在activity中的listener中寫上不同的動畫效果

主要是

 AnimationSet set = new AnimationSet(true);
 set.addAnimation(alpha);
package com.kale.anim;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.animation.AlphaAnimation;import android.view.animation.Animation;import android.view.animation.AnimationSet;import android.view.animation.RotateAnimation;import android.view.animation.ScaleAnimation;import android.view.animation.TranslateAnimation;import android.widget.ImageView;public class MainActivity extends Activity {    ImageView iV;        @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        iV = (ImageView)findViewById(R.id.imageView);    }        public void buttonListener(View v) {        AnimationSet set = new AnimationSet(true);        switch (v.getId()) {        case R.id.alpha_button:            //設定漸層從不透明->透明,1表示不透明,0表示透明            AlphaAnimation alpha = new AlphaAnimation(1f, 0f);            //設定執行的時間            alpha.setDuration(1000);            set.addAnimation(alpha);            break;        case R.id.rotate_button:            //RotateAnimation rotate = new RotateAnimation(0, 180, 0, 0);//從0度旋轉到180度,以左上方(0,0)為圓心            //從相對於自身(圓心在圖片的中心)旋轉360度,            //x軸座標相對於父控制項寬的一半,y軸相對於自身高的一半,於是確定一個圓心            RotateAnimation rotate = new RotateAnimation(0, 360,                    Animation.RELATIVE_TO_PARENT, 0.5f, //0.5 = 1/2的自己父控制項的長度                    Animation.RELATIVE_TO_SELF, 0.5f);//0.5 = 1/2的自己的長度            rotate.setDuration(5000);            set.addAnimation(rotate);            break;        case R.id.scale_button:            //縮放動畫,x座標從1f->2f,y座標從1f->2f。縮放的軸是相對於自己的一半,等於是自己的中心            ScaleAnimation scale = new ScaleAnimation(1f, 2f, 1f, 2f,                    Animation.RELATIVE_TO_SELF, 0.5f,                    Animation.RELATIVE_TO_SELF, 0.5f);            scale.setDuration(1000);            set.addAnimation(scale);            break;        case R.id.translate_button:            //移動動畫.x:從相對於自己x軸為0的位置移動到相對於自己x軸為1的位置。等於自己向右邊移動一個身位            //y:從相對於自己y軸為0的位置移動到相對於自己y軸為1的位置。等於自己向下移動了兩個身位            TranslateAnimation translate = new TranslateAnimation(                    Animation.RELATIVE_TO_SELF, 0f,                     Animation.RELATIVE_TO_SELF, 1f,                     Animation.RELATIVE_TO_SELF, 0f,                    Animation.RELATIVE_TO_SELF, 2f);            translate.setDuration(1000);            set.addAnimation(translate);            break;        default:            break;        }        //設定開始動畫        iV.startAnimation(set);    }}

 

補充:

        set.setStartOffset(1000);//一秒後再執行動畫 = 等待1秒後執行動畫        set.setFillAfter(true);//設定動畫執行後保持最後狀態        set.setFillBefore(false);//設定動畫執行後不回到原來狀態        set.setRepeatCount(3);//設定動畫重複執行的次數

 

通過AnimationSet設定動畫

聯繫我們

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