2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package android.widget;
19 import android.content.Context;
20 import android.os.Handler;
21 import android.util.AttributeSet;
22 import android.view.KeyEvent;
23 import android.view.MotionEvent;
24 import android.view.View;
25 import android.view.View.OnLongClickListener;
27 public class ZoomButton extends ImageButton implements OnLongClickListener {
29 private final Handler mHandler;
30 private final Runnable mRunnable = new Runnable() {
32 if ((mOnClickListener != null) && mIsInLongpress && isEnabled()) {
33 mOnClickListener.onClick(ZoomButton.this);
34 mHandler.postDelayed(this, mZoomSpeed);
39 private long mZoomSpeed = 1000;
40 private boolean mIsInLongpress;
42 public ZoomButton(Context context) {
46 public ZoomButton(Context context, AttributeSet attrs) {
47 this(context, attrs, 0);
50 public ZoomButton(Context context, AttributeSet attrs, int defStyle) {
51 super(context, attrs, defStyle);
52 mHandler = new Handler();
53 setOnLongClickListener(this);
57 public boolean onTouchEvent(MotionEvent event) {
58 if ((event.getAction() == MotionEvent.ACTION_CANCEL)
59 || (event.getAction() == MotionEvent.ACTION_UP)) {
60 mIsInLongpress = false;
62 return super.onTouchEvent(event);
65 public void setZoomSpeed(long speed) {
69 public boolean onLongClick(View v) {
70 mIsInLongpress = true;
71 mHandler.post(mRunnable);
76 public boolean onKeyUp(int keyCode, KeyEvent event) {
77 mIsInLongpress = false;
78 return super.onKeyUp(keyCode, event);
82 public void setEnabled(boolean enabled) {
85 /* If we're being disabled reset the state back to unpressed
86 * as disabled views don't get events and therefore we won't
87 * get the up event to reset the state.
91 super.setEnabled(enabled);
95 public boolean dispatchUnhandledMove(View focused, int direction) {
97 return super.dispatchUnhandledMove(focused, direction);