PieStackView.java revision 565752e1025129ad16c030ceca9dee7a695ed73e
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        super.layout(anchorX, anchorY, left, angle);
64        buildViews();
65        mWidth = mChildWidth;
66        mHeight = mChildHeight + (mViews.size() - 1) * mMinHeight;
67        mLeft = anchorX + (left ? SLOP : -(SLOP + mChildWidth));
68        mTop = anchorY - mHeight / 2;
69        if (mViews != null) {
70            layoutChildrenLinear();
71        }
72    }
73
74    private void layoutChildrenLinear() {
75        final int n = mViews.size();
76        int top = mTop;
77        int dy = (n == 1) ? 0 : (mHeight - mChildHeight) / (n - 1);
78        for (View view : mViews) {
79            int x = mLeft;
80            view.layout(x, top, x + mChildWidth, top + mChildHeight);
81            top += dy;
82        }
83    }
84
85    @Override
86    public void draw(Canvas canvas) {
87        if (mViews != null) {
88            final int n = mViews.size();
89            for (int i = 0; i < mCurrent; i++) {
90                drawView(mViews.get(i), canvas);
91            }
92            for (int i = n - 1; i > mCurrent; i--) {
93                drawView(mViews.get(i), canvas);
94            }
95            drawView(mViews.get(mCurrent), canvas);
96        }
97    }
98
99    @Override
100    protected int findChildAt(int y) {
101        final int ix = (y - mTop) * mViews.size() / mHeight;
102        return ix;
103    }
104
105}
106