Example usage for android.graphics Bitmap getRowBytes

List of usage examples for android.graphics Bitmap getRowBytes

Introduction

In this page you can find the example usage for android.graphics Bitmap getRowBytes.

Prototype

public final int getRowBytes() 

Source Link

Document

Return the number of bytes between rows in the bitmap's pixels.

Usage

From source file:com.cmput301w17t07.moody.CreateMoodActivity.java

/**
 * Compression of image. From: http://blog.csdn.net/harryweasley/article/details/51955467 <br>
 * author: HarryWeasley 2016-07-20 15:26 <br>
 * taken by Xin Huang 2017-03-04 18:45 <br>
 * for compressing the image to meet the project storage requirements <br>
 * @param image <br>/*ww w  . j  a v  a 2s .c  o  m*/
 * @return Bitmap image <br>
 */
public Bitmap compress(Bitmap image) {
    try {
        while (((image.getRowBytes() * image.getHeight()) / 8) > 65536) {
            BitmapFactory.Options options2 = new BitmapFactory.Options();
            options2.inPreferredConfig = Bitmap.Config.RGB_565;

            Matrix matrix = new Matrix();
            matrix.setScale(0.5f, 0.5f);
            image = Bitmap.createBitmap(image, 0, 0, image.getWidth(), image.getHeight(), matrix, true);
        }
    } catch (Exception E) {

    }
    return image;
}

From source file:uk.org.ngo.squeezer.util.ImageCache.java

/**
 * Initialize the cache, providing all parameters.
 *
 * @param cacheParams The cache parameters to initialize the cache
 *//*from   ww w .  j a  v a 2  s  . c om*/
private void init(ImageCacheParams cacheParams) {
    mCacheParams = cacheParams;

    // Set up memory cache
    if (mCacheParams.memoryCacheEnabled) {
        if (BuildConfig.DEBUG) {
            Log.d(TAG, "Memory cache created (size = " + mCacheParams.memCacheSize + ")");
        }
        mMemoryCache = new LruCache<String, Bitmap>(mCacheParams.memCacheSize) {
            @Override
            protected int sizeOf(String key, Bitmap bitmap) {
                return (bitmap.getRowBytes() * bitmap.getHeight());
            }
        };
    }

    // By default the disk cache is not initialized here as it should be initialized
    // on a separate thread due to disk access.
    if (cacheParams.initDiskCacheOnCreate) {
        // Set up disk cache
        initDiskCache();
    }
}

From source file:github.daneren2005.dsub.util.ImageLoader.java

public ImageLoader(Context context) {
    this.context = context;
    handler = new Handler(Looper.getMainLooper());
    final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
    final int cacheSize = maxMemory / 4;

    // Determine the density-dependent image sizes.
    imageSizeDefault = context.getResources().getDrawable(R.drawable.unknown_album).getIntrinsicHeight();
    DisplayMetrics metrics = context.getResources().getDisplayMetrics();
    imageSizeLarge = Math.round(Math.min(metrics.widthPixels, metrics.heightPixels));
    avatarSizeDefault = context.getResources().getDrawable(R.drawable.ic_social_person).getIntrinsicHeight();

    cache = new LruCache<String, Bitmap>(cacheSize) {
        @Override/*  www . j a  v a2 s.c  om*/
        protected int sizeOf(String key, Bitmap bitmap) {
            return bitmap.getRowBytes() * bitmap.getHeight() / 1024;
        }

        @Override
        protected void entryRemoved(boolean evicted, String key, Bitmap oldBitmap, Bitmap newBitmap) {
            if (evicted) {
                if (oldBitmap != nowPlaying || clearingCache) {
                    oldBitmap.recycle();
                } else {
                    cache.put(key, oldBitmap);
                }
            }
        }
    };
}

From source file:github.madmarty.madsonic.util.ImageLoader.java

public ImageLoader(Context context) {

    this.context = context;

    final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
    final int cacheSize = maxMemory / 4;

    cache = new LruCache<String, Bitmap>(cacheSize) {
        @Override/*from   w w w. ja  v  a  2  s.c o  m*/
        protected int sizeOf(String key, Bitmap bitmap) {
            return bitmap.getRowBytes() * bitmap.getHeight() / 1024;
        }

        @Override
        protected void entryRemoved(boolean evicted, String key, Bitmap oldBitmap, Bitmap newBitmap) {
            if (evicted) {
                try {
                    oldBitmap.recycle();
                } catch (Exception e) {
                    // Do nothing, just means that the drawable is a flat image
                }
            }
        }
    };

    queue = new LinkedBlockingQueue<Task>(1000);

    // Determine the density-dependent image sizes.
    imageSizeDefault = (int) Math
            .round((context.getResources().getDrawable(R.drawable.unknown_album).getIntrinsicHeight()));
    LOG.info("imageSizeDefault: " + imageSizeDefault);

    imageSizeMedium = 180; // (int) Math.round((context.getResources().getDrawable(R.drawable.unknown_album_medium).getIntrinsicHeight()));;
    LOG.info("imageSizeMedium: " + imageSizeMedium);

    DisplayMetrics metrics = context.getResources().getDisplayMetrics();
    imageSizeLarge = (int) Math.round(Math.min(metrics.widthPixels, metrics.heightPixels) * 0.6);
    LOG.info("imageSizeLarge: " + imageSizeLarge);

    //        imageSizeDefault = Util.getCoverSize(context);

    for (int i = 0; i < CONCURRENCY; i++) {
        new Thread(this, "ImageLoader").start();
    }

    createLargeUnknownImage(context);
}

From source file:com.example.xyzreader.cp7.ArticleListFragment.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Get max available VM memory, exceeding this amount will throw an
    // OutOfMemory exception. Stored in kilobytes as LruCache takes an
    // int in its constructor.
    final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);

    // Use 1/8th of the available memory for this memory cache.
    final int cacheSize = maxMemory / 8;

    mMemoryCache = new LruCache<String, Bitmap>(cacheSize) {
        @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
        @Override//w  w  w  .  jav  a 2s .c  o  m
        protected int sizeOf(String key, Bitmap bitmap) {
            // The cache size will be measured in kilobytes rather than
            // number of items.
            if (Build.VERSION.SDK_INT > Build.VERSION_CODES.HONEYCOMB_MR1) {
                return bitmap.getByteCount() / 1024;
            } else {
                return bitmap.getRowBytes() * bitmap.getHeight() / 1024;
            }
        }
    };

    setListAdapter(new MyAdapter());
    setHasOptionsMenu(true);
}

From source file:github.popeen.dsub.util.ImageLoader.java

public ImageLoader(Context context) {
    this.context = context;
    handler = new Handler(Looper.getMainLooper());
    final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
    cacheSize = maxMemory / 4;//from w w  w  .java 2 s  . c o  m

    // Determine the density-dependent image sizes.
    imageSizeDefault = context.getResources().getDrawable(R.drawable.unknown_album).getIntrinsicHeight();
    DisplayMetrics metrics = context.getResources().getDisplayMetrics();
    imageSizeLarge = Math.round(Math.min(metrics.widthPixels, metrics.heightPixels));
    avatarSizeDefault = context.getResources().getDrawable(R.drawable.ic_social_person).getIntrinsicHeight();

    cache = new LruCache<String, Bitmap>(cacheSize) {
        @Override
        protected int sizeOf(String key, Bitmap bitmap) {
            return bitmap.getRowBytes() * bitmap.getHeight() / 1024;
        }

        @Override
        protected void entryRemoved(boolean evicted, String key, Bitmap oldBitmap, Bitmap newBitmap) {
            if (evicted) {
                if ((oldBitmap != nowPlaying && oldBitmap != nowPlayingSmall) || clearingCache) {
                    oldBitmap.recycle();
                } else if (oldBitmap != newBitmap) {
                    cache.put(key, oldBitmap);
                }
            }
        }
    };
}

From source file:com.example.xyzreader.cp8.ArticleListFragment.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Get max available VM memory, exceeding this amount will throw an
    // OutOfMemory exception. Stored in kilobytes as LruCache takes an
    // int in its constructor.
    final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);

    // Use 1/8th of the available memory for this memory cache.
    final int cacheSize = maxMemory / 8;

    mMemoryCache = new LruCache<String, Bitmap>(cacheSize) {
        @TargetApi(12)/*from  w  w w .j a  va2 s  . c  o  m*/
        @Override
        protected int sizeOf(String key, Bitmap bitmap) {
            // The cache size will be measured in kilobytes rather than
            // number of items.
            if (Build.VERSION.SDK_INT > Build.VERSION_CODES.HONEYCOMB_MR1) {
                return bitmap.getByteCount() / 1024;
            } else {
                return bitmap.getRowBytes() * bitmap.getHeight() / 1024;
            }
        }
    };

    setListAdapter(new MyAdapter());
    setHasOptionsMenu(true);
}

From source file:com.cmput301w17t07.moody.EditMoodActivity.java

/**
 * Compression of image. From: http://blog.csdn.net/harryweasley/article/details/51955467 <br>
 * author: HarryWeasley 2016-07-20 15:26 <br>
 * taken by Xin Huang 2017-03-04 18:45 <br>
 * for compressing the image to meet the project storage requirements <br>
 * @param image  // the image to be compressed <br>
 * @return Bitmap  // the compressed image <br>
 *///  ww w .  j  ava 2s .c o m
public Bitmap compress(Bitmap image) {
    try {
        // Compression of image. From: http://blog.csdn.net/harryweasley/article/details/51955467
        // for compressing the image to meet the project storage requirements
        while (((image.getRowBytes() * image.getHeight()) / 8) > 65536) {
            BitmapFactory.Options options2 = new BitmapFactory.Options();
            options2.inPreferredConfig = Bitmap.Config.RGB_565;
            Matrix matrix = new Matrix();
            matrix.setScale(0.5f, 0.5f);
            image = Bitmap.createBitmap(image, 0, 0, image.getWidth(), image.getHeight(), matrix, true);
        }
    } catch (Exception E) {
        E.printStackTrace();
    }
    return image;
}

From source file:com.tangjd.displayingbitmaps.util.ImageCache.java

/**
 * @param candidate - Bitmap to check/*from  ww  w .j ava2s  .  c o  m*/
 * @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>
 */
private static boolean canUseForInBitmap(Bitmap candidate, BitmapFactory.Options targetOptions) {

    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());

    int allocationByteCount;
    if (Utils.hasKitKat()) {
        allocationByteCount = candidate.getAllocationByteCount();
    } else if (Utils.hasHoneycombMR1()) {
        allocationByteCount = candidate.getByteCount();
    } else {
        allocationByteCount = candidate.getRowBytes() * candidate.getHeight();
    }

    return byteCount <= allocationByteCount;
}

From source file:fr.forexperts.ui.ArticleListFragment.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Get max available VM memory, exceeding this amount will throw an
    // OutOfMemory exception. Stored in kilobytes as LruCache takes an
    // int in its constructor.
    final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);

    // Use 1/8th of the available memory for this memory cache.
    final int cacheSize = maxMemory / 8;

    mMemoryCache = new LruCache<String, Bitmap>(cacheSize) {
        @TargetApi(12)/*w  w w . j a  v  a 2 s . c om*/
        @Override
        protected int sizeOf(String key, Bitmap bitmap) {
            // The cache size will be measured in kilobytes rather than
            // number of items.
            if (Build.VERSION.SDK_INT > Build.VERSION_CODES.HONEYCOMB_MR1) {
                return bitmap.getByteCount() / 1024;
            } else {
                return bitmap.getRowBytes() * bitmap.getHeight() / 1024;
            }
        }
    };

    setHasOptionsMenu(true);
}