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

Side by Side Diff: ui/android/java/src/org/chromium/ui/resources/statics/StaticResource.java

Issue 731133002: Upstream ResourceManager (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase Created 6 years 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 2014 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.ui.resources.statics;
6
7 import android.content.res.Resources;
8 import android.graphics.Bitmap;
9 import android.graphics.BitmapFactory;
10 import android.graphics.Canvas;
11 import android.graphics.Rect;
12 import android.graphics.drawable.Drawable;
13
14 import org.chromium.ui.resources.Resource;
15
16 /**
17 * A representation of a static resource and all related information for drawing it. In general
18 * this means a {@link Bitmap} and a potential {@link NinePatchData}.
19 */
20 public class StaticResource implements Resource {
21 private final Bitmap mBitmap;
22 private final NinePatchData mNinePatchData;
23 private final Rect mBitmapSize;
24
25 /**
26 * Creates a {@link StaticResource} that represents {@code bitmap}. This wi ll automatically
27 * pull out the {@link NinePatchData} from {@code bitmap} if it exists.
28 * @param bitmap The {@link Bitmap} to build a {@link StaticResource} of.
29 */
30 public StaticResource(Bitmap bitmap) {
31 mBitmap = bitmap;
32 mNinePatchData = NinePatchData.create(mBitmap);
33 mBitmapSize = new Rect(0, 0, mBitmap.getWidth(), mBitmap.getHeight());
34 }
35
36 @Override
37 public Bitmap getBitmap() {
38 return mBitmap;
39 }
40
41 @Override
42 public Rect getBitmapSize() {
43 return mBitmapSize;
44 }
45
46 @Override
47 public Rect getPadding() {
48 return mNinePatchData != null ? mNinePatchData.getPadding() : mBitmapSiz e;
49 }
50
51 @Override
52 public Rect getAperture() {
53 return mNinePatchData != null ? mNinePatchData.getAperture() : mBitmapSi ze;
54 }
55
56 /**
57 * Attempts to load the Android resource specified by {@code resId} from {@c ode resources}.
58 * This will attempt to first load the resource as a {@code Bitmap}. If tha t fails it will try
59 * to load the resource as a {@link Drawable}.
60 * @param resources The {@link Resources} instance to load from.
61 * @param resId The id of the Android resource to load.
62 * @param fitWidth The smallest width the image can be. The image will be shrunk to scale to
63 * try to get close to this value. Or use {@code 0} to use the intrinsic
64 * size.
65 * @param fitHeight The smallest height the image can be. The image will be shrunk to scale to
66 * try to get close to this value. Or use {@code 0} to use the intrinsic
67 * size.
68 * @return The loaded {@link StaticResource} or {@code null} if the resource could not
69 * be loaded.
70 */
71 public static StaticResource create(Resources resources, int resId, int fitW idth,
72 int fitHeight) {
73 if (resId <= 0) return null;
74 Bitmap bitmap = decodeBitmap(resources, resId, fitWidth, fitHeight);
75 if (bitmap == null) bitmap = decodeDrawable(resources, resId, fitWidth, fitHeight);
76 if (bitmap == null) return null;
77
78 return new StaticResource(bitmap);
79 }
80
81 private static Bitmap decodeBitmap(Resources resources, int resId, int fitWi dth,
82 int fitHeight) {
83 BitmapFactory.Options options = createOptions(resources, resId, fitWidth , fitHeight);
84 options.inPreferredConfig = Bitmap.Config.ARGB_8888;
85 Bitmap bitmap = BitmapFactory.decodeResource(resources, resId, options);
86
87 if (bitmap == null) return null;
88 if (bitmap.getConfig() == options.inPreferredConfig) return bitmap;
89
90 Bitmap convertedBitmap = Bitmap.createBitmap(
91 bitmap.getWidth(), bitmap.getHeight(), options.inPreferredConfig );
92 Canvas canvas = new Canvas(convertedBitmap);
93 canvas.drawBitmap(bitmap, 0, 0, null);
94 bitmap.recycle();
95 return convertedBitmap;
96 }
97
98 private static Bitmap decodeDrawable(Resources resources, int resId, int fit Width,
99 int fitHeight) {
100 try {
101 Drawable drawable = resources.getDrawable(resId);
102 int width = Math.max(drawable.getMinimumWidth(), Math.max(fitWidth, 1));
103 int height = Math.max(drawable.getMinimumHeight(), Math.max(fitHeigh t, 1));
104 Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARG B_8888);
105 Canvas canvas = new Canvas(bitmap);
106 drawable.setBounds(0, 0, width, height);
107 drawable.draw(canvas);
108 return bitmap;
109 } catch (Resources.NotFoundException ex) {
110 return null;
111 }
112 }
113
114 private static BitmapFactory.Options createOptions(Resources resources, int resId,
115 int fitWidth, int fitHeight) {
116 BitmapFactory.Options options = new BitmapFactory.Options();
117 options.inPreferredConfig = Bitmap.Config.ARGB_8888;
118 if (fitWidth == 0 || fitHeight == 0) return options;
119
120 options.inJustDecodeBounds = true;
121 BitmapFactory.decodeResource(resources, resId, options);
122 options.inJustDecodeBounds = false;
123
124 if (options.outHeight <= fitHeight && options.outWidth <= fitWidth) retu rn options;
125
126 int heightRatio = Math.round((float) options.outHeight / (float) fitHeig ht);
127 int widthRatio = Math.round((float) options.outWidth / (float) fitWidth) ;
128 options.inSampleSize = Math.min(heightRatio, widthRatio);
129
130 return options;
131 }
132 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698