-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for passing through Bitmaps and Drawables.
- Loading branch information
Showing
7 changed files
with
207 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
library/src/main/java/com/bumptech/glide/load/resource/bitmap/UnitBitmapDecoder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.bumptech.glide.load.resource.bitmap; | ||
|
||
import android.graphics.Bitmap; | ||
import android.support.annotation.Nullable; | ||
import com.bumptech.glide.load.Options; | ||
import com.bumptech.glide.load.ResourceDecoder; | ||
import com.bumptech.glide.load.engine.Resource; | ||
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool; | ||
import java.io.IOException; | ||
|
||
public final class UnitBitmapDecoder implements ResourceDecoder<Bitmap, Bitmap> { | ||
|
||
private final BitmapPool bitmapPool; | ||
|
||
public UnitBitmapDecoder(BitmapPool bitmapPool) { | ||
this.bitmapPool = bitmapPool; | ||
} | ||
|
||
@Override | ||
public boolean handles(Bitmap source, Options options) throws IOException { | ||
return true; | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public Resource<Bitmap> decode(Bitmap source, int width, int height, Options options) | ||
throws IOException { | ||
return new BitmapResource(source, bitmapPool); | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
library/src/main/java/com/bumptech/glide/load/resource/drawable/DrawableResourceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package com.bumptech.glide.load.resource.drawable; | ||
|
||
import android.graphics.drawable.BitmapDrawable; | ||
import android.graphics.drawable.Drawable; | ||
import com.bumptech.glide.load.engine.Resource; | ||
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool; | ||
import com.bumptech.glide.load.resource.bitmap.BitmapDrawableResource; | ||
|
||
/** | ||
* Handles generic {@link Drawable} types where we may be uncertain of their size or type. | ||
*/ | ||
final class DrawableResourceImpl extends DrawableResource<Drawable> { | ||
|
||
@SuppressWarnings("unchecked") | ||
public static Resource<Drawable> newInstance(Drawable drawable, BitmapPool bitmapPool) { | ||
if (drawable instanceof BitmapDrawable) { | ||
BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable; | ||
return (Resource<Drawable>) (Resource<? extends Drawable>) | ||
new BitmapDrawableResource(bitmapDrawable, bitmapPool); | ||
} | ||
return new DrawableResourceImpl(drawable); | ||
} | ||
|
||
private DrawableResourceImpl(Drawable drawable) { | ||
super(drawable); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
@Override | ||
public Class<Drawable> getResourceClass() { | ||
return (Class<Drawable>) drawable.getClass(); | ||
} | ||
|
||
@Override | ||
public int getSize() { | ||
// 4 bytes per pixel for ARGB_8888 Bitmaps is something of a reasonable approximation. If | ||
// there are no intrinsic bounds, we can fall back just to 1. | ||
return Math.max(1, drawable.getIntrinsicWidth() * drawable.getIntrinsicHeight() * 4); | ||
} | ||
|
||
@Override | ||
public void recycle() { | ||
// Do nothing. | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
library/src/main/java/com/bumptech/glide/load/resource/drawable/UnitDrawableDecoder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.bumptech.glide.load.resource.drawable; | ||
|
||
import android.graphics.drawable.Drawable; | ||
import android.support.annotation.Nullable; | ||
import com.bumptech.glide.load.Options; | ||
import com.bumptech.glide.load.ResourceDecoder; | ||
import com.bumptech.glide.load.engine.Resource; | ||
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool; | ||
import java.io.IOException; | ||
|
||
/** | ||
* Passes through a {@link Drawable} as a {@link Drawable} based {@link Resource}. | ||
*/ | ||
public class UnitDrawableDecoder implements ResourceDecoder<Drawable, Drawable> { | ||
private final BitmapPool bitmapPool; | ||
|
||
public UnitDrawableDecoder(BitmapPool bitmapPool) { | ||
this.bitmapPool = bitmapPool; | ||
} | ||
|
||
@Override | ||
public boolean handles(Drawable source, Options options) throws IOException { | ||
return true; | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public Resource<Drawable> decode(Drawable source, int width, int height, Options options) | ||
throws IOException { | ||
return DrawableResourceImpl.newInstance(source, bitmapPool); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters