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 17 package android.widget; 18 19 import android.annotation.Widget; 20 import android.content.Context; 21 import android.util.AttributeSet; 22 import android.view.LayoutInflater; 23 import android.view.MotionEvent; 24 import android.view.View; 25 import android.view.accessibility.AccessibilityEvent; 26 import android.view.accessibility.AccessibilityNodeInfo; 27 import android.view.animation.AlphaAnimation; 28 29 import com.android.internal.R; 30 31 32 /** 33 * The {@code ZoomControls} class displays a simple set of controls used for zooming and 34 * provides callbacks to register for events. */ 35 @Widget 36 public class ZoomControls extends LinearLayout { 37 38 private final ZoomButton mZoomIn; 39 private final ZoomButton mZoomOut; 40 ZoomControls(Context context)41 public ZoomControls(Context context) { 42 this(context, null); 43 } 44 ZoomControls(Context context, AttributeSet attrs)45 public ZoomControls(Context context, AttributeSet attrs) { 46 super(context, attrs); 47 setFocusable(false); 48 49 LayoutInflater inflater = (LayoutInflater) context 50 .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 51 inflater.inflate(R.layout.zoom_controls, this, // we are the parent 52 true); 53 54 mZoomIn = (ZoomButton) findViewById(R.id.zoomIn); 55 mZoomOut = (ZoomButton) findViewById(R.id.zoomOut); 56 } 57 setOnZoomInClickListener(OnClickListener listener)58 public void setOnZoomInClickListener(OnClickListener listener) { 59 mZoomIn.setOnClickListener(listener); 60 } 61 setOnZoomOutClickListener(OnClickListener listener)62 public void setOnZoomOutClickListener(OnClickListener listener) { 63 mZoomOut.setOnClickListener(listener); 64 } 65 66 /* 67 * Sets how fast you get zoom events when the user holds down the 68 * zoom in/out buttons. 69 */ setZoomSpeed(long speed)70 public void setZoomSpeed(long speed) { 71 mZoomIn.setZoomSpeed(speed); 72 mZoomOut.setZoomSpeed(speed); 73 } 74 75 @Override onTouchEvent(MotionEvent event)76 public boolean onTouchEvent(MotionEvent event) { 77 78 /* Consume all touch events so they don't get dispatched to the view 79 * beneath this view. 80 */ 81 return true; 82 } 83 show()84 public void show() { 85 fade(View.VISIBLE, 0.0f, 1.0f); 86 } 87 hide()88 public void hide() { 89 fade(View.GONE, 1.0f, 0.0f); 90 } 91 fade(int visibility, float startAlpha, float endAlpha)92 private void fade(int visibility, float startAlpha, float endAlpha) { 93 AlphaAnimation anim = new AlphaAnimation(startAlpha, endAlpha); 94 anim.setDuration(500); 95 startAnimation(anim); 96 setVisibility(visibility); 97 } 98 setIsZoomInEnabled(boolean isEnabled)99 public void setIsZoomInEnabled(boolean isEnabled) { 100 mZoomIn.setEnabled(isEnabled); 101 } 102 setIsZoomOutEnabled(boolean isEnabled)103 public void setIsZoomOutEnabled(boolean isEnabled) { 104 mZoomOut.setEnabled(isEnabled); 105 } 106 107 @Override hasFocus()108 public boolean hasFocus() { 109 return mZoomIn.hasFocus() || mZoomOut.hasFocus(); 110 } 111 112 @Override onInitializeAccessibilityEvent(AccessibilityEvent event)113 public void onInitializeAccessibilityEvent(AccessibilityEvent event) { 114 super.onInitializeAccessibilityEvent(event); 115 event.setClassName(ZoomControls.class.getName()); 116 } 117 118 @Override onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info)119 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) { 120 super.onInitializeAccessibilityNodeInfo(info); 121 info.setClassName(ZoomControls.class.getName()); 122 } 123 } 124