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