OLD | NEW |
| (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.content.browser; | |
6 | |
7 import android.content.Context; | |
8 import android.content.res.Resources; | |
9 import android.graphics.Bitmap; | |
10 import android.graphics.BitmapFactory; | |
11 | |
12 import org.chromium.base.CalledByNative; | |
13 import org.chromium.base.JNINamespace; | |
14 import org.chromium.content.R; | |
15 import org.chromium.ui.base.SystemUIResourceType; | |
16 | |
17 /** | |
18 * Helper class for retrieving both system and embedded UI-related resources. | |
19 */ | |
20 @JNINamespace("content") | |
21 class UIResources { | |
22 private static Bitmap getResourceBitmap(Resources res, int resId, int reqWid
th, int reqHeight) { | |
23 final BitmapFactory.Options options = new BitmapFactory.Options(); | |
24 if (reqWidth > 0 && reqHeight > 0) { | |
25 options.inJustDecodeBounds = true; | |
26 BitmapFactory.decodeResource(res, resId, options); | |
27 options.inSampleSize = calculateInSampleSize(options, reqWidth, reqH
eight); | |
28 } | |
29 options.inJustDecodeBounds = false; | |
30 options.inPreferredConfig = Bitmap.Config.ARGB_8888; | |
31 return BitmapFactory.decodeResource(res, resId, options); | |
32 } | |
33 | |
34 // http://developer.android.com/training/displaying-bitmaps/load-bitmap.html | |
35 private static int calculateInSampleSize( | |
36 BitmapFactory.Options options, int reqWidth, int reqHeight) { | |
37 // Raw height and width of image | |
38 final int height = options.outHeight; | |
39 final int width = options.outWidth; | |
40 int inSampleSize = 1; | |
41 | |
42 if (height > reqHeight || width > reqWidth) { | |
43 // Calculate ratios of height and width to requested height and widt
h | |
44 final int heightRatio = Math.round((float) height / (float) reqHeigh
t); | |
45 final int widthRatio = Math.round((float) width / (float) reqWidth); | |
46 | |
47 // Choose the smallest ratio as inSampleSize value, this will guaran
tee | |
48 // a final image with both dimensions larger than or equal to the | |
49 // requested height and width. | |
50 inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio; | |
51 } | |
52 | |
53 return inSampleSize; | |
54 } | |
55 | |
56 @SuppressWarnings("unused") | |
57 @CalledByNative | |
58 private static Bitmap getBitmap( | |
59 Context context, int uiResourceId, int reqWidth, int reqHeight) { | |
60 Resources res = null; | |
61 int resId = 0; | |
62 | |
63 if (uiResourceId == SystemUIResourceType.OVERSCROLL_EDGE) { | |
64 res = Resources.getSystem(); | |
65 resId = res.getIdentifier("android:drawable/overscroll_edge", null,
null); | |
66 } else if (uiResourceId == SystemUIResourceType.OVERSCROLL_GLOW) { | |
67 res = Resources.getSystem(); | |
68 resId = res.getIdentifier("android:drawable/overscroll_glow", null,
null); | |
69 } else if (uiResourceId == SystemUIResourceType.OVERSCROLL_REFRESH_IDLE)
{ | |
70 res = context.getResources(); | |
71 resId = R.drawable.refresh_gray; | |
72 } else if (uiResourceId == SystemUIResourceType.OVERSCROLL_REFRESH_ACTIV
E) { | |
73 res = context.getResources(); | |
74 resId = R.drawable.refresh_blue; | |
75 } else { | |
76 assert false; | |
77 } | |
78 | |
79 if (res == null) return null; | |
80 if (resId == 0) return null; | |
81 | |
82 return getResourceBitmap(res, resId, reqWidth, reqHeight); | |
83 } | |
84 } | |
OLD | NEW |