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