Skip to content

Commit

Permalink
Reduce memory usage by capping max size of dribbble images.
Browse files Browse the repository at this point in the history
  • Loading branch information
nickbutcher committed Nov 23, 2015
1 parent 08a4a3f commit bdfbe94
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 40 deletions.
31 changes: 20 additions & 11 deletions app/src/main/java/io/plaidapp/data/api/dribbble/model/Images.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,9 @@
*/
public class Images implements Parcelable {

@SuppressWarnings("unused")
public static final Parcelable.Creator<Images> CREATOR = new Parcelable.Creator<Images>() {
@Override
public Images createFromParcel(Parcel in) {
return new Images(in);
}
private static final int[] NORMAL_IMAGE_SIZE = new int[] { 400, 300 };
private static final int[] TWO_X_IMAGE_SIZE = new int[] { 800, 600 };

@Override
public Images[] newArray(int size) {
return new Images[size];
}
};
public final String hidpi;
public final String normal;
public final String teaser;
Expand All @@ -57,6 +48,10 @@ public String best() {
return !TextUtils.isEmpty(hidpi) ? hidpi : normal;
}

public int[] bestSize() {
return !TextUtils.isEmpty(hidpi) ? TWO_X_IMAGE_SIZE : NORMAL_IMAGE_SIZE;
}

@Override
public int describeContents() {
return 0;
Expand All @@ -68,4 +63,18 @@ public void writeToParcel(Parcel dest, int flags) {
dest.writeString(normal);
dest.writeString(teaser);
}

@SuppressWarnings("unused")
public static final Parcelable.Creator<Images> CREATOR = new Parcelable.Creator<Images>() {
@Override
public Images createFromParcel(Parcel in) {
return new Images(in);
}

@Override
public Images[] newArray(int size) {
return new Images[size];
}
};

}
2 changes: 2 additions & 0 deletions app/src/main/java/io/plaidapp/ui/DribbbleShot.java
Original file line number Diff line number Diff line change
Expand Up @@ -188,11 +188,13 @@ public void onDragDismissed() {
};

// load the main image
final int[] imageSize = shot.images.bestSize();
Glide.with(this)
.load(shot.images.best())
.listener(shotLoadListener)
.diskCacheStrategy(DiskCacheStrategy.SOURCE)
.priority(Priority.IMMEDIATE)
.override(imageSize[0], imageSize[1])
.into(imageView);
imageView.setOnClickListener(shotClick);
shotSpacer.setOnClickListener(shotClick);
Expand Down
17 changes: 3 additions & 14 deletions app/src/main/java/io/plaidapp/ui/FeedAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ public class FeedAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private static final int TYPE_DRIBBBLE_SHOT = 1;
private static final int TYPE_PRODUCT_HUNT_POST = 2;
private static final int TYPE_LOADING_MORE = -1;
private static final int MAX_IMAGE_CACHE_WIDTH = 1440;
public static final float DUPE_WEIGHT_BOOST = 0.4f;

// we need to hold on to an activity ref for the shared element transitions :/
Expand Down Expand Up @@ -127,7 +126,6 @@ public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType
layoutInflater.inflate(R.layout.designer_news_story_item, parent, false),
pocketIsInstalled);
case TYPE_DRIBBBLE_SHOT:
ensureShotImageWidth(parent);
return new DribbbleShotHolder(
layoutInflater.inflate(R.layout.dribbble_shot_item, parent, false));
case TYPE_PRODUCT_HUNT_POST:
Expand Down Expand Up @@ -282,6 +280,7 @@ private void bindDribbbleShotView(final Shot shot,
final DribbbleShotHolder holder,
final int position) {
final BadgedFourThreeImageView iv = (BadgedFourThreeImageView) holder.itemView;
final int[] imageSize = shot.images.bestSize();
Glide.with(host)
.load(shot.images.best())
.listener(new RequestListener<String, GlideDrawable>() {
Expand Down Expand Up @@ -333,7 +332,8 @@ public boolean onException(Exception e, String model, Target<GlideDrawable>
.placeholder(shotLoadingPlaceholders[position % shotLoadingPlaceholders.length])
.diskCacheStrategy(DiskCacheStrategy.SOURCE)
.fitCenter()
.into(new DribbbleTarget(iv, false, shotWidth));
.override(imageSize[0], imageSize[1])
.into(new DribbbleTarget(iv, false));

iv.setOnClickListener(new View.OnClickListener() {
@Override
Expand Down Expand Up @@ -558,17 +558,6 @@ public int getItemCount() {
return getDataItemCount() + 1;
}

/**
* We override the image size as we want to cache images at device width for a smooth
* transition from the home grid to the detail screen
*/
private void ensureShotImageWidth(View view) {
if (shotWidth == 0) {
// constrain to a max width to reduce OutOfMemory errors!
shotWidth = Math.min(view.getRootView().getWidth(), MAX_IMAGE_CACHE_WIDTH);
}
}

/**
* The shared element transition to dribbble shots & dn stories can intersect with the FAB.
* This can cause a strange layers-passing-through-each-other effect, especially on return.
Expand Down
16 changes: 1 addition & 15 deletions app/src/main/java/io/plaidapp/util/glide/DribbbleTarget.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,9 @@ public class DribbbleTarget extends GlideDrawableImageViewTarget implements

private final boolean playGifs;

// we override the image size as we want to cache images at device width for a smooth
// transition from the home grid to the detail screen
private final int imageWidth;
private final int imageHeight;

public DribbbleTarget(BadgedFourThreeImageView view,
boolean playGifs,
int imageWidth) {
public DribbbleTarget(BadgedFourThreeImageView view, boolean playGifs) {
super(view);
this.playGifs = playGifs;
this.imageWidth = imageWidth;
this.imageHeight = imageWidth * 3 / 4;
}

@Override
Expand Down Expand Up @@ -109,9 +100,4 @@ public void onGenerated(Palette palette) {
ContextCompat.getColor(getView().getContext(), R.color.mid_grey), true));
}

@Override
public void getSize(SizeReadyCallback cb) {
cb.onSizeReady(imageWidth, imageHeight);
}

}

0 comments on commit bdfbe94

Please sign in to comment.