RXjava,RXandroid下载图片并显示

1联网权限
2引入rx

    implementation 'io.reactivex.rxjava2:rxandroid:2.0.1'
    implementation 'io.reactivex.rxjava2:rxjava:2.0.7'

3使用



import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;

import com.hisense.test.R;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

public class ConstrantActivity extends AppCompatActivity {
    private ImageView img;

    // 网络图片的链接地址
    private final static String PATH = "http://pic1.win4000.com/wallpaper/c/53cdd1f7c1f21.jpg";
    private ProgressDialog progressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_constrant);


        img = (ImageView) findViewById(R.id.img);

    }

    public void show(View view) {
        //上层
        Observable.just(PATH)
                //path变为bitmap    2
                .map(new Function<String, Bitmap>() {
                    @NonNull @Override
                    public Bitmap apply(@NonNull String path) throws Exception {
                        try {
                            Thread.sleep(2000); // 睡眠2秒钟

                            URL url = new URL(path);
                            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                            httpURLConnection.setConnectTimeout(5000); // 设置请求连接时长 5秒
                            int responseCode = httpURLConnection.getResponseCode(); // 才开始 request    拿到服务器的响应  200成功  404有问题 ...
                            if (responseCode == HttpURLConnection.HTTP_OK) {
                                InputStream inputStream = httpURLConnection.getInputStream();
                                Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                                return bitmap;
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        return null;
                    }
                })
                //给联网耗时操作分配异步线程  3  
                .subscribeOn(Schedulers.io())
                给显示图片的终点分配android主线程  4  
                .observeOn(AndroidSchedulers.mainThread())

                .subscribe(new Observer<Bitmap>() {
                    //关联起点和终点(订阅)  1
                    @Override public void onSubscribe(Disposable d) {
                        // 显示加载框
                        progressDialog = new ProgressDialog(ConstrantActivity.this);
                        progressDialog.setTitle("RXJava Derry run 正在加载中..");
                        progressDialog.show();

                    }
                    //接收上层的响应(当前层)  5
                    @Override public void onNext(Bitmap s) {
                        img.setImageBitmap(s);

                    }

                    @Override public void onError(Throwable e) {

                    }
                    //6
                    @Override public void onComplete() {
                        if(progressDialog!=null){
                            progressDialog.cancel();
                        }

                    }
                });

    }
}

4layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.ConstrantActivity">


    <Button

        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="show picture"
        android:onClick="show"
         />
    <ImageView
        android:id="@+id/img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</LinearLayout>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值