Chromium Code Reviews| 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 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 113 | 113 |
| 114 // Inflate the View without attaching to hierarchy (attachToRoot param i s false). | 114 // Inflate the View without attaching to hierarchy (attachToRoot param i s false). |
| 115 mView = LayoutInflater.from(mContext).inflate(mLayoutId, mContainer, fal se); | 115 mView = LayoutInflater.from(mContext).inflate(mLayoutId, mContainer, fal se); |
| 116 | 116 |
| 117 // Make sure the View we just inflated is the right one. | 117 // Make sure the View we just inflated is the right one. |
| 118 assert mView.getId() == mViewId; | 118 assert mView.getId() == mViewId; |
| 119 | 119 |
| 120 // Allow subclasses to access/modify the View before it's attached | 120 // Allow subclasses to access/modify the View before it's attached |
| 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 | |
| 124 registerResource(); | |
| 125 } | 123 } |
| 126 | 124 |
| 127 /** | 125 /** |
| 128 * Invalidate the inflated View, causing a snapshot of the View to be captur ed. | 126 * Invalidate the inflated View, causing a snapshot of the View to be captur ed. |
| 129 */ | 127 */ |
| 130 public void invalidate() { | 128 public void invalidate() { |
| 131 // View must be inflated at this point. If it's not, do it now. | 129 invalidate(false); |
| 132 if (mView == null) { | |
| 133 inflate(); | |
| 134 } | |
| 135 | |
| 136 mIsInvalidated = true; | |
| 137 | |
| 138 // If the View is already attached, we don't need to do anything because the | |
| 139 // snapshot will be captured automatically when the View is drawn. | |
| 140 if (!mIsAttached) { | |
| 141 if (shouldAttachView()) { | |
| 142 // TODO(pedrosimonetti): investigate if complex views can be ren dered offline. | |
| 143 // NOTE(pedrosimonetti): it seems that complex views don't get r endered | |
| 144 // properly if not attached to the hierarchy. The problem seem t o be related | |
| 145 // to the use of the property "layout_gravity: end", possibly in combination | |
| 146 // of other things like elastic views (layout_weight: 1) and/or fading edges. | |
| 147 attachView(); | |
| 148 } else { | |
| 149 // When the View is not attached, we need to manually layout the View | |
| 150 // and invalidate the resource in order to capture a new snapsho t. | |
| 151 layout(); | |
| 152 invalidateResource(); | |
| 153 } | |
| 154 } | |
| 155 } | 130 } |
| 156 | 131 |
| 157 /** | 132 /** |
| 133 * Request a layout update, causing the {@link ViewResourceInflater} to be i nvalidated. | |
| 134 */ | |
| 135 public void requestLayout() { | |
| 136 invalidate(true); | |
| 137 } | |
| 138 | |
| 139 /** | |
| 158 * Destroy the instance. | 140 * Destroy the instance. |
| 159 */ | 141 */ |
| 160 public void destroy() { | 142 public void destroy() { |
| 161 if (mView == null) return; | 143 if (mView == null) return; |
| 162 | 144 |
| 163 unregisterResource(); | 145 unregisterResource(); |
| 164 | 146 |
| 165 detachView(); | 147 detachView(); |
| 166 mView = null; | 148 mView = null; |
| 167 | 149 |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 275 mView.getViewTreeObserver().removeOnDrawListener(mOnDrawListener ); | 257 mView.getViewTreeObserver().removeOnDrawListener(mOnDrawListener ); |
| 276 mOnDrawListener = null; | 258 mOnDrawListener = null; |
| 277 } | 259 } |
| 278 | 260 |
| 279 assert mView.getParent() != null; | 261 assert mView.getParent() != null; |
| 280 mContainer.removeView(mView); | 262 mContainer.removeView(mView); |
| 281 mIsAttached = false; | 263 mIsAttached = false; |
| 282 } | 264 } |
| 283 } | 265 } |
| 284 | 266 |
| 267 private void invalidate(boolean requestLayout) { | |
| 268 // View must be inflated at this point. If it's not, do it now. | |
| 269 if (mView == null) { | |
| 270 inflate(); | |
| 271 } | |
| 272 | |
| 273 // Layout the View if necessary, updating its size. | |
| 274 if (requestLayout) { | |
| 275 layout(true); | |
| 276 } | |
| 277 | |
| 278 // Register the resource, if not registered yet. | |
| 279 if (mResourceAdapter == null) { | |
| 280 registerResource(); | |
| 281 } | |
| 282 | |
| 283 mIsInvalidated = true; | |
| 284 | |
| 285 if (!mIsAttached && shouldAttachView()) { | |
| 286 // TODO(pedrosimonetti): investigate if complex views can be rendere d offline. | |
| 287 // NOTE(pedrosimonetti): it seems that complex views don't get rende red | |
| 288 // properly if not attached to the hierarchy. The problem seem to be related | |
| 289 // to the use of the property "layout_gravity: end", possibly in com bination | |
| 290 // of other things like elastic views (layout_weight: 1) and/or fadi ng edges. | |
| 291 attachView(); | |
| 292 } | |
| 293 | |
| 294 if (!mIsAttached) { | |
| 295 // When the View is not attached, we need to manually layout the Vie w and | |
| 296 // invalidate the resource in order to capture a new snapshot. | |
| 297 layout(false); | |
| 298 invalidateResource(); | |
| 299 } | |
| 300 } | |
| 301 | |
| 285 /** | 302 /** |
| 286 * Layout the View. This is to be used when the View is not attached to the hierarchy. | 303 * Lays out the view according to the current width and height measure specs . |
| 287 */ | 304 */ |
| 288 private void layout() { | 305 private void layout(boolean updateLayoutParams) { |
| 289 // View must be inflated at this point. | 306 // View must be inflated at this point. |
| 290 assert mView != null; | 307 assert mView != null; |
| 291 | 308 |
| 309 // Update LayoutParams according to the current measure spec. | |
| 310 if (updateLayoutParams) { | |
| 311 final int widthMeasureSpec = getWidthMeasureSpec(); | |
| 312 int width = ViewGroup.LayoutParams.WRAP_CONTENT; | |
| 313 if (View.MeasureSpec.getMode(widthMeasureSpec) == View.MeasureSpec.E XACTLY) { | |
| 314 width = View.MeasureSpec.getSize(widthMeasureSpec); | |
| 315 } | |
| 316 | |
| 317 final int heightMeasureSpec = getHeightMeasureSpec(); | |
| 318 int height = ViewGroup.LayoutParams.WRAP_CONTENT; | |
| 319 if (View.MeasureSpec.getMode(heightMeasureSpec) == View.MeasureSpec. EXACTLY) { | |
| 320 height = View.MeasureSpec.getSize(heightMeasureSpec); | |
| 321 } | |
| 322 | |
| 323 ViewGroup.LayoutParams params = mView.getLayoutParams(); | |
| 324 params.width = width; | |
| 325 params.height = height; | |
| 326 mView.setLayoutParams(params); | |
| 327 } | |
| 328 | |
| 292 mView.measure(getWidthMeasureSpec(), getHeightMeasureSpec()); | 329 mView.measure(getWidthMeasureSpec(), getHeightMeasureSpec()); |
|
newt (away)
2016/03/24 22:31:36
If you're going to attach this view to the view hi
pedro (no code reviews)
2016/03/25 00:37:47
Made changes based on our offline discussion.
| |
| 293 mView.layout(0, 0, getMeasuredWidth(), getMeasuredHeight()); | 330 mView.layout(0, 0, getMeasuredWidth(), getMeasuredHeight()); |
| 294 } | 331 } |
| 295 | 332 |
| 296 /** | 333 /** |
| 297 * @return An unspecified MeasureSpec value. | 334 * @return An unspecified MeasureSpec value. |
| 298 */ | 335 */ |
| 299 private int getUnspecifiedMeasureSpec() { | 336 private int getUnspecifiedMeasureSpec() { |
| 300 return View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED) ; | 337 return View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED) ; |
| 301 } | 338 } |
| 302 | 339 |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 367 } | 404 } |
| 368 } | 405 } |
| 369 | 406 |
| 370 /** | 407 /** |
| 371 * Called when the View is drawn, | 408 * Called when the View is drawn, |
| 372 */ | 409 */ |
| 373 private void onDraw() { | 410 private void onDraw() { |
| 374 invalidateResource(); | 411 invalidateResource(); |
| 375 } | 412 } |
| 376 } | 413 } |
| OLD | NEW |