Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android 點擊效果動畫增強

android 點擊效果動畫增強

編輯:關於Android編程

android 點擊效果動畫增強

1.使用開源庫制作view 的放大+透明度的動畫

animate(v).scaleX(factor).scaleY(factor).alpha(0);

2. 設計回調接口
    public interface ClickAnimation {
        public void onClick(View v);
    }

3. 在view 的onclick 的處理中增加動畫的調用,並把以前的onclick事件處理 作為回調加入到animation中,在動畫完成後回調處理函數。在動畫結束時將view的狀態恢復到原始狀態

    private void animateClickView(final View v, final ClickAnimation callback) {
        float factor = 2;
        animate(v).scaleX(factor).scaleY(factor).alpha(0).setListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                ViewHelper.setScaleX(v, 1);
                ViewHelper.setScaleY(v, 1);
                ViewHelper.setAlpha(v, 1);
                if (callback != null) {
                    callback.onClick(v);
                }
                super.onAnimationEnd(animation);
            }
        });
    }

4. TODO:可以依據這個結構,添加更多動畫效果。比如搖擺/震動等

實現效果參見youku視頻:

http://v.youku.com/v_show/id_XNjYzOTI4NzMy.html


具體實現參加代碼:

java文件

package com.buptfarmer.devapp;

import static com.nineoldandroids.view.ViewPropertyAnimator.animate;

import com.nineoldandroids.animation.Animator;
import com.nineoldandroids.animation.AnimatorListenerAdapter;
import com.nineoldandroids.view.ViewHelper;

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;
import android.widget.TextView;
import android.widget.Toast;

public class ClickAnimationExample extends Activity implements OnClickListener {

    private Button aaaBtn;
    private Button bbbBtn;
    private ImageView bbbImg;
    private Button cccBtn;
    private TextView aaaText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.click_animation_example);
        initView();
    }

    private void initView() {

        aaaBtn = (Button) findViewById(R.id.aaa_btn);
        aaaBtn.setOnClickListener(this);
        aaaText = (TextView) findViewById(R.id.aaa_text);
        aaaText.setOnClickListener(this);
        bbbBtn = (Button) findViewById(R.id.bbb_btn);
        bbbBtn.setOnClickListener(this);
        bbbImg = (ImageView) findViewById(R.id.bbb_img);
        bbbImg.setOnClickListener(this);
    }

    private void animateClickView(final View v, final ClickAnimation callback) {
        float factor = 2;
        animate(v).scaleX(factor).scaleY(factor).alpha(0).setListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                ViewHelper.setScaleX(v, 1);
                ViewHelper.setScaleY(v, 1);
                ViewHelper.setAlpha(v, 1);
                if (callback != null) {
                    callback.onClick(v);
                }
                super.onAnimationEnd(animation);
            }
        });
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        animateClickView(v, new ClickAnimation() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if (v == aaaBtn) {
                    Toast.makeText(getApplicationContext(), "aaaBtn has been clicked", Toast.LENGTH_SHORT)
                            .show();
                } else if (v == aaaText) {
                    Toast.makeText(getApplicationContext(), "aaaText has been clicked", Toast.LENGTH_SHORT)
                            .show();
                } else if (v == bbbBtn) {
                    Toast.makeText(getApplicationContext(), "bbbBtn has been clicked", Toast.LENGTH_SHORT)
                            .show();
                } else if (v == bbbImg) {
                    Toast.makeText(getApplicationContext(), "bbbImg has been clicked", Toast.LENGTH_SHORT)
                            .show();
                }
            }
        });
    }

    public interface ClickAnimation {
        public void onClick(View v);
    }
}

layout 布局文件


    

    

    

        



  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved