android.graphics.Bitmap.getAllocationByteCount()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(8.9k)|赞(0)|评价(0)|浏览(116)

本文整理了Java中android.graphics.Bitmap.getAllocationByteCount()方法的一些代码示例,展示了Bitmap.getAllocationByteCount()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Bitmap.getAllocationByteCount()方法的具体详情如下:
包路径:android.graphics.Bitmap
类名称:Bitmap
方法名:getAllocationByteCount

Bitmap.getAllocationByteCount介绍

暂无

代码示例

代码示例来源:origin: bumptech/glide

  1. @Nullable
  2. @TargetApi(Build.VERSION_CODES.KITKAT)
  3. private static String getBitmapString(Bitmap bitmap) {
  4. if (bitmap == null) {
  5. return null;
  6. }
  7. String sizeString = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT
  8. ? " (" + bitmap.getAllocationByteCount() + ")" : "";
  9. return "[" + bitmap.getWidth() + "x" + bitmap.getHeight() + "] " + bitmap.getConfig()
  10. + sizeString;
  11. }

代码示例来源:origin: bumptech/glide

  1. /**
  2. * Returns the in memory size of the given {@link Bitmap} in bytes.
  3. */
  4. @TargetApi(Build.VERSION_CODES.KITKAT)
  5. public static int getBitmapByteSize(@NonNull Bitmap bitmap) {
  6. // The return value of getAllocationByteCount silently changes for recycled bitmaps from the
  7. // internal buffer size to row bytes * height. To avoid random inconsistencies in caches, we
  8. // instead assert here.
  9. if (bitmap.isRecycled()) {
  10. throw new IllegalStateException("Cannot obtain size for recycled Bitmap: " + bitmap
  11. + "[" + bitmap.getWidth() + "x" + bitmap.getHeight() + "] " + bitmap.getConfig());
  12. }
  13. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
  14. // Workaround for KitKat initial release NPE in Bitmap, fixed in MR1. See issue #148.
  15. try {
  16. return bitmap.getAllocationByteCount();
  17. } catch (@SuppressWarnings("PMD.AvoidCatchingNPE") NullPointerException e) {
  18. // Do nothing.
  19. }
  20. }
  21. return bitmap.getHeight() * bitmap.getRowBytes();
  22. }

代码示例来源:origin: commonsguy/cw-omnibus

  1. @TargetApi(Build.VERSION_CODES.KITKAT)
  2. private int byteCount(Bitmap b) {
  3. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
  4. return(b.getAllocationByteCount());
  5. }
  6. return(b.getByteCount());
  7. }
  8. }

代码示例来源:origin: square/assertj-android

  1. @TargetApi(KITKAT)
  2. public BitmapAssert hasAllocationByteCount(int count) {
  3. isNotNull();
  4. int actualCount = actual.getAllocationByteCount();
  5. assertThat(actualCount) //
  6. .overridingErrorMessage("Expected allocation byte count <%s> but was <%s>.", count,
  7. actualCount) //
  8. .isEqualTo(count);
  9. return this;
  10. }

代码示例来源:origin: stackoverflow.com

  1. /**
  2. * returns the bytesize of the give bitmap
  3. */
  4. public static int byteSizeOf(Bitmap bitmap) {
  5. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
  6. return bitmap.getAllocationByteCount();
  7. } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR1) {
  8. return bitmap.getByteCount();
  9. } else {
  10. return bitmap.getRowBytes() * bitmap.getHeight();
  11. }
  12. }

代码示例来源:origin: koral--/android-gif-drawable

  1. /**
  2. * Returns size of the memory needed to store pixels of this object. It counts possible length of all frame buffers.
  3. * Returned value may be lower than amount of actually allocated memory if GIF uses dispose to previous method but frame requiring it
  4. * has never been needed yet. Returned value does not change during runtime.
  5. *
  6. * @return possible size of the memory needed to store pixels of this object
  7. */
  8. public long getAllocationByteCount() {
  9. long byteCount = mNativeInfoHandle.getAllocationByteCount();
  10. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
  11. byteCount += mBuffer.getAllocationByteCount();
  12. } else {
  13. byteCount += getFrameByteCount();
  14. }
  15. return byteCount;
  16. }

代码示例来源:origin: koral--/android-gif-drawable

  1. /**
  2. * Like {@link #getAllocationByteCount()} but includes also backing {@link android.graphics.Bitmap} and takes sample size into account.
  3. *
  4. * @param oldDrawable optional old drawable to be reused, pass {@code null} if there is no one
  5. * @param sampleSize sample size, pass {@code 1} if not using subsampling
  6. * @return possible size of the memory needed to store pixels
  7. * @throws IllegalArgumentException if sample size out of range
  8. */
  9. @Beta
  10. public long getDrawableAllocationByteCount(@Nullable GifDrawable oldDrawable, @IntRange(from = 1, to = Character.MAX_VALUE) int sampleSize) {
  11. if (sampleSize < 1 || sampleSize > Character.MAX_VALUE) {
  12. throw new IllegalStateException("Sample size " + sampleSize + " out of range <1, " + Character.MAX_VALUE + ">");
  13. }
  14. final int sampleSizeFactor = sampleSize * sampleSize;
  15. final long bufferSize;
  16. if (oldDrawable != null && !oldDrawable.mBuffer.isRecycled()) {
  17. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
  18. bufferSize = oldDrawable.mBuffer.getAllocationByteCount();
  19. } else {
  20. bufferSize = oldDrawable.getFrameByteCount();
  21. }
  22. } else {
  23. bufferSize = (mWidth * mHeight * 4) / sampleSizeFactor;
  24. }
  25. return (mPixelsBytesCount / sampleSizeFactor) + bufferSize;
  26. }

代码示例来源:origin: robolectric/robolectric

  1. @Test
  2. @Config(minSdk = KITKAT)
  3. public void getAllocationByteCount() {
  4. Bitmap bitmap = Bitmap.createBitmap(100, 200, Bitmap.Config.ARGB_8888);
  5. assertThat(bitmap.getAllocationByteCount()).isGreaterThan(0);
  6. }

代码示例来源:origin: bumptech/glide

  1. @Test
  2. public void loadWideGamutImage_withArgb888OfSufficientSizeInPool_usesArgb8888Bitmap() {
  3. Bitmap wideGamut = Bitmap.createBitmap(100, 50, Bitmap.Config.RGBA_F16);
  4. byte[] data = asPng(wideGamut);
  5. Bitmap argb8888 = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
  6. Glide.init(context, new GlideBuilder()
  7. .setBitmapPool(new LruBitmapPool(wideGamut.getAllocationByteCount() * 5)));
  8. Glide.get(context).getBitmapPool().put(argb8888);
  9. Bitmap result =
  10. concurrency.get(
  11. Glide.with(context)
  12. .asBitmap()
  13. .load(data)
  14. .submit());
  15. assertThat(result).isSameAs(argb8888);
  16. }

代码示例来源:origin: moagrius/TileView

  1. private static boolean qualifies(Bitmap candidate, BitmapFactory.Options targetOptions) {
  2. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
  3. int width = targetOptions.outWidth / targetOptions.inSampleSize;
  4. int height = targetOptions.outHeight / targetOptions.inSampleSize;
  5. int byteCount = width * height * getBytesPerPixel(candidate.getConfig());
  6. return byteCount <= candidate.getAllocationByteCount();
  7. }
  8. return candidate.getWidth() == targetOptions.outWidth
  9. && candidate.getHeight() == targetOptions.outHeight
  10. && targetOptions.inSampleSize == 1;
  11. }

代码示例来源:origin: guolindev/giffun

  1. /**
  2. * Returns the in memory size of the given {@link Bitmap} in bytes.
  3. */
  4. @TargetApi(Build.VERSION_CODES.KITKAT)
  5. public static int getBitmapByteSize(Bitmap bitmap) {
  6. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
  7. // Workaround for KitKat initial release NPE in Bitmap, fixed in MR1. See issue #148.
  8. try {
  9. return bitmap.getAllocationByteCount();
  10. } catch (NullPointerException e) {
  11. // Do nothing.
  12. }
  13. }
  14. return bitmap.getHeight() * bitmap.getRowBytes();
  15. }

代码示例来源:origin: mozilla-tw/Rocket

  1. @Nullable
  2. @TargetApi(Build.VERSION_CODES.KITKAT)
  3. private static String getBitmapString(Bitmap bitmap) {
  4. if (bitmap == null) {
  5. return null;
  6. }
  7. String sizeString = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT
  8. ? " (" + bitmap.getAllocationByteCount() + ")" : "";
  9. return "[" + bitmap.getWidth() + "x" + bitmap.getHeight() + "] " + bitmap.getConfig()
  10. + sizeString;
  11. }

代码示例来源:origin: yangchong211/YCAudioPlayer

  1. @Override
  2. protected int sizeOf(String key, Bitmap bitmap) {
  3. //API19
  4. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
  5. return bitmap.getAllocationByteCount() / 1024;
  6. } else {
  7. return bitmap.getByteCount() / 1024;
  8. }
  9. }
  10. };

代码示例来源:origin: hidaron/HiFrameAnimation

  1. private static int getBitmapByteCount(Bitmap bitmap) {
  2. if (null == bitmap) return 0;
  3. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
  4. return bitmap.getAllocationByteCount();
  5. }
  6. return bitmap.getByteCount();
  7. }

代码示例来源:origin: com.squareup.assertj/assertj-android

  1. @TargetApi(KITKAT)
  2. public BitmapAssert hasAllocationByteCount(int count) {
  3. isNotNull();
  4. int actualCount = actual.getAllocationByteCount();
  5. assertThat(actualCount) //
  6. .overridingErrorMessage("Expected allocation byte count <%s> but was <%s>.", count,
  7. actualCount) //
  8. .isEqualTo(count);
  9. return this;
  10. }

代码示例来源:origin: jruesga/rview

  1. @TargetApi(Build.VERSION_CODES.KITKAT)
  2. public static int byteSizeOf(Bitmap bitmap) {
  3. if (AndroidHelper.isKitkatOrGreater()) {
  4. return bitmap.getAllocationByteCount();
  5. }
  6. return bitmap.getByteCount();
  7. }
  8. }

代码示例来源:origin: stackoverflow.com

  1. public static int getBitmapByteCount(Bitmap bitmap) {
  2. if (VERSION.SDK_INT < VERSION_CODES.HONEYCOMB_MR1)
  3. return bitmap.getRowBytes() * bitmap.getHeight();
  4. if (VERSION.SDK_INT < VERSION_CODES.KITKAT)
  5. return bitmap.getByteCount();
  6. return bitmap.getAllocationByteCount();
  7. }

代码示例来源:origin: qyxxjd/AndroidBasicProject

  1. /**
  2. * 得到bitmap的大小
  3. */
  4. public static int getBitmapSize(Bitmap bitmap) {
  5. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { //API 19
  6. return bitmap.getAllocationByteCount();
  7. }
  8. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR1) {//API 12
  9. return bitmap.getByteCount();
  10. }
  11. // 在低版本中用一行的字节x高度
  12. return bitmap.getRowBytes() * bitmap.getHeight(); //earlier version
  13. }

代码示例来源:origin: luhaoaimama1/zone-sdk

  1. /**
  2. * 得到bitmap的大小
  3. */
  4. public static int getBitmapSize(Bitmap bitmap) {
  5. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { //API 19
  6. return bitmap.getAllocationByteCount();
  7. }
  8. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR1) {//API 12
  9. return bitmap.getByteCount();
  10. }
  11. // 在低版本中用一行的字节x高度
  12. return bitmap.getRowBytes() * bitmap.getHeight(); //earlier version
  13. }

代码示例来源:origin: stackoverflow.com

  1. int bytes = byteSizeOf(b);
  2. protected int byteSizeOf(Bitmap data) {
  3. if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB_MR1) {
  4. return data.getRowBytes() * data.getHeight();
  5. } else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
  6. return data.getByteCount();
  7. } else {
  8. return data.getAllocationByteCount();
  9. }

相关文章