本文整理汇总了Java中android.graphics.Bitmap.getAllocationByteCount方法的典型用法代码示例。如果您正苦于以下问题:Java Bitmap.getAllocationByteCount方法的具体用法?Java Bitmap.getAllocationByteCount怎么用?Java Bitmap.getAllocationByteCount使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类android.graphics.Bitmap
的用法示例。
在下文中一共展示了Bitmap.getAllocationByteCount方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getBitmapSize
import android.graphics.Bitmap; //导入方法依赖的package包/类
/**
* Get the size in bytes of a bitmap in a BitmapDrawable. Note that from Android 4.4 (KitKat)
* onward this returns the allocated memory size of the bitmap which can be larger than the
* actual bitmap data byte count (in the case it was re-used).
*
* @param value
* @return size in bytes
*/
@TargetApi(VERSION_CODES.KITKAT)
public static int getBitmapSize(BitmapDrawable value) {
Bitmap bitmap = value.getBitmap();
// From KitKat onward use getAllocationByteCount() as allocated bytes can potentially be
// larger than bitmap byte count.
if (Utils.hasKitKat()) {
return bitmap.getAllocationByteCount();
}
if (Utils.hasHoneycombMR1()) {
return bitmap.getByteCount();
}
// Pre HC-MR1
return bitmap.getRowBytes() * bitmap.getHeight();
}
示例2: getBitmapByteSize
import android.graphics.Bitmap; //导入方法依赖的package包/类
/**
* Returns the in memory size of the given {@link Bitmap} in bytes.
*/
@TargetApi(Build.VERSION_CODES.KITKAT)
public static int getBitmapByteSize(Bitmap bitmap) {
// The return value of getAllocationByteCount silently changes for recycled bitmaps from the
// internal buffer size to row bytes * height. To avoid random inconsistencies in caches, we
// instead assert here.
if (bitmap.isRecycled()) {
throw new IllegalStateException("Cannot obtain size for recycled Bitmap: " + bitmap
+ "[" + bitmap.getWidth() + "x" + bitmap.getHeight() + "] " + bitmap.getConfig());
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
// Workaround for KitKat initial release NPE in Bitmap, fixed in MR1. See issue #148.
try {
return bitmap.getAllocationByteCount();
} catch (NullPointerException e) {
// Do nothing.
}
}
return bitmap.getHeight() * bitmap.getRowBytes();
}
示例3: canUseForInBitmap
import android.graphics.Bitmap; //导入方法依赖的package包/类
/**
* @param candidate - Bitmap to check
* @param targetOptions - Options that have the out* value populated
* @return true if <code>candidate</code> can be used for inBitmap re-use with
* <code>targetOptions</code>
*/
@TargetApi(Build.VERSION_CODES.KITKAT)
private static boolean canUseForInBitmap(
Bitmap candidate, BitmapFactory.Options targetOptions) {
//BEGIN_INCLUDE(can_use_for_inbitmap)
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
// On earlier versions, the dimensions must match exactly and the inSampleSize must be 1
return candidate.getWidth() == targetOptions.outWidth
&& candidate.getHeight() == targetOptions.outHeight
&& targetOptions.inSampleSize == 1;
}
// From Android 4.4 (KitKat) onward we can re-use if the byte size of the new bitmap
// is smaller than the reusable bitmap candidate allocation byte count.
int width = targetOptions.outWidth / targetOptions.inSampleSize;
int height = targetOptions.outHeight / targetOptions.inSampleSize;
int byteCount = width * height * getBytesPerPixel(candidate.getConfig());
return byteCount <= candidate.getAllocationByteCount();
//END_INCLUDE(can_use_for_inbitmap)
}
示例4: getSizeInBytes
import android.graphics.Bitmap; //导入方法依赖的package包/类
/**
* @return size in bytes of the underlying bitmap
*/
@SuppressLint("NewApi")
public static int getSizeInBytes(@Nullable Bitmap bitmap) {
if (bitmap == null) {
return 0;
}
// There's a known issue in KitKat where getAllocationByteCount() can throw an NPE. This was
// apparently fixed in MR1: http://bit.ly/1IvdRpd. So we do a version check here, and
// catch any potential NPEs just to be safe.
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) {
try {
return bitmap.getAllocationByteCount();
} catch (NullPointerException npe) {
// Swallow exception and try fallbacks.
}
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR1) {
return bitmap.getByteCount();
}
// Estimate for earlier platforms. Same code as getByteCount() for Honeycomb.
return bitmap.getRowBytes() * bitmap.getHeight();
}
示例5: canUseForInBitmap
import android.graphics.Bitmap; //导入方法依赖的package包/类
/**
* @param candidate - Bitmap to check
* @param targetOptions - Options that have the out* value populated
* @return true if <code>candidate</code> can be used for inBitmap re-use with
* <code>targetOptions</code>
*/
@TargetApi(VERSION_CODES.KITKAT)
private static boolean canUseForInBitmap(
Bitmap candidate, BitmapFactory.Options targetOptions) {
//BEGIN_INCLUDE(can_use_for_inbitmap)
if (!Utils.hasKitKat()) {
// On earlier versions, the dimensions must match exactly and the inSampleSize must be 1
return candidate.getWidth() == targetOptions.outWidth
&& candidate.getHeight() == targetOptions.outHeight
&& targetOptions.inSampleSize == 1;
}
// From Android 4.4 (KitKat) onward we can re-use if the byte size of the new bitmap
// is smaller than the reusable bitmap candidate allocation byte count.
int width = targetOptions.outWidth / targetOptions.inSampleSize;
int height = targetOptions.outHeight / targetOptions.inSampleSize;
int byteCount = width * height * getBytesPerPixel(candidate.getConfig());
return byteCount <= candidate.getAllocationByteCount();
//END_INCLUDE(can_use_for_inbitmap)
}
示例6: getPixels
import android.graphics.Bitmap; //导入方法依赖的package包/类
private static byte[] getPixels(final Bitmap b) {
final int byteCount = b.getAllocationByteCount();
final ByteBuffer buffer = ByteBuffer.allocate(byteCount);
try {
b.copyPixelsToBuffer(buffer);
} catch (RuntimeException e) {
// Android throws this if there's not enough space in the buffer.
// This should never occur, but if it does, we don't
// really care -- we probably don't need the entire image.
// This is awful. I apologize.
if ("Buffer not large enough for pixels".equals(e.getMessage())) {
return buffer.array();
}
throw e;
}
return buffer.array();
}
示例7: getResizedBitmap
import android.graphics.Bitmap; //导入方法依赖的package包/类
/**
* Gets resized bitmap
* @param pic
* @param maxSize
* @return
*/
public Bitmap getResizedBitmap(Bitmap pic, int maxSize) {
double div = 90.0;
Bitmap image = pic.copy(pic.getConfig(), true);
int size = image.getAllocationByteCount();
int quality;
while (size >= maxSize) {
quality = (int) (div);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
pic.compress(Bitmap.CompressFormat.JPEG, quality, stream);
byte[] byteArray = stream.toByteArray();
image = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
size = byteArray.length;
div = div * 0.9;
}
return image;
}
示例8: getBitmapString
import android.graphics.Bitmap; //导入方法依赖的package包/类
@Nullable
@TargetApi(Build.VERSION_CODES.KITKAT)
private static String getBitmapString(Bitmap bitmap) {
if (bitmap == null) {
return null;
}
String sizeString = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT
? " (" + bitmap.getAllocationByteCount() + ")" : "";
return "[" + bitmap.getWidth() + "x" + bitmap.getHeight() + "] " + bitmap.getConfig()
+ sizeString;
}
示例9: getBitmapBytes
import android.graphics.Bitmap; //导入方法依赖的package包/类
static int getBitmapBytes(Bitmap bitmap) {
int result = SDK_INT >= KITKAT ? bitmap.getAllocationByteCount() : bitmap.getByteCount();
if (result < 0) {
throw new IllegalStateException("Negative size: " + bitmap);
}
return result;
}
示例10: getBitmapByteSize
import android.graphics.Bitmap; //导入方法依赖的package包/类
/**
* Returns the in memory size of the given {@link Bitmap} in bytes.
*/
@TargetApi(Build.VERSION_CODES.KITKAT)
public static int getBitmapByteSize(Bitmap bitmap) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
// Workaround for KitKat initial release NPE in Bitmap, fixed in MR1. See issue #148.
try {
return bitmap.getAllocationByteCount();
} catch (NullPointerException e) {
// Do nothing.
}
}
return bitmap.getHeight() * bitmap.getRowBytes();
}
示例11: getBitmapSize
import android.graphics.Bitmap; //导入方法依赖的package包/类
/**
* 得到bitmap的大小
*/
public static int getBitmapSize(Bitmap bitmap) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { //API 19
return bitmap.getAllocationByteCount();
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR1) {//API 12
return bitmap.getByteCount();
}
// 在低版本中用一行的字节x高度
return bitmap.getRowBytes() * bitmap.getHeight(); //earlier version
}
示例12: getBitmapSize
import android.graphics.Bitmap; //导入方法依赖的package包/类
@TargetApi(Build.VERSION_CODES.KITKAT)
public static int getBitmapSize(BitmapDrawable value) {
Bitmap bitmap = value.getBitmap();
// From KitKat onward use getAllocationByteCount() as allocated bytes can potentially be
// larger than bitmap byte count.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
return bitmap.getAllocationByteCount();
}
return bitmap.getByteCount();
}
示例13: getSizeInBytes
import android.graphics.Bitmap; //导入方法依赖的package包/类
public static long getSizeInBytes(Bitmap bitmap) {
if (bitmap == null)
return 0L;
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) {
try {
return bitmap.getAllocationByteCount();
} catch (NullPointerException e) {
e.printStackTrace();
}
}
return bitmap.getByteCount();
}
示例14: getBitmapSize
import android.graphics.Bitmap; //导入方法依赖的package包/类
public static int getBitmapSize(Bitmap bitmap) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) //API 19
{
return bitmap.getAllocationByteCount();
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR1) //API 12
{
return bitmap.getByteCount();
}
return bitmap.getRowBytes() * bitmap.getHeight(); //earlier version
}
示例15: getSizeInBytes
import android.graphics.Bitmap; //导入方法依赖的package包/类
public static long getSizeInBytes(Bitmap bitmap) {
if (bitmap == null)
return 0L;
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) {
try {
return bitmap.getAllocationByteCount();
} catch (Exception e) {
//ignore...
}
}
return bitmap.getByteCount();
}