1 /* 2 * Copyright (C) 2013 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.example.android.livebutton; 18 19 import android.app.Activity; 20 import android.os.Bundle; 21 import android.view.MotionEvent; 22 import android.view.View; 23 import android.view.animation.DecelerateInterpolator; 24 import android.view.animation.OvershootInterpolator; 25 import android.widget.Button; 26 27 /** 28 * This app shows a simple application of anticipation and follow-through techniques as 29 * the button animates into its pressed state and animates back out of it, overshooting 30 * end state before resolving. 31 * 32 * Watch the associated video for this demo on the DevBytes channel of developer.android.com 33 * or on the DevBytes playlist in the androiddevelopers channel on YouTube at 34 * https://www.youtube.com/playlist?list=PLWz5rJ2EKKc_XOgcRukSoKKjewFJZrKV0. 35 */ 36 public class LiveButton extends Activity { 37 38 DecelerateInterpolator sDecelerator = new DecelerateInterpolator(); 39 OvershootInterpolator sOvershooter = new OvershootInterpolator(10f); 40 41 @Override onCreate(Bundle savedInstanceState)42 protected void onCreate(Bundle savedInstanceState) { 43 super.onCreate(savedInstanceState); 44 setContentView(R.layout.activity_overshoot); 45 46 final Button clickMeButton = (Button) findViewById(R.id.clickMe); 47 clickMeButton.animate().setDuration(200); 48 49 clickMeButton.setOnTouchListener(new View.OnTouchListener() { 50 51 @Override 52 public boolean onTouch(View arg0, MotionEvent arg1) { 53 if (arg1.getAction() == MotionEvent.ACTION_DOWN) { 54 clickMeButton.animate().setInterpolator(sDecelerator). 55 scaleX(.7f).scaleY(.7f); 56 } else if (arg1.getAction() == MotionEvent.ACTION_UP) { 57 clickMeButton.animate().setInterpolator(sOvershooter). 58 scaleX(1f).scaleY(1f); 59 } 60 return false; 61 } 62 }); 63 64 } 65 } 66