RenderOverlay.java revision 38a6c24485d36651b93d5935bfbaf3cd367885e7
1/*
2 * Copyright (C) 2012 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.camera.ui;
18
19import android.content.Context;
20import android.graphics.Canvas;
21import android.util.AttributeSet;
22import android.view.View;
23
24import java.util.ArrayList;
25import java.util.List;
26
27public class RenderOverlay extends RotateLayout {
28
29    private static final String TAG = "CAM_Overlay";
30
31    interface Renderer {
32
33        public void setOverlay(RenderOverlay overlay);
34        public void layout(int left, int top, int right, int bottom);
35        public void draw(Canvas canvas);
36
37    }
38
39    private RenderView mRenderView;
40    private List<Renderer> mClients;
41
42    public RenderOverlay(Context context, AttributeSet attrs) {
43        super(context, attrs);
44        mRenderView = new RenderView(context);
45        addView(mRenderView, new LayoutParams(LayoutParams.MATCH_PARENT,
46                LayoutParams.MATCH_PARENT));
47        mClients = new ArrayList<Renderer>(10);
48    }
49
50    public void addRenderer(Renderer renderer) {
51        mClients.add(renderer);
52        renderer.setOverlay(this);
53    }
54
55    public void addRenderer(int pos, Renderer renderer) {
56        mClients.add(pos, renderer);
57        renderer.setOverlay(this);
58    }
59
60    public void remove(Renderer renderer) {
61        mClients.remove(renderer);
62        renderer.setOverlay(null);
63    }
64
65    public int getClientSize() {
66        return mClients.size();
67    }
68
69    private class RenderView extends View {
70
71        public RenderView(Context context) {
72            super(context);
73            setWillNotDraw(false);
74        }
75
76        @Override
77        public void layout(int left, int top, int right, int bottom) {
78            super.layout(left,  top, right, bottom);
79            if (mClients == null) return;
80            for (Renderer renderer : mClients) {
81                renderer.layout(left, top, right, bottom);
82            }
83        }
84
85        @Override
86        public void draw(Canvas canvas) {
87            if (mClients == null) return;
88            for (Renderer renderer : mClients) {
89                renderer.draw(canvas);
90            }
91            invalidate();
92        }
93    }
94
95}
96