Skip to content

Commit

Permalink
Fixed bug with nullable resource id in library. Removed bitmap drawab…
Browse files Browse the repository at this point in the history
…le as bitmap source support, now works only resource id as bitmap source.
  • Loading branch information
oleg-vasiliev committed Apr 5, 2017
1 parent 3daf1f7 commit 3ac830d
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,23 @@ public class BitmapWorkerTask extends AsyncTask<Integer, Void, Bitmap> {

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

public BitmapWorkerTask(Resources resources, BitmapDrawable providedBitmap, @DrawableRes int providedBitmapResId) {
public BitmapWorkerTask(Resources resources, @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;
Bitmap cachedBitmap = cache.getBitmapFromBgMemCache(key);
if (cachedBitmap == null) {
cachedBitmap = BitmapFactory.decodeResource(mResources, mProvidedBitmapResId, new BitmapFactoryOptions());
cache.addBitmapToBgMemoryCache(key, cachedBitmap);
}
return cachedBitmap;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,15 @@ public View makeView() {
bgImageOutRightAnimation = createBgImageOutAnimation(0, bgImageGap, movementDuration);
}

public ECBackgroundSwitcherView withAnimationSettings(int movementDuration, int alphaDuration) {
this.movementDuration = movementDuration;
this.alphaDuration = alphaDuration;
bgImageInLeftAnimation = createBgImageInAnimation(bgImageGap, 0, movementDuration, alphaDuration);
bgImageOutLeftAnimation = createBgImageOutAnimation(0, -bgImageGap, movementDuration);
bgImageInRightAnimation = createBgImageInAnimation(-bgImageGap, 0, movementDuration, alphaDuration);
bgImageOutRightAnimation = createBgImageOutAnimation(0, bgImageGap, movementDuration);
return this;
}
// public ECBackgroundSwitcherView withAnimationSettings(int movementDuration, int alphaDuration) {
// this.movementDuration = movementDuration;
// this.alphaDuration = alphaDuration;
// bgImageInLeftAnimation = createBgImageInAnimation(bgImageGap, 0, movementDuration, alphaDuration);
// bgImageOutLeftAnimation = createBgImageOutAnimation(0, -bgImageGap, movementDuration);
// bgImageInRightAnimation = createBgImageInAnimation(-bgImageGap, 0, movementDuration, alphaDuration);
// bgImageOutRightAnimation = createBgImageOutAnimation(0, bgImageGap, movementDuration);
// return this;
// }

@Override
protected int getChildDrawingOrder(int childCount, int i) {
Expand Down Expand Up @@ -114,29 +114,23 @@ private synchronized void setImageBitmapWithAnimation(Bitmap newBitmap, Animatio
public void cacheBackgroundAtPosition(ECPager pager, int position) {
if (position >= 0 && position < pager.getAdapter().getCount()) {
Integer mainBgImageDrawableResource = pager.getDataFromAdapterDataset(position).getMainBackgroundResource();
BitmapDrawable mainBgImageDrawable = pager.getDataFromAdapterDataset(position).getMainBackgroundDrawable();
BitmapWorkerTask addBitmapToCacheTask = new BitmapWorkerTask(getResources(), mainBgImageDrawable, mainBgImageDrawableResource);
if (mainBgImageDrawableResource == null) return;
BitmapWorkerTask addBitmapToCacheTask = new BitmapWorkerTask(getResources(), mainBgImageDrawableResource);
addBitmapToCacheTask.execute(position);
}
}

public void updateCurrentBackground(ECPager pager, final AnimationDirection direction) {
int position = pager.getCurrentPosition();
BackgroundBitmapCache instance = BackgroundBitmapCache.getInstance();
Integer mainBgImageDrawableResource = pager.getDataFromAdapterDataset(position).getMainBackgroundResource();
BitmapDrawable mainBgImageDrawable = pager.getDataFromAdapterDataset(position).getMainBackgroundDrawable();

if (mainBgImageDrawable != null && !mainBgImageDrawable.getBitmap().isRecycled()) {
instance.addBitmapToBgMemoryCache(position, mainBgImageDrawable.getBitmap());
setImageBitmapWithAnimation(mainBgImageDrawable.getBitmap(), direction);
} else {
Bitmap cachedBitmap = instance.getBitmapFromBgMemCache(position);
if (cachedBitmap == null) {
cachedBitmap = BitmapFactory.decodeResource(getResources(), mainBgImageDrawableResource, new BitmapFactoryOptions());
instance.addBitmapToBgMemoryCache(position, cachedBitmap);
}
setImageBitmapWithAnimation(cachedBitmap, direction);
Bitmap cachedBitmap = instance.getBitmapFromBgMemCache(position);
if (cachedBitmap == null) {
Integer mainBgImageDrawableResource = pager.getDataFromAdapterDataset(position).getMainBackgroundResource();
if (mainBgImageDrawableResource == null) return;
cachedBitmap = BitmapFactory.decodeResource(getResources(), mainBgImageDrawableResource, new BitmapFactoryOptions());
instance.addBitmapToBgMemoryCache(position, cachedBitmap);
}
setImageBitmapWithAnimation(cachedBitmap, direction);
}

public void updateCurrentBackgroundAsync(ECPager pager, final AnimationDirection direction) {
Expand All @@ -145,15 +139,15 @@ public void updateCurrentBackgroundAsync(ECPager pager, final AnimationDirection
}
int position = pager.getCurrentPosition();
Integer mainBgImageDrawableResource = pager.getDataFromAdapterDataset(position).getMainBackgroundResource();
BitmapDrawable mainBgImageDrawable = pager.getDataFromAdapterDataset(position).getMainBackgroundDrawable();
mCurrentAnimationTask = new BitmapWorkerTask(getResources(), mainBgImageDrawable, mainBgImageDrawableResource) {
if (mainBgImageDrawableResource == null) return;
mCurrentAnimationTask = new BitmapWorkerTask(getResources(), mainBgImageDrawableResource) {
@Override
protected void onPostExecute(Bitmap bitmap) {
super.onPostExecute(bitmap);
setImageBitmapWithAnimation(bitmap, direction);
}
};
mCurrentAnimationTask.execute(position, pager.getDataFromAdapterDataset(position).getMainBackgroundResource());
mCurrentAnimationTask.execute(position);
}


Expand Down Expand Up @@ -185,6 +179,6 @@ private Animation createBgImageOutAnimation(int fromX, int toX, int transitionDu
}

enum AnimationDirection {
LEFT, RIGHT;
LEFT, RIGHT
}
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,16 @@
package com.ramotion.expandingcollection;

import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.support.annotation.DrawableRes;
import android.support.v4.media.MediaMetadataCompat;

import java.util.Collection;
import java.util.List;

public interface ECCardData<T> {

@DrawableRes
Integer getMainBackgroundResource();

BitmapDrawable getMainBackgroundDrawable();

@DrawableRes
Integer getHeadBackgroundResource();

BitmapDrawable getHeadBackgroundDrawable();

List<T> getListItems();
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@

import ramotion.com.expandingcollection.R;

import static android.R.attr.bitmap;

public abstract class ECPagerViewAdapter extends PagerAdapter {
public static final String TAG = "ecview";

private ECPagerCard activeCard;
private List<ECCardData> dataset;
Expand All @@ -35,12 +36,9 @@ public Object instantiateItem(ViewGroup container, int position) {

headView.setHeight(pagerContainer.getCardHeight());

Bitmap bitmap = dataset.get(position).getHeadBackgroundDrawable() == null ? null : dataset.get(position).getHeadBackgroundDrawable().getBitmap();
if (bitmap == null) {
Integer drawableRes = dataset.get(position).getHeadBackgroundResource();
if (drawableRes != null) {
headView.setHeadImageBitmap(BitmapFactory.decodeResource(pagerContainer.getResources(), drawableRes, new BitmapFactoryOptions()));
}
Integer drawableRes = dataset.get(position).getHeadBackgroundResource();
if (drawableRes != null) {
headView.setHeadImageBitmap(BitmapFactory.decodeResource(pagerContainer.getResources(), drawableRes, new BitmapFactoryOptions()));
}

instantiateCard(inflaterService, headView, ecPagerCardContentList, dataset.get(position));
Expand Down

0 comments on commit 3ac830d

Please sign in to comment.