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 17package com.example.android.batchstepsensor.cardstream; 18 19import android.content.Context; 20import android.util.AttributeSet; 21import android.view.MotionEvent; 22import android.view.ViewConfiguration; 23import android.widget.RelativeLayout; 24 25/** 26 * Custom Button with a special 'pressed' effect for touch events. 27 */ 28public class CardLayout extends RelativeLayout { 29 30 private boolean mSwiping = false; 31 private float mDownX = 0.f; 32 private float mDownY = 0.f; 33 private float mTouchSlop = 0.f; 34 35 public CardLayout(Context context) { 36 super(context); 37 init(); 38 } 39 40 public CardLayout(Context context, AttributeSet attrs) { 41 super(context, attrs); 42 init(); 43 } 44 45 public CardLayout(Context context, AttributeSet attrs, int defStyle) { 46 super(context, attrs, defStyle); 47 init(); 48 } 49 50 private void init(){ 51 setFocusable(true); 52 setDescendantFocusability(FOCUS_AFTER_DESCENDANTS); 53 setWillNotDraw(false); 54 setClickable(true); 55 56 mTouchSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop() * 2.f; 57 } 58 59 @Override 60 public boolean onTouchEvent(MotionEvent event) { 61 switch(event.getAction()){ 62 case MotionEvent.ACTION_CANCEL: 63 case MotionEvent.ACTION_UP: 64 mSwiping = false; 65 break; 66 } 67 return super.onTouchEvent(event); 68 } 69 70 @Override 71 public boolean onInterceptTouchEvent(MotionEvent event) { 72 73 switch(event.getAction()){ 74 case MotionEvent.ACTION_MOVE: 75 if( !mSwiping ){ 76 mSwiping = Math.abs(mDownX - event.getX()) > mTouchSlop; 77 } 78 break; 79 case MotionEvent.ACTION_DOWN: 80 mDownX = event.getX(); 81 mDownY = event.getY(); 82 mSwiping = false; 83 break; 84 case MotionEvent.ACTION_CANCEL: 85 case MotionEvent.ACTION_UP: 86 mSwiping = false; 87 break; 88 } 89 return mSwiping; 90 } 91}