Skip to content

Commit

Permalink
implemented background image caching
Browse files Browse the repository at this point in the history
  • Loading branch information
oleg-vasiliev committed Mar 27, 2017
1 parent bf3631e commit 9dcacb3
Show file tree
Hide file tree
Showing 21 changed files with 340 additions and 78 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
package com.ramotion.expandingcollection.examples.simple;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Point;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.StrictMode;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.util.Log;
import android.util.TypedValue;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
Expand All @@ -29,6 +35,7 @@
// Simple example of usage expanding collection library
public class MainActivity extends FragmentActivity {

private final String TAG = "MainEx";
private ECPagerView ecPagerView;

@Override
Expand All @@ -43,6 +50,7 @@ protected void onCreate(Bundle savedInstanceState) {
display.getSize(displaySize);
setContentView(R.layout.activity_main);

Log.e(TAG, "Display sizes is " + displaySize.x + " to " + displaySize.y);
// Prepare example dataset with cute penguins images
List<ECCardData> exampleDataSet = getExampleDataSet(displaySize.x, displaySize.y);

Expand Down Expand Up @@ -98,8 +106,8 @@ public void onClick(final View v) {

// Tune pager view
ecPagerView
.withCardSize(500, 550)
.withCardExpandedHeaderHeight(450)
.withCardSize((int) (displaySize.x * 0.7), (int) (displaySize.y * 0.45))
.withCardExpandedHeaderHeight((int) (displaySize.y * 0.35))
.withBackgroundImageSwitcher(bgView)
.withPagerViewAdapter(adapter);
}
Expand All @@ -114,47 +122,52 @@ private List<ECCardData> getExampleDataSet(int imageWidth, int imageHeight) {
List<ECCardData> dataset = new ArrayList<>();

PagerCardDataPOJO item5 = new PagerCardDataPOJO();
item5.setBgImageDrawable(placePenguin(imageWidth, imageHeight));
item5.setHeadTitle("FiftH");
item5.setMainBgImageDrawableResource(R.drawable.attractions);
item5.setHeadBgImageDrawableResource(R.drawable.attractions_head);
item5.setHeadTitle("Attractions");
item5.setPersonMessage("Vae, nix!");
item5.setPersonName("Frederich Nilson");
item5.setPersonPicture(placePenguin(100, 100));
item5.setPersonPicture(getResources().getDrawable(R.drawable.m5));
item5.setComments(prepareCommentsArray());
dataset.add(item5);

PagerCardDataPOJO item4 = new PagerCardDataPOJO();
item4.setBgImageDrawable(placePenguin(imageWidth, imageHeight));
item4.setHeadTitle("FOurTH");
item4.setMainBgImageDrawableResource(R.drawable.cityscape);
item4.setHeadBgImageDrawableResource(R.drawable.cityscape_head);
item4.setHeadTitle("city Scape");
item4.setPersonMessage("Celery can be covered with heated peanut butter.");
item4.setPersonName("Peter Jackson");
item4.setPersonPicture(placePenguin(100, 100));
item4.setPersonPicture(getResources().getDrawable(R.drawable.f4));
item4.setComments(prepareCommentsArray());
dataset.add(item4);

PagerCardDataPOJO item3 = new PagerCardDataPOJO();
item3.setBgImageDrawable(placePenguin(imageWidth, imageHeight));
item3.setHeadTitle("AWESOMENESS");
item3.setMainBgImageDrawableResource(R.drawable.cuisine);
item3.setHeadBgImageDrawableResource(R.drawable.cuisine_head);
item3.setHeadTitle("Cuisine");
item3.setPersonMessage("Nanomachine of a clear advice, deserve the sonic shower!");
item3.setPersonName("Fedor Bondarchuk");
item3.setPersonPicture(placePenguin(100, 100));
item3.setPersonPicture(getResources().getDrawable(R.drawable.m3));
item3.setComments(prepareCommentsArray());
dataset.add(item3);

PagerCardDataPOJO item2 = new PagerCardDataPOJO();
item2.setBgImageDrawable(placePenguin(imageWidth, imageHeight));
item2.setHeadTitle("LANDMARK");
item2.setMainBgImageDrawableResource(R.drawable.nature);
item2.setHeadBgImageDrawableResource(R.drawable.nature_head);
item2.setHeadTitle("Nature");
item2.setPersonName("James Parkinson");
item2.setPersonMessage("Whales scream on fortune at port royal!");
item2.setComments(prepareCommentsArray());
item2.setPersonPicture(placePenguin(100, 100));
item2.setPersonPicture(getResources().getDrawable(R.drawable.f2));
dataset.add(item2);

PagerCardDataPOJO item1 = new PagerCardDataPOJO();
item1.setBgImageDrawable(placePenguin(imageWidth, imageHeight));
item1.setHeadTitle("Cuisine");
item1.setMainBgImageDrawableResource(R.drawable.nightlife);
item1.setHeadBgImageDrawableResource(R.drawable.nightlife_head);
item1.setHeadTitle("Night Life");
item1.setPersonMessage("Crescere satis ducunt ad teres terror.");
item1.setPersonName("Peter Robinson");
item1.setPersonPicture(placePenguin(100, 100));
item1.setPersonPicture(getResources().getDrawable(R.drawable.f1));
item1.setComments(prepareCommentsArray());
dataset.add(item1);

Expand All @@ -174,10 +187,10 @@ private List<CommentPOJO> prepareCommentsArray() {
new CommentPOJO(getResources().getDrawable(R.drawable.m5), "David Gibson", "When the queen experiments for nowhere, all particles control reliable, cold captains.", "13 nov. 1964"));
}

public Drawable placePenguin(int w, int h) {
public BitmapDrawable placePenguin(int w, int h) {
try {
InputStream is = (InputStream) new URL("http://placepenguin.com/" + w + "/" + h + "?" + System.currentTimeMillis()).getContent();
return Drawable.createFromStream(is, "place the penguin");
return (BitmapDrawable) BitmapDrawable.createFromStream(is, "place the penguin");
} catch (Exception e) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.ramotion.expandingcollection.examples.simple;

import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;

import com.ramotion.expandingcollection.ECCardData;
Expand All @@ -11,8 +12,12 @@
public class PagerCardDataPOJO implements ECCardData<CommentPOJO> {

private String headTitle;
private Drawable headBgImageDrawable;
private Drawable mainBgImageDrawable;

private BitmapDrawable headBgImageDrawable;
private BitmapDrawable mainBgImageDrawable;

private Integer headBgImageDrawableResource;
private Integer mainBgImageDrawableResource;

private Drawable personPicture;
private String personName;
Expand All @@ -38,25 +43,39 @@ public void setHeadTitle(String headTitle) {
}

@Override
public Drawable getMainBgImageDrawable() {
public BitmapDrawable getHeadBgImageDrawable() {
return headBgImageDrawable;
}

public void setHeadBgImageDrawable(BitmapDrawable headBgImageDrawable) {
this.headBgImageDrawable = headBgImageDrawable;
}

@Override
public Drawable getHeadBgImageDrawable() {
return headBgImageDrawable;
public BitmapDrawable getMainBgImageDrawable() {
return mainBgImageDrawable;
}

public void setBgImageDrawable(Drawable bgImageDrawable) {
this.headBgImageDrawable = bgImageDrawable;
public void setMainBgImageDrawable(BitmapDrawable mainBgImageDrawable) {
this.mainBgImageDrawable = mainBgImageDrawable;
}

public void setHeadBgImageDrawable(Drawable headBgImageDrawable) {
this.headBgImageDrawable = headBgImageDrawable;
@Override
public Integer getHeadBgImageDrawableResource() {
return headBgImageDrawableResource;
}

public void setMainBgImageDrawable(Drawable mainBgImageDrawable) {
this.mainBgImageDrawable = mainBgImageDrawable;
public void setHeadBgImageDrawableResource(Integer headBgImageDrawableResource) {
this.headBgImageDrawableResource = headBgImageDrawableResource;
}

@Override
public Integer getMainBgImageDrawableResource() {
return mainBgImageDrawableResource;
}

public void setMainBgImageDrawableResource(Integer mainBgImageDrawableResource) {
this.mainBgImageDrawableResource = mainBgImageDrawableResource;
}

public Drawable getPersonPicture() {
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.ramotion.expandingcollection;

import android.graphics.Bitmap;
import android.util.LruCache;

public class BackgroundBitmapCache {

private final String TAG = "cache_manager";

private LruCache<Integer, Bitmap> mBackgroundsCache;

private static BackgroundBitmapCache instance;

public static BackgroundBitmapCache getInstance() {
if (instance == null) {
instance = new BackgroundBitmapCache();
instance.init();
}
return instance;
}

private void init() {
final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);

final int cacheSize = maxMemory / 5;

mBackgroundsCache = new LruCache<Integer, Bitmap>(cacheSize) {
@Override
protected void entryRemoved(boolean evicted, Integer key, Bitmap oldValue, Bitmap newValue) {
super.entryRemoved(evicted, key, oldValue, newValue);
}

@Override
protected int sizeOf(Integer key, Bitmap bitmap) {
// The cache size will be measured in kilobytes rather than number of items.
return bitmap.getByteCount() / 1024;
}
};
}

public void addBitmapToBgMemoryCache(Integer key, Bitmap bitmap) {
if (getBitmapFromBgMemCache(key) == null) {
mBackgroundsCache.put(key, bitmap);
}
}

public Bitmap getBitmapFromBgMemCache(Integer key) {
return mBackgroundsCache.get(key);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.ramotion.expandingcollection;

import android.graphics.BitmapFactory;

public class BitmapFactoryOptions extends BitmapFactory.Options {
public BitmapFactoryOptions() {
this.inScaled = false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.ramotion.expandingcollection;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.AsyncTask;
import android.support.annotation.DrawableRes;

public class BitmapWorkerTask extends AsyncTask<Integer, Void, Bitmap> {

private final Resources mResources;
private final BackgroundBitmapCache cache;
private final BitmapDrawable mProvidedBitmap;
@DrawableRes
private final int mProvidedBitmapResId;

public BitmapWorkerTask(Resources resources, BitmapDrawable providedBitmap, @DrawableRes int providedBitmapResId) {
this.mResources = resources;
this.cache = BackgroundBitmapCache.getInstance();
this.mProvidedBitmap = providedBitmap;
this.mProvidedBitmapResId = providedBitmapResId;
}

@Override
protected Bitmap doInBackground(Integer... params) {
Integer key = params[0];

if (mProvidedBitmap != null && !mProvidedBitmap.getBitmap().isRecycled()) {
cache.addBitmapToBgMemoryCache(key, mProvidedBitmap.getBitmap());
return mProvidedBitmap.getBitmap();
} else {
Bitmap cachedBitmap = cache.getBitmapFromBgMemCache(key);
if (cachedBitmap == null) {
cachedBitmap = BitmapFactory.decodeResource(mResources, mProvidedBitmapResId, new BitmapFactoryOptions());
cache.addBitmapToBgMemoryCache(key, cachedBitmap);
}
return cachedBitmap;
}
}


}
Loading

0 comments on commit 9dcacb3

Please sign in to comment.