Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(136)

Side by Side Diff: chrome/android/java_staging/src/org/chromium/chrome/browser/enhancedbookmarks/EnhancedBookmarkSalientImageView.java

Issue 1141283003: Upstream oodles of Chrome for Android code into Chromium. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: final patch? Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 package org.chromium.chrome.browser.enhancedbookmarks;
6
7 import android.animation.AnimatorListenerAdapter;
8 import android.content.Context;
9 import android.graphics.Bitmap;
10 import android.graphics.BitmapShader;
11 import android.graphics.ColorFilter;
12 import android.graphics.Matrix;
13 import android.graphics.Paint;
14 import android.graphics.PixelFormat;
15 import android.graphics.Rect;
16 import android.graphics.RectF;
17 import android.graphics.Shader;
18 import android.graphics.drawable.Drawable;
19 import android.util.AttributeSet;
20 import android.view.View;
21 import android.widget.FrameLayout;
22 import android.widget.ImageView;
23
24 import org.chromium.chrome.browser.enhanced_bookmarks.EnhancedBookmarksBridge.Sa lientImageCallback;
25 import org.chromium.chrome.browser.enhanced_bookmarks.EnhancedBookmarksModel;
26 import org.chromium.ui.interpolators.BakedBezierInterpolator;
27
28 /**
29 * Container view for image of a bookmark. If there is no appropriate image stor ed in server, we
30 * show pure color as placeholder; otherwise, real image will be loaded asynchro nously with animated
31 * transition.
32 * <p>
33 * Note this image is actually a FrameLayout that contains images. Therefore som e xml attributes for
34 * ImageView are not applicable to this class.
35 */
36 public class EnhancedBookmarkSalientImageView extends FrameLayout {
37
38 /**
39 * Factory for getting drawable for Salient Image View, because in various s cenarios, shapes of
40 * images might vary. This factory should be responsible for providing drawa bles for both color
41 * place holder as well as salient bitmap image.
42 */
43 public interface SalientImageDrawableFactory {
44 /**
45 * @return The drawable containing pure color.
46 */
47 Drawable getSalientDrawable(int color);
48 /**
49 * @return The drawable containing bitmap as background.
50 */
51 Drawable getSalientDrawable(Bitmap bitmap);
52 }
53
54 /**
55 * Base class for different kinds of drawables to be used to define shapes o f salient images.
56 */
57 public abstract static class BaseSalientDrawable extends Drawable {
58 protected RectF mRect = new RectF();
59 protected Paint mPaint = new Paint();
60 private int mBitmapWidth;
61 private int mBitmapHeight;
62 private boolean mShouldScale = true;
63
64 @Override
65 protected void onBoundsChange(Rect bounds) {
66 // Subclasses must override this function. It should first set up co rrect boundary of
67 // mRect and then call super.onBoundsChange().
68 if (mBitmapWidth > 0 && mBitmapHeight > 0) {
69 BitmapShader shader = (BitmapShader) mPaint.getShader();
70
71 float scale = 1.0f;
72 if (mShouldScale) {
73 scale = Math.max((float) bounds.width() / mBitmapWidth,
74 (float) bounds.height() / mBitmapHeight);
75 }
76 float dx = (bounds.width() - mBitmapWidth * scale) * 0.5f + boun ds.left;
77 float dy = (bounds.height() - mBitmapHeight * scale) * 0.5f + bo unds.top;
78
79 Matrix matrix = new Matrix();
80 matrix.setScale(scale, scale);
81 matrix.postTranslate(dx, dy);
82 shader.setLocalMatrix(matrix);
83 }
84 }
85
86 protected BaseSalientDrawable(Bitmap bitmap) {
87 mPaint.setAntiAlias(true);
88 mPaint.setShader(
89 new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileM ode.CLAMP));
90 mBitmapWidth = bitmap.getWidth();
91 mBitmapHeight = bitmap.getHeight();
92 }
93
94 protected BaseSalientDrawable(int color) {
95 mPaint.setAntiAlias(true);
96 mPaint.setColor(color);
97 mBitmapWidth = 0;
98 mBitmapHeight = 0;
99 }
100
101 /**
102 * @param shouldScale True to let bitmap be scaled to match the size of
103 * the drawable. False to let it maintain its origina l size,
104 * regardless of the size of the drawable.
105 */
106 public void shouldScale(boolean shouldScale) {
107 mShouldScale = shouldScale;
108 }
109
110 @Override
111 public int getOpacity() {
112 return PixelFormat.TRANSLUCENT;
113 }
114
115 @Override
116 public void setAlpha(int alpha) {
117 mPaint.setAlpha(alpha);
118 }
119
120 @Override
121 public void setColorFilter(ColorFilter cf) {
122 mPaint.setColorFilter(cf);
123 }
124 }
125
126 private static final int SALIENT_IMAGE_ANIMATION_MS = 300;
127
128 private final ImageView mSolidColorImage;
129 private final ImageView mSalientImage;
130 private SalientImageCallback mSalientImageCallback;
131
132 /**
133 * Create instance of a image view container.
134 */
135 public EnhancedBookmarkSalientImageView(Context context, AttributeSet attrs) {
136 super(context, attrs);
137 mSolidColorImage = new ImageView(context, attrs);
138 mSalientImage = new ImageView(context, attrs);
139 mSalientImage.setVisibility(View.INVISIBLE);
140 addView(mSalientImage);
141 addView(mSolidColorImage);
142 }
143
144 /**
145 * Load the image for a specific URL using a given bookmark model. If no ima ge is found or is
146 * still loading, a place holder with a given color is shown.
147 * @param model Enhanced bookmark model from which to get image.
148 * @param url Url for representing bookmark.
149 * @param color Color of place holder.
150 * @param drawableFactory Factory providing drawables that defines shape of the ImageView.
151 */
152 public void load(EnhancedBookmarksModel model, String url, int color,
153 final SalientImageDrawableFactory drawableFactory) {
154 // Reset conditions.
155 clearAnimation();
156 mSolidColorImage.setImageDrawable(null);
157 mSalientImage.setImageDrawable(null);
158 mSolidColorImage.setVisibility(View.VISIBLE);
159 mSalientImage.setVisibility(View.INVISIBLE);
160
161 mSalientImageCallback = new SalientImageCallback() {
162 @Override
163 public void onSalientImageReady(Bitmap image, String imageUrl) {
164 if (image == null) return;
165 // Since we are reusing this view in GridView, it is possible th at after the view is
166 // reset to represent another bookmark, this method is finally c alled from native
167 // side. Thus check if this callback is for current request.
168 if (this != mSalientImageCallback) return;
169
170 mSalientImage.setVisibility(View.VISIBLE);
171 if (mSolidColorImage.getDrawable() != null) {
172 mSolidColorImage.animate().alpha(0.0f).setDuration(SALIENT_I MAGE_ANIMATION_MS)
173 .setInterpolator(BakedBezierInterpolator.FADE_OUT_CU RVE)
174 .setListener(new AnimatorListenerAdapter() {
175 @Override
176 public void onAnimationEnd(android.animation.Ani mator animation) {
177 mSolidColorImage.setVisibility(View.INVISIBL E);
178 mSolidColorImage.setAlpha(1.0f);
179 }
180 });
181 mSalientImage.setImageDrawable(drawableFactory.getSalientDra wable(image));
182 mSalientImage.setAlpha(0.0f);
183 mSalientImage.animate().alpha(1.0f).setDuration(SALIENT_IMAG E_ANIMATION_MS)
184 .setInterpolator(BakedBezierInterpolator.FADE_IN_CUR VE);
185 } else {
186 mSalientImage.setImageDrawable(drawableFactory.getSalientDra wable(image));
187 mSolidColorImage.setVisibility(View.INVISIBLE);
188 }
189 }
190 };
191
192 boolean isSalientImageLoaded = model.salientImageForUrl(url, mSalientIma geCallback);
193
194 // If there is no image or the image is still loading asynchronously, sh ow pure color.
195 if (!isSalientImageLoaded) {
196 mSolidColorImage.setImageDrawable(drawableFactory.getSalientDrawable (color));
197 }
198 }
199 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698