Loading Image from an URL in an ImageView (showing progress bar while loading image).
We can create a Custom ImageView for that ,It will make stuff more easy.
we will call this class ImageLoaderView.
public class ImageLoaderView extends LinearLayout
{
public ImageLoaderView(Context context)
{
super(context);
init(context, null);
}
public ImageLoaderView(Context context, AttributeSet attrs)
{
super(context, attrs);
init(context, attrs);
}
public ImageLoaderView(Context context, AttributeSet attrs, int defStyleAttr)
{
super(context, attrs, defStyleAttr);
init(context, attrs);
}
public ImageLoaderView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)
{
super(context, attrs, defStyleAttr, defStyleRes);
init(context, attrs);
}
private ImageView imageView;
private ProgressBar progressBar;
public void init(Context context, AttributeSet attrs)
{
this.context = context;
setGravity(Gravity.CENTER);
imageView = new ImageView(context);
LinearLayout.LayoutParams params = new LayoutParams
(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
imageView.setLayoutParams(params);
progressBar = new ProgressBar(context);
progressBar.setVisibility(GONE);
addView(imageView);
addView(progressBar);
}
private Context context;
public ImageView getImageView()
{
return imageView;
}
public void setImageURLresize(String url, int height, int width)
{
loadingImage(true);
//we are using glide as an example here
Glide.with(context)
.load(url)
.override(height, width)
.placeholder(R.drawable.loading)
.listener(new RequestListener<String, GlideDrawable>()
{
@Override
public boolean onException(Exception e, String model,
Target<GlideDrawable> target, boolean isFirstResource)
{
return false;
}
@Override
public boolean onResourceReady(GlideDrawable resource, String model,
Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource)
{
loadingImage(false);
return false;
}
})
.into(imageView);
}
public void setImageURL(String url)
{
loadingImage(true);
//we are using glide as an example here
Glide.with(context)
.load(url)
.placeholder(R.drawable.loading)
.listener(new RequestListener<String, GlideDrawable>()
{
@Override
public boolean onException(Exception e, String model,
Target<GlideDrawable> target, boolean isFirstResource)
{
return false;
}
@Override
public boolean onResourceReady(GlideDrawable resource, String model,
Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource)
{
loadingImage(false);
return false;
}
})
.into(imageView);
}
public void loadingImage(boolean loading)
{
if (loading)
{
imageView.setVisibility(GONE);
progressBar.setVisibility(VISIBLE);
}
else
{
progressBar.setVisibility(GONE);
imageView.setVisibility(VISIBLE);
}
}
}
Now, instead of using ImageView use ImageLoaderView everywhere in your project.
for example:
We can create a Custom ImageView for that ,It will make stuff more easy.
we will call this class ImageLoaderView.
public class ImageLoaderView extends LinearLayout
{
public ImageLoaderView(Context context)
{
super(context);
init(context, null);
}
public ImageLoaderView(Context context, AttributeSet attrs)
{
super(context, attrs);
init(context, attrs);
}
public ImageLoaderView(Context context, AttributeSet attrs, int defStyleAttr)
{
super(context, attrs, defStyleAttr);
init(context, attrs);
}
public ImageLoaderView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)
{
super(context, attrs, defStyleAttr, defStyleRes);
init(context, attrs);
}
private ImageView imageView;
private ProgressBar progressBar;
public void init(Context context, AttributeSet attrs)
{
this.context = context;
setGravity(Gravity.CENTER);
imageView = new ImageView(context);
LinearLayout.LayoutParams params = new LayoutParams
(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
imageView.setLayoutParams(params);
progressBar = new ProgressBar(context);
progressBar.setVisibility(GONE);
addView(imageView);
addView(progressBar);
}
private Context context;
public ImageView getImageView()
{
return imageView;
}
public void setImageURLresize(String url, int height, int width)
{
loadingImage(true);
//we are using glide as an example here
Glide.with(context)
.load(url)
.override(height, width)
.placeholder(R.drawable.loading)
.listener(new RequestListener<String, GlideDrawable>()
{
@Override
public boolean onException(Exception e, String model,
Target<GlideDrawable> target, boolean isFirstResource)
{
return false;
}
@Override
public boolean onResourceReady(GlideDrawable resource, String model,
Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource)
{
loadingImage(false);
return false;
}
})
.into(imageView);
}
public void setImageURL(String url)
{
loadingImage(true);
//we are using glide as an example here
Glide.with(context)
.load(url)
.placeholder(R.drawable.loading)
.listener(new RequestListener<String, GlideDrawable>()
{
@Override
public boolean onException(Exception e, String model,
Target<GlideDrawable> target, boolean isFirstResource)
{
return false;
}
@Override
public boolean onResourceReady(GlideDrawable resource, String model,
Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource)
{
loadingImage(false);
return false;
}
})
.into(imageView);
}
public void loadingImage(boolean loading)
{
if (loading)
{
imageView.setVisibility(GONE);
progressBar.setVisibility(VISIBLE);
}
else
{
progressBar.setVisibility(GONE);
imageView.setVisibility(VISIBLE);
}
}
}
Now, instead of using ImageView use ImageLoaderView everywhere in your project.
for example:
<ImageLoaderView
android:id = "@+id/imgv_search_profile" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_gravity = "center" android:background = "@android:color/white" android:src = "@mipmap/ic_launcher"/>
0 comments:
Post a Comment