OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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.content.Context; | 7 import android.content.Context; |
8 import android.view.LayoutInflater; | 8 import android.view.LayoutInflater; |
9 import android.view.View; | 9 import android.view.View; |
10 import android.view.ViewGroup; | 10 import android.view.ViewGroup; |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
121 // to the hierarchy (if allowed) or snapshots are captured. | 121 // to the hierarchy (if allowed) or snapshots are captured. |
122 onFinishInflate(); | 122 onFinishInflate(); |
123 | 123 |
124 registerResource(); | 124 registerResource(); |
125 } | 125 } |
126 | 126 |
127 /** | 127 /** |
128 * Invalidate the inflated View, causing a snapshot of the View to be captur
ed. | 128 * Invalidate the inflated View, causing a snapshot of the View to be captur
ed. |
129 */ | 129 */ |
130 public void invalidate() { | 130 public void invalidate() { |
| 131 invalidate(false); |
| 132 } |
| 133 |
| 134 /** |
| 135 * Invalidate the inflated View, causing a snapshot of the View to be captur
ed. |
| 136 * |
| 137 * @param didViewSizeChange Whether the View's size has changed.. |
| 138 */ |
| 139 public void invalidate(boolean didViewSizeChange) { |
131 // View must be inflated at this point. If it's not, do it now. | 140 // View must be inflated at this point. If it's not, do it now. |
132 if (mView == null) { | 141 if (mView == null) { |
133 inflate(); | 142 inflate(); |
134 } | 143 } |
135 | 144 |
136 mIsInvalidated = true; | 145 mIsInvalidated = true; |
137 | 146 |
138 // If the View is already attached, we don't need to do anything because
the | 147 if (!mIsAttached && shouldAttachView()) { |
139 // snapshot will be captured automatically when the View is drawn. | 148 // TODO(pedrosimonetti): investigate if complex views can be rendere
d offline. |
140 if (!mIsAttached) { | 149 // NOTE(pedrosimonetti): it seems that complex views don't get rende
red |
141 if (shouldAttachView()) { | 150 // properly if not attached to the hierarchy. The problem seem to be
related |
142 // TODO(pedrosimonetti): investigate if complex views can be ren
dered offline. | 151 // to the use of the property "layout_gravity: end", possibly in com
bination |
143 // NOTE(pedrosimonetti): it seems that complex views don't get r
endered | 152 // of other things like elastic views (layout_weight: 1) and/or fadi
ng edges. |
144 // properly if not attached to the hierarchy. The problem seem t
o be related | 153 attachView(); |
145 // to the use of the property "layout_gravity: end", possibly in
combination | 154 } |
146 // of other things like elastic views (layout_weight: 1) and/or
fading edges. | 155 |
147 attachView(); | 156 if (mIsAttached) { |
148 } else { | 157 // Update the View's layout params, which will trigger a re-layout. |
149 // When the View is not attached, we need to manually layout the
View | 158 if (didViewSizeChange) { |
150 // and invalidate the resource in order to capture a new snapsho
t. | 159 updateLayoutParams(); |
151 layout(); | |
152 invalidateResource(); | |
153 } | 160 } |
| 161 } else { |
| 162 // When the View is not attached, we need to manually layout the Vie
w and |
| 163 // invalidate the resource in order to capture a new snapshot. |
| 164 mView.measure(getWidthMeasureSpec(), getHeightMeasureSpec()); |
| 165 mView.layout(0, 0, getMeasuredWidth(), getMeasuredHeight()); |
| 166 invalidateResource(); |
154 } | 167 } |
155 } | 168 } |
156 | 169 |
157 /** | 170 /** |
158 * Destroy the instance. | 171 * Destroy the instance. |
159 */ | 172 */ |
160 public void destroy() { | 173 public void destroy() { |
161 if (mView == null) return; | 174 if (mView == null) return; |
162 | 175 |
163 unregisterResource(); | 176 unregisterResource(); |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
276 mOnDrawListener = null; | 289 mOnDrawListener = null; |
277 } | 290 } |
278 | 291 |
279 assert mView.getParent() != null; | 292 assert mView.getParent() != null; |
280 mContainer.removeView(mView); | 293 mContainer.removeView(mView); |
281 mIsAttached = false; | 294 mIsAttached = false; |
282 } | 295 } |
283 } | 296 } |
284 | 297 |
285 /** | 298 /** |
286 * Layout the View. This is to be used when the View is not attached to the
hierarchy. | 299 * Lay out the view according to the current width and height measure specs. |
287 */ | 300 */ |
288 private void layout() { | 301 private void updateLayoutParams() { |
289 // View must be inflated at this point. | 302 // View must be inflated at this point. |
290 assert mView != null; | 303 assert mView != null; |
291 | 304 |
292 mView.measure(getWidthMeasureSpec(), getHeightMeasureSpec()); | 305 // Update LayoutParams according to the current measure spec. |
293 mView.layout(0, 0, getMeasuredWidth(), getMeasuredHeight()); | 306 final int widthMeasureSpec = getWidthMeasureSpec(); |
| 307 int width = ViewGroup.LayoutParams.WRAP_CONTENT; |
| 308 if (View.MeasureSpec.getMode(widthMeasureSpec) == View.MeasureSpec.EXACT
LY) { |
| 309 width = View.MeasureSpec.getSize(widthMeasureSpec); |
| 310 } |
| 311 |
| 312 final int heightMeasureSpec = getHeightMeasureSpec(); |
| 313 int height = ViewGroup.LayoutParams.WRAP_CONTENT; |
| 314 if (View.MeasureSpec.getMode(heightMeasureSpec) == View.MeasureSpec.EXAC
TLY) { |
| 315 height = View.MeasureSpec.getSize(heightMeasureSpec); |
| 316 } |
| 317 |
| 318 ViewGroup.LayoutParams params = mView.getLayoutParams(); |
| 319 params.width = width; |
| 320 params.height = height; |
| 321 mView.setLayoutParams(params); |
294 } | 322 } |
295 | 323 |
296 /** | 324 /** |
297 * @return An unspecified MeasureSpec value. | 325 * @return An unspecified MeasureSpec value. |
298 */ | 326 */ |
299 private int getUnspecifiedMeasureSpec() { | 327 private int getUnspecifiedMeasureSpec() { |
300 return View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)
; | 328 return View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)
; |
301 } | 329 } |
302 | 330 |
303 /** | 331 /** |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
367 } | 395 } |
368 } | 396 } |
369 | 397 |
370 /** | 398 /** |
371 * Called when the View is drawn, | 399 * Called when the View is drawn, |
372 */ | 400 */ |
373 private void onDraw() { | 401 private void onDraw() { |
374 invalidateResource(); | 402 invalidateResource(); |
375 } | 403 } |
376 } | 404 } |
OLD | NEW |