• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package com.example.android.apis.graphics;
18 
19 import android.content.Context;
20 import android.graphics.Canvas;
21 import android.graphics.Picture;
22 import android.graphics.Rect;
23 import android.graphics.drawable.Drawable;
24 import android.util.AttributeSet;
25 import android.view.View;
26 import android.view.ViewGroup;
27 import android.view.ViewParent;
28 
29 
30 public class PictureLayout extends ViewGroup {
31     private final Picture mPicture = new Picture();
32 
PictureLayout(Context context)33     public PictureLayout(Context context) {
34         super(context);
35     }
36 
PictureLayout(Context context, AttributeSet attrs)37     public PictureLayout(Context context, AttributeSet attrs) {
38         super(context, attrs);
39     }
40 
41     @Override
addView(View child)42     public void addView(View child) {
43         if (getChildCount() > 1) {
44             throw new IllegalStateException("PictureLayout can host only one direct child");
45         }
46 
47         super.addView(child);
48     }
49 
50     @Override
addView(View child, int index)51     public void addView(View child, int index) {
52         if (getChildCount() > 1) {
53             throw new IllegalStateException("PictureLayout can host only one direct child");
54         }
55 
56         super.addView(child, index);
57     }
58 
59     @Override
addView(View child, LayoutParams params)60     public void addView(View child, LayoutParams params) {
61         if (getChildCount() > 1) {
62             throw new IllegalStateException("PictureLayout can host only one direct child");
63         }
64 
65         super.addView(child, params);
66     }
67 
68     @Override
addView(View child, int index, LayoutParams params)69     public void addView(View child, int index, LayoutParams params) {
70         if (getChildCount() > 1) {
71             throw new IllegalStateException("PictureLayout can host only one direct child");
72         }
73 
74         super.addView(child, index, params);
75     }
76 
77     @Override
generateDefaultLayoutParams()78     protected LayoutParams generateDefaultLayoutParams() {
79         return new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
80     }
81 
82     @Override
onMeasure(int widthMeasureSpec, int heightMeasureSpec)83     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
84         final int count = getChildCount();
85 
86         int maxHeight = 0;
87         int maxWidth = 0;
88 
89         for (int i = 0; i < count; i++) {
90             final View child = getChildAt(i);
91             if (child.getVisibility() != GONE) {
92                 measureChild(child, widthMeasureSpec, heightMeasureSpec);
93             }
94         }
95 
96         maxWidth += getPaddingLeft() + getPaddingRight();
97         maxHeight += getPaddingTop() + getPaddingBottom();
98 
99         Drawable drawable = getBackground();
100         if (drawable != null) {
101             maxHeight = Math.max(maxHeight, drawable.getMinimumHeight());
102             maxWidth = Math.max(maxWidth, drawable.getMinimumWidth());
103         }
104 
105         setMeasuredDimension(resolveSize(maxWidth, widthMeasureSpec),
106                 resolveSize(maxHeight, heightMeasureSpec));
107     }
108 
drawPict(Canvas canvas, int x, int y, int w, int h, float sx, float sy)109     private void drawPict(Canvas canvas, int x, int y, int w, int h,
110                           float sx, float sy) {
111         canvas.save();
112         canvas.translate(x, y);
113         canvas.clipRect(0, 0, w, h);
114         canvas.scale(0.5f, 0.5f);
115         canvas.scale(sx, sy, w, h);
116         canvas.drawPicture(mPicture);
117         canvas.restore();
118     }
119 
120     @Override
dispatchDraw(Canvas canvas)121     protected void dispatchDraw(Canvas canvas) {
122         super.dispatchDraw(mPicture.beginRecording(getWidth(), getHeight()));
123         mPicture.endRecording();
124 
125         int x = getWidth()/2;
126         int y = getHeight()/2;
127 
128         if (false) {
129             canvas.drawPicture(mPicture);
130         } else {
131             drawPict(canvas, 0, 0, x, y,  1,  1);
132             drawPict(canvas, x, 0, x, y, -1,  1);
133             drawPict(canvas, 0, y, x, y,  1, -1);
134             drawPict(canvas, x, y, x, y, -1, -1);
135         }
136     }
137 
138     @Override
invalidateChildInParent(int[] location, Rect dirty)139     public ViewParent invalidateChildInParent(int[] location, Rect dirty) {
140         location[0] = getLeft();
141         location[1] = getTop();
142         dirty.set(0, 0, getWidth(), getHeight());
143         return getParent();
144     }
145 
146     @Override
onLayout(boolean changed, int l, int t, int r, int b)147     protected void onLayout(boolean changed, int l, int t, int r, int b) {
148         final int count = super.getChildCount();
149 
150         for (int i = 0; i < count; i++) {
151             final View child = getChildAt(i);
152             if (child.getVisibility() != GONE) {
153                 final int childLeft = getPaddingLeft();
154                 final int childTop = getPaddingTop();
155                 child.layout(childLeft, childTop,
156                         childLeft + child.getMeasuredWidth(),
157                         childTop + child.getMeasuredHeight());
158 
159             }
160         }
161     }
162 }
163