OLD | NEW |
---|---|
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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.chrome.browser.photo_picker; | 5 package org.chromium.chrome.browser.photo_picker; |
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.Paint; | 10 import android.graphics.Paint; |
(...skipping 27 matching lines...) Expand all Loading... | |
38 /** | 38 /** |
39 * The notification handler for when an image has been decoded. | 39 * The notification handler for when an image has been decoded. |
40 * @param filePath The file path for the newly decoded image. | 40 * @param filePath The file path for the newly decoded image. |
41 * @param bitmap The results of the decoding (or placeholder image, if faile d). | 41 * @param bitmap The results of the decoding (or placeholder image, if faile d). |
42 */ | 42 */ |
43 public void imageDecodedCallback(String filePath, Bitmap bitmap) { | 43 public void imageDecodedCallback(String filePath, Bitmap bitmap) { |
44 if (bitmap == null || bitmap.getWidth() == 0 || bitmap.getHeight() == 0) { | 44 if (bitmap == null || bitmap.getWidth() == 0 || bitmap.getHeight() == 0) { |
45 return; | 45 return; |
46 } | 46 } |
47 | 47 |
48 if (mCategoryView.getHighResBitmaps().get(filePath) == null) { | |
49 mCategoryView.getHighResBitmaps().put(filePath, bitmap); | |
Michael van Ouwerkerk
2017/04/27 17:07:34
The documentation for this param says it might be
Finnur
2017/05/04 16:05:15
That changed with the latest sync. Decoding will n
| |
50 } | |
51 | |
52 if (mCategoryView.getLowResBitmaps().get(filePath) == null) { | |
53 // Scaling the image down takes between 0-1 ms on average (Nexus 6 p hone debug build). | |
54 Bitmap lowres = BitmapUtils.scale(bitmap, 40, false); | |
Michael van Ouwerkerk
2017/04/27 17:07:34
What's 40? Px? Dp? Maybe extract a documented cons
Finnur
2017/05/04 16:05:15
Done.
| |
55 mCategoryView.getLowResBitmaps().put(filePath, lowres); | |
56 } | |
57 | |
48 if (!TextUtils.equals(mBitmapDetails.getFilePath(), filePath)) { | 58 if (!TextUtils.equals(mBitmapDetails.getFilePath(), filePath)) { |
49 return; | 59 return; |
50 } | 60 } |
51 | 61 |
52 mItemView.setThumbnailBitmap(bitmap); | 62 mItemView.setThumbnailBitmap(bitmap); |
53 } | 63 } |
54 | 64 |
55 /** | 65 /** |
56 * Display a single item from |position| in the PickerCategoryView. | 66 * Display a single item from |position| in the PickerCategoryView. |
57 * @param categoryView The PickerCategoryView to use to fetch the image. | 67 * @param categoryView The PickerCategoryView to use to fetch the image. |
58 * @param position The position of the item to fetch. | 68 * @param position The position of the item to fetch. |
59 */ | 69 */ |
60 public void displayItem(PickerCategoryView categoryView, int position) { | 70 public void displayItem(PickerCategoryView categoryView, int position) { |
61 mCategoryView = categoryView; | 71 mCategoryView = categoryView; |
62 | 72 |
63 List<PickerBitmap> pickerBitmaps = mCategoryView.getPickerBitmaps(); | 73 List<PickerBitmap> pickerBitmaps = mCategoryView.getPickerBitmaps(); |
64 mBitmapDetails = pickerBitmaps.get(position); | 74 mBitmapDetails = pickerBitmaps.get(position); |
65 | 75 |
66 if (mBitmapDetails.type() == PickerBitmap.CAMERA | 76 if (mBitmapDetails.type() == PickerBitmap.CAMERA |
67 || mBitmapDetails.type() == PickerBitmap.GALLERY) { | 77 || mBitmapDetails.type() == PickerBitmap.GALLERY) { |
68 mItemView.initializeSpecialTile(mBitmapDetails); | 78 mItemView.initializeSpecialTile(mBitmapDetails); |
69 return; | 79 return; |
70 } | 80 } |
71 | 81 |
72 // TODO(finnur): Use cached image, if available. | 82 String filePath = mBitmapDetails.getFilePath(); |
83 Bitmap original = mCategoryView.getHighResBitmaps().get(filePath); | |
84 if (original != null) { | |
85 mItemView.initialize(mBitmapDetails, original, false); | |
86 return; | |
87 } | |
88 | |
89 int size = mCategoryView.getImageSize(); | |
90 Bitmap placeholder = mCategoryView.getLowResBitmaps().get(filePath); | |
91 if (placeholder != null) { | |
92 // Scaling the image up takes between 3-4 ms on average (Nexus 6 pho ne debug build). | |
93 placeholder = BitmapUtils.scale(placeholder, size, false); | |
94 mItemView.initialize(mBitmapDetails, placeholder, true); | |
95 } else { | |
96 mItemView.initialize(mBitmapDetails, null, true); | |
97 } | |
73 | 98 |
74 // TODO(finnur): Use decoder instead. | 99 // TODO(finnur): Use decoder instead. |
75 int size = mCategoryView.getImageSize(); | |
76 imageDecodedCallback(mBitmapDetails.getFilePath(), createPlaceholderBitm ap(size, size)); | 100 imageDecodedCallback(mBitmapDetails.getFilePath(), createPlaceholderBitm ap(size, size)); |
77 } | 101 } |
78 | 102 |
79 /** | 103 /** |
80 * Creates a placeholder bitmap. | 104 * Creates a placeholder bitmap. |
81 * @param width The requested width of the resulting bitmap. | 105 * @param width The requested width of the resulting bitmap. |
82 * @param height The requested height of the resulting bitmap. | 106 * @param height The requested height of the resulting bitmap. |
83 * @return Placeholder bitmap. | 107 * @return Placeholder bitmap. |
84 */ | 108 */ |
85 // TODO(finnur): Remove once the decoder is in place. | 109 // TODO(finnur): Remove once the decoder is in place. |
86 private Bitmap createPlaceholderBitmap(int width, int height) { | 110 private Bitmap createPlaceholderBitmap(int width, int height) { |
87 Bitmap placeholder = Bitmap.createBitmap(width, height, Bitmap.Config.AR GB_8888); | 111 Bitmap placeholder = Bitmap.createBitmap(width, height, Bitmap.Config.AR GB_8888); |
88 Canvas canvas = new Canvas(placeholder); | 112 Canvas canvas = new Canvas(placeholder); |
89 Paint paint = new Paint(); | 113 Paint paint = new Paint(); |
90 paint.setColor(Color.GRAY); | 114 paint.setColor(Color.GRAY); |
91 canvas.drawRect(0, 0, (float) width, (float) height, paint); | 115 canvas.drawRect(0, 0, (float) width, (float) height, paint); |
92 return placeholder; | 116 return placeholder; |
93 } | 117 } |
94 | 118 |
95 /** | 119 /** |
96 * Returns the file path of the current request. | 120 * Returns the file path of the current request. |
97 */ | 121 */ |
98 public String getFilePath() { | 122 public String getFilePath() { |
99 return mBitmapDetails == null ? null : mBitmapDetails.getFilePath(); | 123 return mBitmapDetails == null ? null : mBitmapDetails.getFilePath(); |
100 } | 124 } |
101 } | 125 } |
OLD | NEW |