OSDN Git Service

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