PieStackView.java revision a4befac241ec69791fd30e11355ed3dc10d7fb37
1/*
2 * Copyright (C) 2011 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 com.android.browser.view;
18
19import com.android.browser.R;
20
21import android.content.Context;
22import android.graphics.Canvas;
23import android.view.View;
24
25/**
26 * shows views in a stack
27 */
28public class PieStackView extends BasePieView {
29
30    private static final int SLOP = 5;
31
32    private OnCurrentListener mCurrentListener;
33    private int mMinHeight;
34
35    public interface OnCurrentListener {
36        public void onSetCurrent(int index);
37    }
38
39    public PieStackView(Context ctx) {
40        mMinHeight = (int) ctx.getResources()
41                .getDimension(R.dimen.qc_tab_title_height);
42    }
43
44    public void setOnCurrentListener(OnCurrentListener l) {
45        mCurrentListener = l;
46    }
47
48    @Override
49    public void setCurrent(int ix) {
50        super.setCurrent(ix);
51        if (mCurrentListener != null) {
52            mCurrentListener.onSetCurrent(ix);
53            buildViews();
54            layoutChildrenLinear();
55        }
56    }
57
58    /**
59     * this will be called before the first draw call
60     */
61    @Override
62    public void layout(int anchorX, int anchorY, boolean left, float angle,
63            int pHeight) {
64        super.layout(anchorX, anchorY, left, angle, pHeight);
65        buildViews();
66        mWidth = mChildWidth;
67        mHeight = mChildHeight + (mViews.size() - 1) * mMinHeight;
68        mLeft = anchorX + (left ? SLOP : -(SLOP + mChildWidth));
69        mTop = anchorY - mHeight / 2;
70        if (mViews != null) {
71            layoutChildrenLinear();
72        }
73    }
74
75    private void layoutChildrenLinear() {
76        final int n = mViews.size();
77        int top = mTop;
78        int dy = (n == 1) ? 0 : (mHeight - mChildHeight) / (n - 1);
79        for (View view : mViews) {
80            int x = mLeft;
81            view.layout(x, top, x + mChildWidth, top + mChildHeight);
82            top += dy;
83        }
84    }
85
86    @Override
87    public void draw(Canvas canvas) {
88        if ((mViews != null) && (mCurrent > -1)) {
89            final int n = mViews.size();
90            for (int i = 0; i < mCurrent; i++) {
91                drawView(mViews.get(i), canvas);
92            }
93            for (int i = n - 1; i > mCurrent; i--) {
94                drawView(mViews.get(i), canvas);
95            }
96            drawView(mViews.get(mCurrent), canvas);
97        }
98    }
99
100    @Override
101    protected int findChildAt(int y) {
102        final int ix = (y - mTop) * mViews.size() / mHeight;
103        return ix;
104    }
105
106}
107