Android Open Source - android-gskbyte-utils Cached Bitmap Colorizer






From Project

Back to project page android-gskbyte-utils.

License

The source code is released under:

GNU Lesser General Public License

If you think the Android project android-gskbyte-utils listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*******************************************************************************
 * Copyright (c) 2013 Jose Alcal Correa.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v3.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl-3.0.txt
 * /* ww  w. j ava  2 s  . c  o  m*/
 * Contributors:
 *     Jose Alcal Correa - initial API and implementation
 ******************************************************************************/
package org.gskbyte.bitmap;

import org.gskbyte.util.Logger;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;

/**
 * A class that produces colorized version of Bitmaps, but keeps them in
 * a cache in order to improve speed.
 * */
public final class CachedBitmapColorizer extends BitmapColorizer
{

/**
 * Optimizd version of the BitmapCache for fixed bitmap sizes and configurations.
 * */
private final class FixedSizeBitmapCache
extends LRUBitmapCache<Integer> // int color value used as key
{
    private final int fixedBitmapByteCount;
    
    public FixedSizeBitmapCache(int maxSize, int fixedBitmapByteCount)
    { super(maxSize); this.fixedBitmapByteCount = fixedBitmapByteCount;}
    
    protected int sizeOf(Integer key, Bitmap value)
    { return fixedBitmapByteCount; }
    
    protected void entryRemoved(boolean evicted, Integer key, Bitmap oldValue, Bitmap newValue)
    { Logger.info(getClass(), "Entry removed from cache, "+size()/fixedBitmapByteCount+" elements use " + cache.size()/1024 +"/" + getMaxSize()/1024 + "KB used"); }
}

private final FixedSizeBitmapCache cache;
private final int backgroundColor;

/**
 * Constructor.
 * @param bitmap The source bitmap to which to apply color.
 * @param outputConfig The bitmapConfig to use for the resulting bitmaps.
 * @param backgroundColor The background color for all generated bitmaps.
 * @param copiesInCache The number of copies to keep in cache
 * */

public CachedBitmapColorizer(Bitmap bitmap, Bitmap.Config outputConfig, int backgroundColor, int copiesInCache)
{
    super(bitmap, outputConfig);
    this.backgroundColor = backgroundColor;
    int bitmapMemorySize = LRUBitmapCache.BitmapMemorySize(bitmap.getWidth(), bitmap.getHeight(), outputConfig);
    cache = new FixedSizeBitmapCache(copiesInCache*bitmapMemorySize, bitmapMemorySize);
}

/**
 * Constructor to load a bitmap from resources
 * @param Context the context from which to load the bitmap resource
 * @param drawableResource The drawable id to load the bitmap
 * @param outputConfig The bitmapConfig to use for the resulting bitmaps.
 * @param backgroundColor The background color for all generated bitmaps.
 * @param copiesInCache The number of copies to keep in cache
 * */
public CachedBitmapColorizer(Context context, int drawableResource, Bitmap.Config outputConfig, int backgroundColor, int copiesInCache)
{
    this(BitmapFactory.decodeResource(context.getResources(), drawableResource), outputConfig, backgroundColor, copiesInCache);
}


/**
 * Applies the color given with the integer to a copy of the source bitmap,
 * which is returned. This bitmap has a transparent background (if the current
 * bitmapConfig supports it).
 * If the entry already exists in the cache, returns it.
 * @param color The color to apply to the bitmap.
 * @return A colorized copy of the source bitmap.
 * */
public Bitmap colorize(int color)
{
    Bitmap bitmap = cache.get(color);
    if(bitmap == null || bitmap.isRecycled()) {
        bitmap = super.colorize(color, backgroundColor);
        cache.put(color, bitmap);
    }
    
    return bitmap;
}

/**
 * Applies the color given with the integer to a copy of the source bitmap,
 * which is returned. This bitmap has a transparent background (if the current
 * bitmapConfig supports it).
 * If the entry already exists in the cache, returns it.
 * @param a alpha component of the color, in the range [0,255]
 * @param r red component of the color, in the range [0,255]
 * @param g green component of the color, in the range [0,255]
 * @param b blue component of the color, in the range [0,255]
 * @return A colorized copy of the source bitmap.
 * */
public Bitmap colorize(int a, int r, int g, int b)
{
    final int color = Color.argb(a, r, g, b);
    return colorize(color);
}
}




Java Source Code List

com.woozzu.android.widget.IndexScroller.java
com.woozzu.android.widget.IndexableListView.java
org.gskbyte.FragmentWrapperActivity.java
org.gskbyte.animation.ExpandAnimation.java
org.gskbyte.bitmap.AbstractBitmapManager.java
org.gskbyte.bitmap.BitmapColorizer.java
org.gskbyte.bitmap.BitmapManager.java
org.gskbyte.bitmap.CachedBitmapColorizer.java
org.gskbyte.bitmap.IndexedBitmaps.java
org.gskbyte.bitmap.LRUBitmapCache.java
org.gskbyte.bitmap.LRUBitmapManager.java
org.gskbyte.bitmap.PrivateBitmapManager.java
org.gskbyte.bitmap.ReferencedBitmaps.java
org.gskbyte.collection.ArrayHashMap.java
org.gskbyte.collection.DoubleSparseArray.java
org.gskbyte.collection.ListHashMap.java
org.gskbyte.dialog.DownloadDialogFragment.java
org.gskbyte.dialog.LoadDialogFragment.java
org.gskbyte.dialog.OpenLinkDialogBuilder.java
org.gskbyte.dialog.PickerDialogFragment.java
org.gskbyte.download.DiskDownload.java
org.gskbyte.download.DownloadManager.java
org.gskbyte.download.Download.java
org.gskbyte.download.MemoryDownload.java
org.gskbyte.drawable.AutoBackgroundButtonDrawable.java
org.gskbyte.listener.IListenable.java
org.gskbyte.listener.ListenableNG.java
org.gskbyte.listener.Listenable.java
org.gskbyte.preferences.DialogSeekBarPreference.java
org.gskbyte.preferences.InlineSeekBarPreference.java
org.gskbyte.remote.AsyncURLRequest.java
org.gskbyte.remote.URLRequest.java
org.gskbyte.tasks.QueuedTaskExecutor.java
org.gskbyte.tasks.TaskStep.java
org.gskbyte.tasks.Task.java
org.gskbyte.ui.ArrayAdapterWithDefaultValue.java
org.gskbyte.ui.ListAdapter.java
org.gskbyte.ui.ColorDialog.ColorDialog.java
org.gskbyte.ui.ColorDialog.ColorPreference.java
org.gskbyte.ui.iconifiedMainMenuList.EntryView.java
org.gskbyte.ui.iconifiedMainMenuList.MainMenuAdapter.java
org.gskbyte.ui.iconifiedMainMenuList.MenuEntry.java
org.gskbyte.util.FrequentIntents.java
org.gskbyte.util.IOUtils.java
org.gskbyte.util.Logger.java
org.gskbyte.util.OpenFileHandlerFactory.java
org.gskbyte.util.OpenFileHandler.java
org.gskbyte.util.XmlUtils.java
org.gskbyte.view.AsyncImageView.java
org.gskbyte.view.AutoBackgroundButton.java
org.gskbyte.view.AutoBackgroundImageButton.java
org.gskbyte.view.AutoHeightImageView.java
org.gskbyte.view.ExpandedGridView.java
org.gskbyte.view.ExpandedListView.java
org.gskbyte.view.FontUtil.java
org.gskbyte.view.FontableButton.java
org.gskbyte.view.FontableCheckBox.java
org.gskbyte.view.FontableEditText.java
org.gskbyte.view.FontableTextView.java
org.gskbyte.view.FullWidthImageView.java
org.gskbyte.view.ProportionalHeightLayout.java
org.gskbyte.view.PullToRefreshListView.java
org.gskbyte.view.SquaredLayout.java
org.gskbyte.view.StepSeekBar.java
org.gskbyte.view.TextViewUtil.java
org.gskbyte.view.ViewUtils.java