阅读 69

RXjava方便实现不同需求的变更(链式编程)

1联网权限
2dependencies

   implementation 'io.reactivex.rxjava2:rxandroid:2.0.1'
    implementation 'io.reactivex.rxjava2:rxjava:2.0.7'
复制代码

3

package com.hisense;

import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
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;
                    }
                })
                //加水印
                .map(new Function<Bitmap, Bitmap>() {
                    @SuppressLint("ResourceAsColor") @NonNull @Override
                    public Bitmap apply(@NonNull Bitmap bitmap) throws Exception {
                        Paint paint = new Paint();
                        paint.setColor(R.color.purple_200);
                        paint.setTextSize(150);
                        Bitmap 青岛印 = drawTextToBitmap(bitmap, "青岛印", paint, 100, 200);

                        return 青岛印;
                    }
                })
                .map(new Function<Bitmap, Bitmap>() {
                    @NonNull @Override
                    public Bitmap apply(@NonNull Bitmap bitmap) throws Exception {
                        System.out.println("日志记录即将显示水印bitmap = " + bitmap);
                        return bitmap;
                    }
                })

                //给联网耗时操作分配异步线程  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();
                        }

                    }
                });

    }

    // 图片上绘制文字 加水印
    private final Bitmap drawTextToBitmap(Bitmap bitmap, String text, Paint paint, int paddingLeft, int paddingTop) {
        Bitmap.Config bitmapConfig = bitmap.getConfig();

        paint.setDither(true); // 获取跟清晰的图像采样
        paint.setFilterBitmap(true);// 过滤一些
        if (bitmapConfig == null) {
            bitmapConfig = Bitmap.Config.ARGB_8888;
        }
        bitmap = bitmap.copy(bitmapConfig, true);
        Canvas canvas = new Canvas(bitmap);

        canvas.drawText(text, paddingLeft, paddingTop, paint);
        return bitmap;
    }
}
复制代码

4

<?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.hisense.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>
复制代码
文章分类
Android
文章标签