1 /* 2 * Copyright 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 18 package com.example.android.batchstepsensor.cardstream; 19 20 import android.content.Context; 21 import android.os.Build; 22 import android.support.v4.view.ViewCompat; 23 import android.util.AttributeSet; 24 import android.view.MotionEvent; 25 import android.view.animation.BounceInterpolator; 26 import android.view.animation.DecelerateInterpolator; 27 import android.widget.Button; 28 29 /** 30 * Custom Button with a special 'pressed' effect for touch events. 31 */ 32 public class CardActionButton extends Button { 33 CardActionButton(Context context)34 public CardActionButton(Context context) { 35 super(context); 36 } 37 CardActionButton(Context context, AttributeSet attrs)38 public CardActionButton(Context context, AttributeSet attrs) { 39 super(context, attrs); 40 } 41 CardActionButton(Context context, AttributeSet attrs, int defStyle)42 public CardActionButton(Context context, AttributeSet attrs, int defStyle) { 43 super(context, attrs, defStyle); 44 } 45 46 @Override onTouchEvent(MotionEvent event)47 public boolean onTouchEvent(MotionEvent event) { 48 49 switch (event.getAction()) { 50 case MotionEvent.ACTION_DOWN: { 51 setPressed(true); 52 if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT) { 53 animate().scaleX(0.98f).scaleY(0.98f).setDuration(100) 54 .setInterpolator(new DecelerateInterpolator()); 55 } else { 56 ViewCompat.setElevation(this, 8.f); 57 } 58 break; 59 } 60 case MotionEvent.ACTION_UP: 61 case MotionEvent.ACTION_CANCEL: { 62 setPressed(false); 63 if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT) { 64 animate().scaleX(1.f).scaleY(1.f).setDuration(50) 65 .setInterpolator(new BounceInterpolator()); 66 } else { 67 ViewCompat.setElevation(this, 0.f); 68 } 69 break; 70 } 71 } 72 73 return super.onTouchEvent(event); 74 } 75 76 } 77 78