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