OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package org.chromium.chrome.browser.photo_picker; |
| 6 |
| 7 import android.graphics.Bitmap; |
| 8 import android.graphics.Canvas; |
| 9 import android.graphics.Color; |
| 10 import android.graphics.Paint; |
| 11 import android.support.v7.widget.RecyclerView.ViewHolder; |
| 12 import android.text.TextUtils; |
| 13 import android.view.View; |
| 14 |
| 15 import java.util.List; |
| 16 |
| 17 /** |
| 18 * Holds onto a View that displays information about a picker bitmap. |
| 19 */ |
| 20 public class PickerBitmapViewHolder extends ViewHolder { |
| 21 // Our parent category. |
| 22 private PickerCategoryView mCategoryView; |
| 23 |
| 24 // The bitmap view we are holding on to. |
| 25 private final PickerBitmapView mItemView; |
| 26 |
| 27 // The request we are showing the bitmap for. |
| 28 private PickerBitmap mRequest; |
| 29 |
| 30 /** |
| 31 * The PickerBitmapViewHolder. |
| 32 * @param itemView The PickerBitmap view for showing the image. |
| 33 */ |
| 34 public PickerBitmapViewHolder(View itemView) { |
| 35 super(itemView); |
| 36 |
| 37 assert itemView instanceof PickerBitmapView; |
| 38 mItemView = (PickerBitmapView) itemView; |
| 39 } |
| 40 |
| 41 public void imageDecodedCallback(String filePath, Bitmap bitmap) { |
| 42 if (bitmap == null || bitmap.getWidth() == 0 || bitmap.getHeight() == 0)
{ |
| 43 return; |
| 44 } |
| 45 |
| 46 if (mCategoryView.getHighResBitmaps().get(filePath) == null) { |
| 47 mCategoryView.getHighResBitmaps().put(filePath, bitmap); |
| 48 } |
| 49 |
| 50 if (mCategoryView.getLowResBitmaps().get(filePath) == null) { |
| 51 // Scaling the image down takes between 0-1 ms on average (Nexus 6 p
hone debug build). |
| 52 Bitmap lowres = BitmapUtils.scale(bitmap, 40, false); |
| 53 mCategoryView.getLowResBitmaps().put(filePath, lowres); |
| 54 } |
| 55 |
| 56 if (!TextUtils.equals(mRequest.getFilePath(), filePath)) { |
| 57 return; |
| 58 } |
| 59 |
| 60 if (mItemView.setThumbnailBitmap(bitmap)) { |
| 61 mItemView.fadeInThumbnail(); |
| 62 } |
| 63 } |
| 64 |
| 65 /** |
| 66 * Display a single item from |position| in the PickerCategoryView. |
| 67 * @param categoryView The PickerCategoryView to use to fetch the image. |
| 68 * @param position The position of the item to fetch. |
| 69 */ |
| 70 public void displayItem(PickerCategoryView categoryView, int position) { |
| 71 mCategoryView = categoryView; |
| 72 |
| 73 List<PickerBitmap> pickerBitmaps = mCategoryView.getPickerBitmaps(); |
| 74 mRequest = pickerBitmaps.get(position); |
| 75 |
| 76 String filePath = mRequest.getFilePath(); |
| 77 if (mRequest.type() == PickerBitmap.TileTypes.CAMERA |
| 78 || mRequest.type() == PickerBitmap.TileTypes.GALLERY) { |
| 79 mItemView.initialize(mRequest, null, false); |
| 80 mItemView.initializeSpecialTile(); |
| 81 return; |
| 82 } |
| 83 |
| 84 Bitmap original = mCategoryView.getHighResBitmaps().get(filePath); |
| 85 if (original != null) { |
| 86 mItemView.initialize(mRequest, original, false); |
| 87 return; |
| 88 } |
| 89 |
| 90 int size = mCategoryView.getImageSize(); |
| 91 Bitmap placeholder = mCategoryView.getLowResBitmaps().get(filePath); |
| 92 if (placeholder != null) { |
| 93 // Scaling the image up takes between 3-4 ms on average (Nexus 6 pho
ne debug build). |
| 94 placeholder = BitmapUtils.scale(placeholder, size, false); |
| 95 mItemView.initialize(mRequest, placeholder, false); |
| 96 } else { |
| 97 placeholder = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_888
8); |
| 98 placeholder.eraseColor(Color.argb(0, 0, 0, 0)); |
| 99 mItemView.initialize(mRequest, placeholder, true); |
| 100 } |
| 101 |
| 102 // TODO(finnur): Use decoder. |
| 103 imageDecodedCallback(mRequest.getFilePath(), createPlaceholderBitmap(siz
e, size)); |
| 104 } |
| 105 |
| 106 /** |
| 107 * Creates a placeholder bitmap. |
| 108 * @param width The requested width of the resulting bitmap. |
| 109 * @param height The requested height of the resulting bitmap. |
| 110 * @return Placeholder bitmap. |
| 111 */ |
| 112 // TODO(finnur): Remove once the decoder is in place. |
| 113 private Bitmap createPlaceholderBitmap(int width, int height) { |
| 114 Bitmap placeholder = Bitmap.createBitmap(width, height, Bitmap.Config.AR
GB_8888); |
| 115 Canvas canvas = new Canvas(placeholder); |
| 116 Paint paint = new Paint(); |
| 117 paint.setColor(Color.GRAY); |
| 118 canvas.drawRect(0, 0, (float) width, (float) height, paint); |
| 119 return placeholder; |
| 120 } |
| 121 |
| 122 /** |
| 123 * Returns the file path of the current request. |
| 124 */ |
| 125 public String getFilePath() { |
| 126 return mRequest == null ? null : mRequest.getFilePath(); |
| 127 } |
| 128 } |
OLD | NEW |