ZoomControls.java revision 9066cfe9886ac131c34d59ed0e2d287b0e3c0087
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.annotation.Widget;
20import android.content.Context;
21import android.util.AttributeSet;
22import android.view.LayoutInflater;
23import android.view.MotionEvent;
24import android.view.View;
25import android.view.animation.AlphaAnimation;
26
27import 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 */
34@Widget
35public class ZoomControls extends LinearLayout {
36
37    private final ZoomButton mZoomIn;
38    private final ZoomButton mZoomOut;
39
40    public ZoomControls(Context context) {
41        this(context, null);
42    }
43
44    public ZoomControls(Context context, AttributeSet attrs) {
45        super(context, attrs);
46        setFocusable(false);
47
48        LayoutInflater inflater = (LayoutInflater) context
49                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
50        inflater.inflate(R.layout.zoom_controls, this, // we are the parent
51                true);
52
53        mZoomIn = (ZoomButton) findViewById(R.id.zoomIn);
54        mZoomOut = (ZoomButton) findViewById(R.id.zoomOut);
55    }
56
57    public void setOnZoomInClickListener(OnClickListener listener) {
58        mZoomIn.setOnClickListener(listener);
59    }
60
61    public void setOnZoomOutClickListener(OnClickListener listener) {
62        mZoomOut.setOnClickListener(listener);
63    }
64
65    /*
66     * Sets how fast you get zoom events when the user holds down the
67     * zoom in/out buttons.
68     */
69    public void setZoomSpeed(long speed) {
70        mZoomIn.setZoomSpeed(speed);
71        mZoomOut.setZoomSpeed(speed);
72    }
73
74    @Override
75    public boolean onTouchEvent(MotionEvent event) {
76
77        /* Consume all touch events so they don't get dispatched to the view
78         * beneath this view.
79         */
80        return true;
81    }
82
83    public void show() {
84        if (ZoomRingController.useOldZoom(mContext)) {
85            fade(View.VISIBLE, 0.0f, 1.0f);
86        }
87    }
88
89    public void hide() {
90        fade(View.GONE, 1.0f, 0.0f);
91    }
92
93    private void fade(int visibility, float startAlpha, float endAlpha) {
94        AlphaAnimation anim = new AlphaAnimation(startAlpha, endAlpha);
95        anim.setDuration(500);
96        startAnimation(anim);
97        setVisibility(visibility);
98    }
99
100    public void setIsZoomInEnabled(boolean isEnabled) {
101        mZoomIn.setEnabled(isEnabled);
102    }
103
104    public void setIsZoomOutEnabled(boolean isEnabled) {
105        mZoomOut.setEnabled(isEnabled);
106    }
107
108    @Override
109    public boolean hasFocus() {
110        return mZoomIn.hasFocus() || mZoomOut.hasFocus();
111    }
112}
113