How can I do this using Glide? I want to cache image to use it another time also. Thanks in advance.
I think the best way to achieve it's works with your own ViewTarget implementation, because this class has specific methods to be handled by Glide automatically in different scenarios.
The abstract implementation for ViewGroup (LinearLayout, RelativeLayout and so on).
public abstract class ViewGroupTarget extends ViewTarget implements GlideAnimation.ViewAdapter {
public ViewGroupTarget(ViewGroup view) {
super(view);
}
/**
* Returns the current {@link android.graphics.drawable.Drawable} being displayed in the view using
* {@link android.widget.ImageView#getDrawable()}.
*/
@Override
public Drawable getCurrentDrawable() {
return view.getBackground();
}
/**
* Sets the given {@link android.graphics.drawable.Drawable} on the view using
* {@link android.widget.ImageView#setImageDrawable(android.graphics.drawable.Drawable)}.
*
* @param drawable {@inheritDoc}
*/
@Override
public void setDrawable(Drawable drawable) {
view.setBackground(drawable);
}
/**
* Sets the given {@link android.graphics.drawable.Drawable} on the view using
* {@link android.widget.ImageView#setImageDrawable(android.graphics.drawable.Drawable)}.
*
* @param placeholder {@inheritDoc}
*/
@Override
public void onLoadStarted(Drawable placeholder) {
view.setBackground(placeholder);
}
/**
* Sets the given {@link android.graphics.drawable.Drawable} on the view using
* {@link android.widget.ImageView#setImageDrawable(android.graphics.drawable.Drawable)}.
*
* @param errorDrawable {@inheritDoc}
*/
@Override
public void onLoadFailed(Exception e, Drawable errorDrawable) {
view.setBackground(errorDrawable);
}
/**
* Sets the given {@link android.graphics.drawable.Drawable} on the view using
* {@link android.widget.ImageView#setImageDrawable(android.graphics.drawable.Drawable)}.
*
* @param placeholder {@inheritDoc}
*/
@Override
public void onLoadCleared(Drawable placeholder) {
view.setBackground(placeholder);
}
@Override
public void onResourceReady(Z resource, GlideAnimation super Z> glideAnimation) {
this.setResource(resource);
}
protected abstract void setResource(Z resource);
}
The specific implementation, in this case for LinearLayout.
public class LinearLayoutTarget extends ViewGroupTarget {
private Context context;
public LinearLayoutTarget(Context context, LinearLayout linearLayout) {
super(linearLayout);
this.context = context;
}
/**
* Sets the {@link android.graphics.Bitmap} on the view using
* {@link android.widget.ImageView#setImageBitmap(android.graphics.Bitmap)}.
*
* @param resource The bitmap to display.
*/
@Override
protected void setResource(Bitmap resource) {
view.setBackground(new BitmapDrawable(context.getResources(), resource));
}
}
To work with.
Glide.with(this.getApplicationContext())
.load(R.drawable.your_image)
.asBitmap()
.into(new LinearLayoutTarget(this.getApplicationContext(), (LinearLayout) yourLinearLayoutInstanceHere));
Or even more simple working without Bitmap.
The specific implementation.
public class LinearLayoutTarget extends ViewGroupTarget {
public LinearLayoutTarget(LinearLayout linearLayout) {
super(linearLayout);
}
/**
* Sets the {@link android.graphics.Bitmap} on the view using
* {@link android.widget.ImageView#setImageBitmap(android.graphics.Bitmap)}.
*
* @param resource The bitmap to display.
*/
@Override
protected void setResource(Drawable resource) {
view.setBackground(resource);
}
}
To work with.
Glide.with(this.getApplicationContext())
.load(R.drawable.your_image)
.into(new LinearLayoutTarget((LinearLayout) yourLinearLayoutInstanceHere));