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