Android开发常用:Fresco加载图片的几种方式

XML使用

xmlns:fresco="http://schemas.android.com/apk/res-auto"


 
        


封装的方法:分别是从res和从网络加载(注:下面的从res加载图片的方法是错误的

   private void setImageViewFromRes(final String imageName, final SimpleDraweeView imageView){
        Observable.create(new ObservableOnSubscribe() {
            @Override
            public void subscribe(@NonNull ObservableEmitter e) throws Exception {

                e.onNext(imageName);


            }
        }).observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io()).subscribe(new Observer() {
            @Override
            public void onSubscribe(@NonNull Disposable d) {

            }

            @Override
            public void onNext(@NonNull String s) {
                Uri uri = Uri.parse("res://" +
                        mContext.getPackageName() +
                        "/" + s);
                imageView.setImageURI( Uri.parse("res://" +
                        mContext.getPackageName() +
                        "/" + s));
            }

            @Override
            public void onError(@NonNull Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });

    }
    private void setImageView(final String uri, final SimpleDraweeView imageView){
        Observable.create(new ObservableOnSubscribe() {
            @Override
            public void subscribe(@NonNull ObservableEmitter e) throws Exception {

                e.onNext(uri);


            }
        }).observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io()).subscribe(new Observer() {
            @Override
            public void onSubscribe(@NonNull Disposable d) {

            }

            @Override
            public void onNext(@NonNull String s) {
                imageView.setImageURI(Uri.parse(s));
            }

            @Override
            public void onError(@NonNull Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });

    }



 

1.网络图

里面的参数是图片的网络地址

userPhoto.setImageURI(SharedPreferenceUtils.get(context, "myInfo", "Photo", "").toString());


2.drawable图

 /**
     * 加载本地图片(drawable图片)
     * @param context
     * @param simpleDraweeView
     * @param id
     */
    public static void loadResPic(Context context, SimpleDraweeView simpleDraweeView, int id) {
        Uri uri = Uri.parse("res://" +
                context.getPackageName() +
                "/" + id);
        simpleDraweeView.setImageURI(uri);
    }


3.file图

userPhoto.setImageURI(Uri.parse("file://"+result.getImage().getCompressPath()));


你可能感兴趣的:(Android)