我有一个回收器视图,它呈现一个带有图像和文本的列表。通过传递image_ID从文件服务器检索该图像url,然后使用该url来下载实际的图像。
这可以使用fresco lib来完成吗?有没有人能建议我如何使用fresco lib来做这件事?
发布于 2018-09-14 02:45:15
这对我来说很有效;https://github.com/abdeldjalilfl/Fresco-Simple-usage
类:
import android.content.Context;
import android.net.Uri;
import android.support.annotation.ColorRes;
import android.support.annotation.DrawableRes;
import android.support.annotation.NonNull;
import com.facebook.drawee.generic.RoundingParams;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.imagepipeline.core.ImagePipeline;
import com.upshotdev.login.R;
/**
* @author Abdeldjalil
* Fresco takes care of image loading and display Using Fresco
* dependencies implementation 'com.facebook.fresco:fresco:1.10.0'
* how to use this class
* example
Fresco.newBuilder((SimpleDraweeView) findViewById(R.id.my_image_view))
.setPlaceholderImage(R.color.grey200)
.withProgressBar(true)
.roundAsCircle(true)
.setProgressBarColor(R.color.colorAccent,R.color.grey500)
.setRoundCircleColor(R.color.colorAccent)
.setUri(Uri.parse("https://www.google.dz/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png"))
.build(this);
*
*/
public class Fresco {
private static Fresco loadImage;
private SimpleDraweeView imageView;
private Uri uri;
private boolean withProgressBar=true;
private boolean roundAsCircle=false;
//
private int ID_RES_PLACEHOLDER_IMAGE=R.color.grey200;
//
private int ID_COLOR_BORDER_CIRCLE=R.color.colorAccent;
//
private int ID_COLOR_PROGRESS=R.color.colorAccent;
private int ID_COLOR_BACKGROUND_PROGRESS=R.color.grey500;
public static Fresco newBuilder(@NonNull SimpleDraweeView imageView) {
loadImage=new Fresco(imageView);
return loadImage;
}
private Fresco(@NonNull SimpleDraweeView imageView) {
this.imageView=imageView;
}
/**
* progress bar
* @param withProgressBar default value: true
*
*/
public Fresco withProgressBar(boolean withProgressBar){
this.withProgressBar=withProgressBar;
return loadImage;
}
/**
*
* @param placeholderImage The placeholder is shown until the image is ready.
* default value : R.color.grey200
*/
public Fresco setPlaceholderImage(@DrawableRes int placeholderImage){
this.ID_RES_PLACEHOLDER_IMAGE=placeholderImage;
return loadImage;
}
/**
*
* @param roundAsCircle default value : false
*
*/
public Fresco roundAsCircle(boolean roundAsCircle){
this.roundAsCircle =roundAsCircle;
return loadImage;
}
/**
*
* @param color R.color.colorAccent
* @param backgroundColor R.color.grey500
*
*/
public Fresco setProgressBarColor(@ColorRes int color, @ColorRes int backgroundColor){
this.ID_COLOR_PROGRESS=color;
this.ID_COLOR_BACKGROUND_PROGRESS=backgroundColor;
return loadImage;
}
/**
*
* @param color default value: R.color.colorAccent
*
*/
public Fresco setRoundCircleColor(@ColorRes int color){
this.ID_COLOR_BORDER_CIRCLE=color;
return loadImage;
}
public Fresco setUri(Uri uri) {
this.uri = uri;
return loadImage;
}
/**
* start downloading the image
*/
public void build(@NonNull Context context){
if (imageView!=null) {
imageView.getHierarchy().setPlaceholderImage(ID_RES_PLACEHOLDER_IMAGE);
if(uri!=null){
imageView.setImageURI(uri);
}
if (withProgressBar){
final CircleProgressBarDrawable progressBarDrawable=new CircleProgressBarDrawable();
progressBarDrawable.setColor(context.getResources().getColor(ID_COLOR_PROGRESS));
progressBarDrawable.setBackgroundColor(context.getResources().getColor(ID_COLOR_BACKGROUND_PROGRESS));
progressBarDrawable.setRadius(context.getResources().getDimensionPixelSize(R.dimen.radius_hierarchy_progress_fresco));
//
imageView.getHierarchy().setProgressBarImage(progressBarDrawable);
}
if (roundAsCircle){
int color = context.getResources().getColor(ID_COLOR_BORDER_CIRCLE);
RoundingParams roundingParams = RoundingParams.fromCornersRadius(6f);
roundingParams.setBorder(color, 7.0f);
roundingParams.setRoundAsCircle(true);
imageView.getHierarchy().setRoundingParams(roundingParams);
}
}
}
//
/**
* clear cache of images using ImagePipeline
*/
public static void clearCache(){
//
ImagePipeline imagePipeline = com.facebook.drawee.backends.pipeline.Fresco.getImagePipeline();
imagePipeline.clearMemoryCaches();
imagePipeline.clearDiskCaches();
// combines above two lines
imagePipeline.clearCaches();
}
}如何使用!
Fresco.newBuilder((SimpleDraweeView) findViewById(R.id.my_image_view))
.setUri(Uri.parse("image link"))
.build(this);有关详细信息,请参阅:
Fresco.newBuilder((SimpleDraweeView) findViewById(R.id.my_image_view))
.setPlaceholderImage(R.color.grey200)
.withProgressBar(true)
.roundAsCircle(true)
.setProgressBarColor(R.color.colorAccent,R.color.grey500)
.setRoundCircleColor(R.color.colorAccent)
.setUri(Uri.parse("image link"))
.build(this);发布于 2018-03-08 23:47:32
Fresco有一个示例应用程序,展示了如何创建一个简单的‘`RecyclerView’。
它使用List<Uri>存储虚拟数据,您必须将其替换为URL。
https://stackoverflow.com/questions/49084246
复制相似问题