android笔记——点击按钮放大

package com.example.button;

import android.os.Bundle;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;


public class MainActivity extends Activity {

 private Button button;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  button = (Button) findViewById(R.id.button);
  button.setOnTouchListener(new Button.OnTouchListener() {
   @Override
   public boolean onTouch(View v, MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
     //点击按钮未松开时按钮放大
     blow_up(button);
    } else if (event.getAction() == MotionEvent.ACTION_UP) {
     //点击按钮松开后按钮缩小
     narrow(button);
    }
    return false;
   }
  });

 }
 //放大按钮动画
 private void blow_up(View v) {
  float[] vaules = new float[] { 1.0f, 1.1f, 1.2f, 1.3f, 1.4f, 1.5f};
  AnimatorSet set = new AnimatorSet();
  set.playTogether(ObjectAnimator.ofFloat(v, "scaleX", vaules),
    ObjectAnimator.ofFloat(v, "scaleY", vaules));
  set.setDuration(150);
  set.start();
 }
 //缩小按钮动画
 private void narrow(View v) {
  float[] vaules = new float[] { 1.5f, 1.4f, 1.3f, 1.2f, 1.1f, 1.0f};
  AnimatorSet set = new AnimatorSet();
  set.playTogether(ObjectAnimator.ofFloat(v, "scaleX", vaules),
    ObjectAnimator.ofFloat(v, "scaleY", vaules));
  set.setDuration(150);
  set.start();
 }
}


布局文件:



    


 Animation介绍

http://www.cnblogs.com/shaweng/archive/2012/06/08/2541258.html

你可能感兴趣的:(Android笔记)