ZoomButton.java revision 9066cfe9886ac131c34d59ed0e2d287b0e3c0087
1/* 2 * Copyright (C) 2008 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 android.widget; 18 19import android.content.Context; 20import android.os.Handler; 21import android.util.AttributeSet; 22import android.view.GestureDetector; 23import android.view.HapticFeedbackConstants; 24import android.view.KeyEvent; 25import android.view.MotionEvent; 26import android.view.View; 27import android.view.GestureDetector.SimpleOnGestureListener; 28import android.view.View.OnLongClickListener; 29 30 31public class ZoomButton extends ImageButton implements OnLongClickListener { 32 33 private final Handler mHandler; 34 private final Runnable mRunnable = new Runnable() { 35 public void run() { 36 if ((mOnClickListener != null) && mIsInLongpress && isEnabled()) { 37 mOnClickListener.onClick(ZoomButton.this); 38 mHandler.postDelayed(this, mZoomSpeed); 39 } 40 } 41 }; 42 private final GestureDetector mGestureDetector; 43 44 private long mZoomSpeed = 1000; 45 private boolean mIsInLongpress; 46 47 public ZoomButton(Context context) { 48 this(context, null); 49 } 50 51 public ZoomButton(Context context, AttributeSet attrs) { 52 this(context, attrs, 0); 53 } 54 55 public ZoomButton(Context context, AttributeSet attrs, int defStyle) { 56 super(context, attrs, defStyle); 57 mHandler = new Handler(); 58 mGestureDetector = new GestureDetector(context, new SimpleOnGestureListener() { 59 @Override 60 public void onLongPress(MotionEvent e) { 61 performHapticFeedback(HapticFeedbackConstants.LONG_PRESS); 62 onLongClick(ZoomButton.this); 63 } 64 }); 65 setOnLongClickListener(this); 66 } 67 68 @Override 69 public boolean onTouchEvent(MotionEvent event) { 70 mGestureDetector.onTouchEvent(event); 71 if ((event.getAction() == MotionEvent.ACTION_CANCEL) 72 || (event.getAction() == MotionEvent.ACTION_UP)) { 73 mIsInLongpress = false; 74 } 75 return super.onTouchEvent(event); 76 } 77 78 public void setZoomSpeed(long speed) { 79 mZoomSpeed = speed; 80 } 81 82 public boolean onLongClick(View v) { 83 mIsInLongpress = true; 84 mHandler.post(mRunnable); 85 return true; 86 } 87 88 @Override 89 public boolean onKeyUp(int keyCode, KeyEvent event) { 90 mIsInLongpress = false; 91 return super.onKeyUp(keyCode, event); 92 } 93 94 @Override 95 public void setEnabled(boolean enabled) { 96 if (!enabled) { 97 98 /* If we're being disabled reset the state back to unpressed 99 * as disabled views don't get events and therefore we won't 100 * get the up event to reset the state. 101 */ 102 setPressed(false); 103 } 104 super.setEnabled(enabled); 105 } 106 107 @Override 108 public boolean dispatchUnhandledMove(View focused, int direction) { 109 clearFocus(); 110 return super.dispatchUnhandledMove(focused, direction); 111 } 112} 113