Android自訂Animation

來源:互聯網
上載者:User

先呈上我測試動畫的介面:

布局檔案main.xml為:

<?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" >    <ImageView        android:id="@+id/image"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center"        android:layout_marginTop="30dp"        android:src="@drawable/naruto" />     <Button        android:id="@+id/button"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center"        android:layout_marginTop="30dp"        android:text="click me to show" /></LinearLayout>

接下來自訂動畫,建立一個類CustomAnimation繼承自Animation,需要重寫applyTransformation和initialize方法,具體解釋就在代碼中給出了,所以呢,在這裡就不重複說了吧O(∩_∩)O。自訂類CustomAnimation代碼如下:

package com.android.animation;import android.graphics.Matrix;import android.view.animation.Animation;import android.view.animation.AnticipateOvershootInterpolator;import android.view.animation.Transformation;public class CustomAnimation extends Animation {    int centerX, centerY;    public CustomAnimation() {            }    @Override    protected void applyTransformation(float interpolatedTime, Transformation t) {        // TODO Auto-generated method stub        super.applyTransformation(interpolatedTime, t);        Matrix matrix = t.getMatrix();        //通過interpolatedTime設定矩陣縮放        matrix.setScale(interpolatedTime, interpolatedTime);        matrix.preTranslate(-centerX, -centerY);        matrix.postTranslate(centerX, centerY);    }    //初始化動畫    @Override    public void initialize(int width, int height, int parentWidth,            int parentHeight) {        // TODO Auto-generated method stub        super.initialize(width, height, parentWidth, parentHeight);        // 初始化中點座標        centerX = width / 2;        centerY = height / 2;        //動畫持續的時間        setDuration(2000);        //設定動畫結束後是否保留在終止的位置        setFillAfter(true);        /**         * 設定動畫的加速度曲線,傳入參數Interpolator的簡介子類有:         * AnticipateOvershootInterpolator 改變開始向後然後向前射出超出螢幕         * AccelerateDecelerateInterpolator 變化的速度開始和結束緩慢但中間加速穿過         * AccelerateInterpolator 變化的速度開始慢慢,然後加速         * AnticipateInterpolator 改變開始向後然後向前射出         * BounceInterpolator 變化到最後時反彈         * CycleInterpolator 重複動畫指定數量的周期CycleInterpolator(float cycles)需傳入周期數         * DecelerateInterpolator 開始變化的速度很快,然後開始減速         * LinearInterpolator 線性變化         * OvershootInterpolator 射出向前,超過螢幕然後回來         *          * pS->文字功底欠佳,描述的不是很準確哈,見諒,見諒o(╯□╰)o具體效果還是要自己動手試試         */        setInterpolator(new AnticipateOvershootInterpolator());    }}

最後在主Activity中使用自訂的Animation:

package com.android.animation;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.ImageView;public class CustomAnimationActivity extends Activity {    /** Called when the activity is first created. */    private CustomAnimation animation;    private ImageView image;    private Button button;        @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        //執行個體化        animation = new CustomAnimation();                image = (ImageView)findViewById(R.id.image);        button = (Button)findViewById(R.id.button);        button.setOnClickListener(new OnClickListener(){            @Override            public void onClick(View arg0) {                // TODO Auto-generated method stub                //為圖片設定動畫效果                image.setAnimation(animation);            }        });    }}

聯繫我們

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