PieListView.java revision 1acef69ffc079d1bc029ff7eb1f5043f7efd7f36
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.graphics.Paint;
24import android.view.View;
25
26/**
27 * shows views in a menu style list
28 */
29public class PieListView extends BasePieView {
30
31    private Paint mBgPaint;
32
33    public PieListView(Context ctx) {
34        mBgPaint = new Paint();
35        mBgPaint.setColor(ctx.getResources().getColor(R.color.qcMenuBackground));
36    }
37
38    /**
39     * this will be called before the first draw call
40     */
41    @Override
42    public void layout(int anchorX, int anchorY, boolean left) {
43        buildViews();
44        mWidth = mChildWidth;
45        mHeight = mChildHeight * mAdapter.getCount();
46        mLeft = anchorX + (left ? 0 : - mChildWidth);
47        mTop = anchorY - mHeight / 2;
48        if (mViews != null) {
49            layoutChildrenLinear();
50        }
51    }
52
53    protected void layoutChildrenLinear() {
54        final int n = mViews.size();
55        int top = mTop;
56        for (View view : mViews) {
57            view.layout(mLeft, top, mLeft + mChildWidth, top + mChildHeight);
58            top += mChildHeight;
59        }
60    }
61
62    @Override
63    public void draw(Canvas canvas) {
64        canvas.drawRect(mLeft, mTop, mLeft + mWidth, mTop + mHeight, mBgPaint);
65        if (mViews != null) {
66            for (View view : mViews) {
67                drawView(view, canvas);
68            }
69        }
70    }
71
72    @Override
73    protected int findChildAt(int y) {
74        final int ix = (y - mTop) * mViews.size() / mHeight;
75        return ix;
76    }
77
78}
79