首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在fresco库中下载图像

在fresco库中下载图像
EN

Stack Overflow用户
提问于 2018-03-03 20:46:57
回答 2查看 1.2K关注 0票数 1

我有一个回收器视图,它呈现一个带有图像和文本的列表。通过传递image_ID从文件服务器检索该图像url,然后使用该url来下载实际的图像。

这可以使用fresco lib来完成吗?有没有人能建议我如何使用fresco lib来做这件事?

EN

回答 2

Stack Overflow用户

发布于 2018-09-14 02:45:15

这对我来说很有效;https://github.com/abdeldjalilfl/Fresco-Simple-usage

类:

代码语言:javascript
复制
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();
    }




}

如何使用!

代码语言:javascript
复制
Fresco.newBuilder((SimpleDraweeView) findViewById(R.id.my_image_view))
.setUri(Uri.parse("image link"))
            .build(this);

有关详细信息,请参阅:

代码语言:javascript
复制
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);
票数 1
EN

Stack Overflow用户

发布于 2018-03-08 23:47:32

Fresco有一个示例应用程序,展示了如何创建一个简单的‘`RecyclerView’。

看一看https://github.com/facebook/fresco/blob/master/samples/showcase/src/main/java/com/facebook/fresco/samples/showcase/drawee/DraweeRecyclerViewFragment.java

它使用List<Uri>存储虚拟数据,您必须将其替换为URL。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49084246

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档