OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.ui.resources.dynamics; | 5 package org.chromium.ui.resources.dynamics; |
6 | 6 |
7 import android.graphics.Bitmap; | 7 import android.graphics.Bitmap; |
8 import android.graphics.Canvas; | 8 import android.graphics.Canvas; |
9 import android.graphics.Color; | 9 import android.graphics.Color; |
10 import android.graphics.Rect; | 10 import android.graphics.Rect; |
11 import android.view.View; | 11 import android.view.View; |
12 import android.view.View.OnLayoutChangeListener; | 12 import android.view.View.OnLayoutChangeListener; |
13 | 13 |
14 import org.chromium.base.TraceEvent; | 14 import org.chromium.base.TraceEvent; |
15 import org.chromium.ui.resources.Resource; | 15 import org.chromium.ui.resources.Resource; |
16 import org.chromium.ui.resources.ResourceFactory; | 16 import org.chromium.ui.resources.ResourceFactory; |
17 import org.chromium.ui.resources.statics.NinePatchData; | 17 import org.chromium.ui.resources.statics.NinePatchData; |
18 | 18 |
19 /** | 19 /** |
20 * An adapter that exposes a {@link View} as a {@link DynamicResource}. In order
to properly use | 20 * An adapter that exposes a {@link View} as a {@link DynamicResource}. In order
to properly use |
21 * this adapter {@link ViewResourceAdapter#invalidate(Rect)} must be called when
parts of the | 21 * this adapter {@link ViewResourceAdapter#invalidate(Rect)} must be called when
parts of the |
22 * {@link View} are invalidated. For {@link ViewGroup}s the easiest way to do t
his is to override | 22 * {@link View} are invalidated. For {@link ViewGroup}s the easiest way to do t
his is to override |
23 * {@link View#invalidateChildInParent(int[], Rect)}. | 23 * {@link View#invalidateChildInParent(int[], Rect)}. |
24 */ | 24 */ |
25 public class ViewResourceAdapter implements DynamicResource, OnLayoutChangeListe
ner { | 25 public class ViewResourceAdapter implements DynamicResource, OnLayoutChangeListe
ner { |
26 private final View mView; | 26 private final View mView; |
27 private final Rect mDirtyRect = new Rect(); | 27 private final Rect mDirtyRect = new Rect(); |
28 private final Rect mContentPadding = new Rect(); | |
29 private final Rect mContentAperture = new Rect(); | |
30 | 28 |
31 private Bitmap mBitmap; | 29 private Bitmap mBitmap; |
32 private Rect mBitmapSize = new Rect(); | 30 private Rect mBitmapSize = new Rect(); |
33 | 31 |
34 /** | 32 /** |
35 * Builds a {@link ViewResourceAdapter} instance around {@code view}. | 33 * Builds a {@link ViewResourceAdapter} instance around {@code view}. |
36 * @param view The {@link View} to expose as a {@link Resource}. | 34 * @param view The {@link View} to expose as a {@link Resource}. |
37 */ | 35 */ |
38 public ViewResourceAdapter(View view) { | 36 public ViewResourceAdapter(View view) { |
39 mView = view; | 37 mView = view; |
(...skipping 28 matching lines...) Expand all Loading... |
68 mDirtyRect.setEmpty(); | 66 mDirtyRect.setEmpty(); |
69 TraceEvent.end("ViewResourceAdapter:getBitmap"); | 67 TraceEvent.end("ViewResourceAdapter:getBitmap"); |
70 return mBitmap; | 68 return mBitmap; |
71 } | 69 } |
72 | 70 |
73 @Override | 71 @Override |
74 public Rect getBitmapSize() { | 72 public Rect getBitmapSize() { |
75 return mBitmapSize; | 73 return mBitmapSize; |
76 } | 74 } |
77 | 75 |
| 76 /** |
| 77 * Override this method to create the native resource type for the generated
bitmap. |
| 78 */ |
78 @Override | 79 @Override |
79 public long createNativeResource() { | 80 public long createNativeResource() { |
80 // TODO(khushalsagar): Fix this to create the correct native resource ty
pe. | 81 return ResourceFactory.createBitmapResource(null); |
81 // See crbug.com/700454. | |
82 computeContentPadding(mContentPadding); | |
83 computeContentAperture(mContentAperture); | |
84 return ResourceFactory.createNinePatchBitmapResource(mContentPadding, mC
ontentAperture); | |
85 } | 82 } |
86 | 83 |
87 @Override | 84 @Override |
88 public NinePatchData getNinePatchData() { | 85 public final NinePatchData getNinePatchData() { |
89 return null; | 86 return null; |
90 } | 87 } |
91 | 88 |
92 @Override | 89 @Override |
93 public boolean isDirty() { | 90 public boolean isDirty() { |
94 if (mBitmap == null) mDirtyRect.set(0, 0, mView.getWidth(), mView.getHei
ght()); | 91 if (mBitmap == null) mDirtyRect.set(0, 0, mView.getWidth(), mView.getHei
ght()); |
95 | 92 |
96 return !mDirtyRect.isEmpty(); | 93 return !mDirtyRect.isEmpty(); |
97 } | 94 } |
98 | 95 |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
143 mView.draw(canvas); | 140 mView.draw(canvas); |
144 } | 141 } |
145 | 142 |
146 /** | 143 /** |
147 * Called after {@link #capture(Canvas)}. | 144 * Called after {@link #capture(Canvas)}. |
148 */ | 145 */ |
149 protected void onCaptureEnd() { | 146 protected void onCaptureEnd() { |
150 } | 147 } |
151 | 148 |
152 /** | 149 /** |
153 * Gives overriding classes the chance to specify a different content paddin
g. | |
154 * @param outContentPadding The resulting content padding. | |
155 */ | |
156 protected void computeContentPadding(Rect outContentPadding) { | |
157 outContentPadding.set(0, 0, mView.getWidth(), mView.getHeight()); | |
158 } | |
159 | |
160 /** | |
161 * Gives overriding classes the chance to specify a different content apertu
re. | |
162 * @param outContentAperture The resulting content aperture. | |
163 */ | |
164 protected void computeContentAperture(Rect outContentAperture) { | |
165 outContentAperture.set(0, 0, mView.getWidth(), mView.getHeight()); | |
166 } | |
167 | |
168 /** | |
169 * @return Whether |mBitmap| is corresponding to |mView| or not. | 150 * @return Whether |mBitmap| is corresponding to |mView| or not. |
170 */ | 151 */ |
171 private boolean validateBitmap() { | 152 private boolean validateBitmap() { |
172 int viewWidth = mView.getWidth(); | 153 int viewWidth = mView.getWidth(); |
173 int viewHeight = mView.getHeight(); | 154 int viewHeight = mView.getHeight(); |
174 boolean isEmpty = viewWidth == 0 || viewHeight == 0; | 155 boolean isEmpty = viewWidth == 0 || viewHeight == 0; |
175 if (isEmpty) { | 156 if (isEmpty) { |
176 viewWidth = 1; | 157 viewWidth = 1; |
177 viewHeight = 1; | 158 viewHeight = 1; |
178 } | 159 } |
179 if (mBitmap != null | 160 if (mBitmap != null |
180 && (mBitmap.getWidth() != viewWidth || mBitmap.getHeight() != vi
ewHeight)) { | 161 && (mBitmap.getWidth() != viewWidth || mBitmap.getHeight() != vi
ewHeight)) { |
181 mBitmap.recycle(); | 162 mBitmap.recycle(); |
182 mBitmap = null; | 163 mBitmap = null; |
183 } | 164 } |
184 | 165 |
185 if (mBitmap == null) { | 166 if (mBitmap == null) { |
186 mBitmap = Bitmap.createBitmap(viewWidth, viewHeight, Bitmap.Config.A
RGB_8888); | 167 mBitmap = Bitmap.createBitmap(viewWidth, viewHeight, Bitmap.Config.A
RGB_8888); |
187 mBitmap.setHasAlpha(true); | 168 mBitmap.setHasAlpha(true); |
188 mDirtyRect.set(0, 0, viewWidth, viewHeight); | 169 mDirtyRect.set(0, 0, viewWidth, viewHeight); |
189 mBitmapSize.set(0, 0, mBitmap.getWidth(), mBitmap.getHeight()); | 170 mBitmapSize.set(0, 0, mBitmap.getWidth(), mBitmap.getHeight()); |
190 } | 171 } |
191 | 172 |
192 return !isEmpty; | 173 return !isEmpty; |
193 } | 174 } |
194 } | 175 } |
OLD | NEW |