| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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.ui.resources.statics; | 5 package org.chromium.ui.resources.statics; |
| 6 | 6 |
| 7 import android.content.res.Resources; | 7 import android.content.res.Resources; |
| 8 import android.graphics.Bitmap; | 8 import android.graphics.Bitmap; |
| 9 import android.graphics.BitmapFactory; | 9 import android.graphics.BitmapFactory; |
| 10 import android.graphics.Canvas; | 10 import android.graphics.Canvas; |
| 11 import android.graphics.Rect; | 11 import android.graphics.Rect; |
| 12 import android.graphics.drawable.Drawable; | 12 import android.graphics.drawable.Drawable; |
| 13 | 13 |
| 14 import org.chromium.base.ApiCompatibilityUtils; | 14 import org.chromium.base.ApiCompatibilityUtils; |
| 15 import org.chromium.ui.resources.Resource; | 15 import org.chromium.ui.resources.Resource; |
| 16 import org.chromium.ui.resources.ResourceFactory; |
| 16 | 17 |
| 17 /** | 18 /** |
| 18 * A representation of a static resource and all related information for drawing
it. In general | 19 * A representation of a static resource and all related information for drawing
it. In general |
| 19 * this means a {@link Bitmap} and a potential {@link NinePatchData}. | 20 * this means a {@link Bitmap} and a potential {@link NinePatchData}. |
| 20 */ | 21 */ |
| 21 public class StaticResource implements Resource { | 22 public class StaticResource implements Resource { |
| 22 private final Bitmap mBitmap; | 23 private final Bitmap mBitmap; |
| 23 private final NinePatchData mNinePatchData; | 24 private final NinePatchData mNinePatchData; |
| 24 private final Rect mBitmapSize; | 25 private final Rect mBitmapSize; |
| 25 | 26 |
| 26 /** | 27 /** |
| 27 * Creates a {@link StaticResource} that represents {@code bitmap}. This wi
ll automatically | 28 * Creates a {@link StaticResource} that represents {@code bitmap}. This wi
ll automatically |
| 28 * pull out the {@link NinePatchData} from {@code bitmap} if it exists. | 29 * pull out the {@link NinePatchData} from {@code bitmap} if it exists. |
| 29 * @param bitmap The {@link Bitmap} to build a {@link StaticResource} of. | 30 * @param bitmap The {@link Bitmap} to build a {@link StaticResource} of. |
| 30 */ | 31 */ |
| 31 public StaticResource(Bitmap bitmap) { | 32 public StaticResource(Bitmap bitmap) { |
| 32 mBitmap = bitmap; | 33 mBitmap = bitmap; |
| 33 mNinePatchData = NinePatchData.create(mBitmap); | 34 mNinePatchData = NinePatchData.create(mBitmap); |
| 34 mBitmapSize = new Rect(0, 0, mBitmap.getWidth(), mBitmap.getHeight()); | 35 mBitmapSize = new Rect(0, 0, mBitmap.getWidth(), mBitmap.getHeight()); |
| 35 } | 36 } |
| 36 | 37 |
| 37 @Override | 38 @Override |
| 39 public NinePatchData getNinePatchData() { |
| 40 return mNinePatchData; |
| 41 } |
| 42 |
| 43 @Override |
| 38 public Bitmap getBitmap() { | 44 public Bitmap getBitmap() { |
| 39 return mBitmap; | 45 return mBitmap; |
| 40 } | 46 } |
| 41 | 47 |
| 42 @Override | 48 @Override |
| 43 public Rect getBitmapSize() { | 49 public Rect getBitmapSize() { |
| 44 return mBitmapSize; | 50 return mBitmapSize; |
| 45 } | 51 } |
| 46 | 52 |
| 47 @Override | 53 @Override |
| 48 public Rect getPadding() { | 54 public long createNativeResource() { |
| 49 return mNinePatchData != null ? mNinePatchData.getPadding() : mBitmapSiz
e; | 55 return ResourceFactory.createBitmapResource(mNinePatchData); |
| 50 } | |
| 51 | |
| 52 @Override | |
| 53 public Rect getAperture() { | |
| 54 return mNinePatchData != null ? mNinePatchData.getAperture() : mBitmapSi
ze; | |
| 55 } | 56 } |
| 56 | 57 |
| 57 /** | 58 /** |
| 58 * Attempts to load the Android resource specified by {@code resId} from {@c
ode resources}. | 59 * Attempts to load the Android resource specified by {@code resId} from {@c
ode resources}. |
| 59 * This will attempt to first load the resource as a {@code Bitmap}. If tha
t fails it will try | 60 * This will attempt to first load the resource as a {@code Bitmap}. If tha
t fails it will try |
| 60 * to load the resource as a {@link Drawable}. | 61 * to load the resource as a {@link Drawable}. |
| 61 * @param resources The {@link Resources} instance to load from. | 62 * @param resources The {@link Resources} instance to load from. |
| 62 * @param resId The id of the Android resource to load. | 63 * @param resId The id of the Android resource to load. |
| 63 * @param fitWidth The smallest width the image can be. The image will be
shrunk to scale to | 64 * @param fitWidth The smallest width the image can be. The image will be
shrunk to scale to |
| 64 * try to get close to this value. Or use {@code 0} to use
the intrinsic | 65 * try to get close to this value. Or use {@code 0} to use
the intrinsic |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 124 | 125 |
| 125 if (options.outHeight <= fitHeight && options.outWidth <= fitWidth) retu
rn options; | 126 if (options.outHeight <= fitHeight && options.outWidth <= fitWidth) retu
rn options; |
| 126 | 127 |
| 127 int heightRatio = Math.round((float) options.outHeight / (float) fitHeig
ht); | 128 int heightRatio = Math.round((float) options.outHeight / (float) fitHeig
ht); |
| 128 int widthRatio = Math.round((float) options.outWidth / (float) fitWidth)
; | 129 int widthRatio = Math.round((float) options.outWidth / (float) fitWidth)
; |
| 129 options.inSampleSize = Math.min(heightRatio, widthRatio); | 130 options.inSampleSize = Math.min(heightRatio, widthRatio); |
| 130 | 131 |
| 131 return options; | 132 return options; |
| 132 } | 133 } |
| 133 } | 134 } |
| OLD | NEW |