android代碼實現ScaleAnimation

來源:互聯網
上載者:User
package com.yangguangfu.cn;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.ScaleAnimation;
import android.widget.ImageView;

public class ScaleAnimationDemoActivity extends Activity implements
        OnClickListener {

    private ImageView top_left;
    private ImageView top_right;
    private ImageView bottom_left;
    private ImageView bottom_right;
    private ImageView center;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) { // 要使用findViewById,
        super.onCreate(savedInstanceState); // 做為使用者介面
        setContentView(R.layout.second);

        // 取得UI 介面中的View 物件
        // 取得View 物件後,再透過轉換成實際的物件
        top_left = (ImageView) findViewById(R.id.top_left);
        top_right = (ImageView) findViewById(R.id.top_right);
        bottom_left = (ImageView) findViewById(R.id.bottom_left);
        bottom_right = (ImageView) findViewById(R.id.bottom_right);
        center = (ImageView) findViewById(R.id.center);
        top_left.setOnClickListener(this);
        top_right.setOnClickListener(this);
        bottom_left.setOnClickListener(this);
        bottom_right.setOnClickListener(this);
        center.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.top_left:
            topLeftScaleAnimation(v);
            break;
        case R.id.top_right:
            topRightScaleAnimation(v);
            break;
        case R.id.bottom_left:
            bottomLiftScaleAnimation(v);
            break;
        case R.id.bottom_right:
            bottomRightScaleAnimation(v);
            break;
        case R.id.center:
            centerScaleAnimation(v);

            break;
        }
    }

    private Animation topLeftanimation;
    private boolean istopLeft = false;

    private void topLeftScaleAnimation(final View v) {

        if (!istopLeft) {

            topLeftanimation = new ScaleAnimation(1.0f, 4.0f, 1.0f, 4.0f,
                    Animation.RELATIVE_TO_SELF, 0.0f,
                    Animation.RELATIVE_TO_SELF, 0.0f);

            istopLeft = true;

        } else {
            topLeftanimation = new ScaleAnimation(4.0f, 1.0f, 4.0f, 1.0f,
                    Animation.RELATIVE_TO_SELF, 0.0f,
                    Animation.RELATIVE_TO_SELF, 0.0f);
            istopLeft = false;

        }

        // 動畫開始到結束的執行時間(1000 = 1 秒)
        topLeftanimation.setDuration(2000);

        // 動畫重複次數(-1 表示一直重複)
        // am.setRepeatCount(1);
        topLeftanimation.setAnimationListener(new AnimationListener() {

            @Override
            public void onAnimationStart(Animation animation) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onAnimationRepeat(Animation animation) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                top_left.setEnabled(true);

            }
        });
        topLeftanimation.setFillAfter(true);
        // 圖片配置動畫
        top_left.setAnimation(topLeftanimation);

        // Help();
        // handler.sendEmptyMessageDelayed(topHand, 2000);
        // top_left.setLayoutParams(lp);
        // 動畫開始
        topLeftanimation.startNow();

        top_left.setEnabled(false);
    }

    private static final int topHand = 6;

    private boolean isBottomLift = false;
    Animation bottomLiftScaleAnimation = null;

    private void bottomLiftScaleAnimation(View v) {

        if (!isBottomLift) {
            bottomLiftScaleAnimation = new ScaleAnimation(1.0f, 4.0f, 1.0f,
                    4.0f, Animation.RELATIVE_TO_SELF, 0f,
                    Animation.RELATIVE_TO_SELF, 1.0f);
            isBottomLift = true;
        } else {
            bottomLiftScaleAnimation = new ScaleAnimation(4.0f, 1.0f, 4.0f,
                    1.0f, Animation.RELATIVE_TO_SELF, 0f,
                    Animation.RELATIVE_TO_SELF, 1.0f);
            isBottomLift = false;
        }

        // 動畫開始到結束的執行時間(1000 = 1 秒)
        bottomLiftScaleAnimation.setDuration(2000);

        // 動畫重複次數(-1 表示一直重複)
        // bottomLiftScaleAnimation.setRepeatCount(-1);
        bottomLiftScaleAnimation.setFillAfter(true);
        // 圖片配置動畫
        bottom_left.setAnimation(bottomLiftScaleAnimation);
        bottomLiftScaleAnimation.setAnimationListener(new AnimationListener() {

            @Override
            public void onAnimationStart(Animation animation) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onAnimationRepeat(Animation animation) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                bottom_left.setEnabled(true);
            }
        });
        // 動畫開始
        bottomLiftScaleAnimation.startNow();
        bottom_left.setEnabled(false);
    }

    private boolean isBottomRigth = false;
    Animation bottomRightScaleAnimation;

    private void bottomRightScaleAnimation(View v) {
        if (!isBottomRigth) {
            bottomRightScaleAnimation = new ScaleAnimation(1.0f, 4.0f, 1.0f,
                    4.0f, Animation.RELATIVE_TO_SELF, 1.0f,
                    Animation.RELATIVE_TO_SELF, 1.0f);
            isBottomRigth = true;
        } else {
            bottomRightScaleAnimation = new ScaleAnimation(4.0f, 1.0f, 4.0f,
                    1.0f, Animation.RELATIVE_TO_SELF, 1.0f,
                    Animation.RELATIVE_TO_SELF, 1.0f);
            isBottomRigth = false;
        }
        // 動畫開始到結束的執行時間(1000 = 1 秒)
        bottomRightScaleAnimation.setDuration(2000);
        bottomRightScaleAnimation.setAnimationListener(new AnimationListener() {

            @Override
            public void onAnimationStart(Animation animation) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onAnimationRepeat(Animation animation) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                // TODO Auto-generated method stub
                bottom_right.setEnabled(true);

            }
        });

        // 動畫重複次數(-1 表示一直重複)
        // bottomRightScaleAnimation.setRepeatCount(-1);
        bottomRightScaleAnimation.setFillAfter(true);
        // 圖片配置動畫
        bottom_right.setAnimation(bottomRightScaleAnimation);

        // 動畫開始
        bottomRightScaleAnimation.startNow();

        bottom_right.setEnabled(false);
    }

    private Animation topRightScaleAnimation;
    private boolean isTopRight = false;

    private void topRightScaleAnimation(View v) {
        if (!isTopRight) {
            topRightScaleAnimation = new ScaleAnimation(1.0f, 4.0f, 1.0f, 4.0f,
                    Animation.RELATIVE_TO_SELF, 1.0f,
                    Animation.RELATIVE_TO_SELF, 0.0f);
            isTopRight = true;

        } else {
            topRightScaleAnimation = new ScaleAnimation(4.0f, 1.0f, 4.0f, 1.0f,
                    Animation.RELATIVE_TO_SELF, 1.0f,
                    Animation.RELATIVE_TO_SELF, 0.0f);
            isTopRight = false;
        }
        topRightScaleAnimation.setDuration(2000);集裝箱運費

        // 動畫重複次數(-1 表示一直重複)
        // topRightScaleAnimation.setRepeatCount(-1);

        // 圖片配置動畫
        top_right.setAnimation(topRightScaleAnimation);
        topRightScaleAnimation.setAnimationListener(new AnimationListener() {

            @Override
            public void onAnimationStart(Animation animation) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onAnimationRepeat(Animation animation) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                top_right.setEnabled(true);

            }
        });

        // 動畫開始
        topRightScaleAnimation.startNow();
        topRightScaleAnimation.setFillAfter(true);

        top_right.setEnabled(false);
    }

    private boolean isCenter = false;
    Animation centerScaleAnimation;

    private void centerScaleAnimation(View v) {
        if (!isCenter) {
            centerScaleAnimation = new ScaleAnimation(1.0f, 4.0f, 1.0f, 4.0f,
                    Animation.RELATIVE_TO_SELF, 0.5f,
                    Animation.RELATIVE_TO_SELF, 0.5f);
            isCenter = true;
        } else {
            centerScaleAnimation = new ScaleAnimation(4.0f, 1.0f, 4.0f, 1.0f,
                    Animation.RELATIVE_TO_SELF, 0.5f,
                    Animation.RELATIVE_TO_SELF, 0.5f);
            isCenter = false;
        }
        centerScaleAnimation.setDuration(2000);
        centerScaleAnimation.setFillAfter(true);
        // 動畫重複次數(-1 表示一直重複)
        // centerScaleAnimation.setRepeatCount(-1);
        centerScaleAnimation.setAnimationListener(new AnimationListener() {

            @Override
            public void onAnimationStart(Animation animation) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onAnimationRepeat(Animation animation) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                center.setEnabled(true);
                // TODO Auto-generated method stub

            }
        });
        // 圖片配置動畫喝咖啡的英文翻譯,喝咖啡的造句
        center.setAnimation(centerScaleAnimation);

        // 動畫開始
        centerScaleAnimation.startNow();

        center.setEnabled(false);
    }
}

相關文章

聯繫我們

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