RxJava再学习系列一(1)

new Thread(() -> {

try {

URL url = new URL(PATH);

HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();

httpURLConnection.setConnectTimeout(5000);

//设置请求方法

httpURLConnection.setRequestMethod(“GET”);

int responseCode = httpURLConnection.getResponseCode(); // 才开始 request

if (responseCode == HttpURLConnection.HTTP_OK) {

InputStream inputStream = httpURLConnection.getInputStream();

Bitmap bitmap = BitmapFactory.decodeStream(inputStream);

Message message = handler.obtainMessage();

message.obj = bitmap;

handler.sendMessage(message);

}

} catch (Exception e) {

e.printStackTrace();

}

}).start();

}

private final Handler handler = new Handler(new Handler.Callback() {

@Override

public boolean handleMessage(@NonNull Message msg) {

Bitmap bitmap = (Bitmap) msg.obj;

image.setImageBitmap(bitmap);

if (progressDialog != null) progressDialog.dismiss();

return false;

}

});

public void rxJavaDownloadImageAction(View view) {

// 起点

Observable.just(PATH) // 内部会分发 PATH Stirng // TODO 第二步

// TODO 第三步

.map(new Function<String, Bitmap>() {

@Override

public Bitmap apply(@NotNull String s) throws Exception {

Log.i(TAG, "apply: ");

URL url = new URL(PATH_RX);

HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();

httpURLConnection.setConnectTimeout(5000);

//设置请求方法

httpURLConnection.setRequestMethod(“GET”);

int responseCode = httpURLConnection.getResponseCode(); // 才开始 request

if (responseCode == HttpURLConnection.HTTP_OK) {

InputStream inputStream = httpURLConnection.getInputStream();

Bitmap bitmap = BitmapFactory.decodeStream(inputStream);

return bitmap;

}

return null;

}

})

.subscribeOn(Schedulers.io()) //给上面代码分配异步线程

.observeOn(AndroidSchedulers.mainThread()) //给下面代码分配主线程

// 订阅 起点 和 终点 订阅起来

.subscribe(new Observer() {

@Override

public void onSubscribe(@NotNull Disposable d) {

Log.i(TAG, "onSubscribe: ");

// 预备 开始 要分发

// TODO 第一步

progressDialog = new ProgressDialog(DownloadActivity.this);

progressDialog.setTitle(“download run”);

progressDialog.show();

}

// TODO 第四步

// 拿到事件

@Override

public void onNext(@NotNull Bitmap bitmap) {

Log.i(TAG, "onNext: ");

ivGlide.setImageBitmap(bitmap);

}

@Override

public void onError(@NotNull Throwable e) {

}

// TODO 第五步

// 完成事件

@Override

public void onComplete() {

Log.i(TAG, "onComplete: ");

if (progressDialog != null)

progressDialog.dismiss();

}

});

}

}

1.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”>

<Button

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:onClick=“downloadImageAction”

android:text=“传统方式下载图片功能” />

<Button

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:onClick=“rxJavaDownloadImageAction”

android:text=“RxJava方式下载图片功能” />

<ImageView

android:id=“@+id/image”

android:layout_width=“match_parent”

android:layout_height=“200dp” />

<ImageView

android:id=“@+id/iv_glide”

android:layout_width=“match_parent”

android:layout_height=“200dp” />

2、代码实战:封转线程切换


public class DownloadActivity extends AppCompatActivity {

// 打印logcat日志的标签

private static final String TAG = “DownloadActivity”;

// 网络图片的链接地址

private final static String PATH = “http://bpic.588ku.com/element_origin_min_pic/16/10/29/2ac8e99273bc079e40a8dc079ca11b1f.jpg”;

private final static String PATH_RX = “http://bpic.588ku.com/element_origin_min_pic/17/06/13/5c5a1442f0ec72e59829ee10d891f224.jpg%21r650”;

// 弹出加载框

private ProgressDialog progressDialog;

// ImageView控件,用来显示结果图像

private ImageView image;

private ImageView ivGlide;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_download);

image = findViewById(R.id.image);

ivGlide = findViewById(R.id.iv_glide);

// Glide.with(this).load(PATH).into(ivGlide);

}

// 零零散散 麻烦 思维

public void downloadImageAction(View view) {

progressDialog = new ProgressDialog(this);

progressDialog.setTitle(“下载图片中…”);

progressDialog.show();

new Thread(() -> {

try {

URL url = new URL(PATH);

HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();

httpURLConnection.setConnectTimeout(5000);

//设置请求方法

httpURLConnection.setRequestMethod(“GET”);

int responseCode = httpURLConnection.getResponseCode(); // 才开始 request

if (responseCode == HttpURLConnection.HTTP_OK) {

InputStream inputStream = httpURLConnection.getInputStream();

Bitmap bitmap = BitmapFactory.decodeStream(inputStream);

Message message = handler.obtainMessage();

message.obj = bitmap;

handler.sendMessage(message);

}

} catch (Exception e) {

e.printStackTrace();

}

}).start();

}

private final Handler handler = new Handler(new Handler.Callback() {

@Override

public boolean handleMessage(@NonNull Message msg) {

Bitmap bitmap = (Bitmap) msg.obj;

image.setImageBitmap(bitmap);

if (progressDialog != null) progressDialog.dismiss();

return false;

}

});

/**

  • 封装我们的操作

  • Upstream 上游

  • Downstream 下游

*/

public final static ObservableTransformer<UD, UD> rxud() {

return new ObservableTransformer<UD, UD>() {

@Override

public ObservableSource apply(Observable upstream) {

return upstream.subscribeOn(Schedulers.io()) // 给上面代码分配异步线程

.observeOn(AndroidSchedulers.mainThread()) // 给下面代码分配主线程;

.map(new Function<UD, UD>() {

@Override

public UD apply(UD ud) throws Exception {

Log.d(TAG, “apply: 我监听到你了,居然在执行”);

return ud;

}

});

// …

}

};

}

public void rxJavaDownloadImageAction(View view) {

// 起点

Observable.just(PATH) // 内部会分发 PATH Stirng // TODO 第二步

// TODO 第三步

.map(new Function<String, Bitmap>() {

@Override

public Bitmap apply(@NotNull String s) throws Exception {

Log.i(TAG, "apply: ");

URL url = new URL(PATH_RX);

HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();

httpURLConnection.setConnectTimeout(5000);

//设置请求方法

httpURLConnection.setRequestMethod(“GET”);

int responseCode = httpURLConnection.getResponseCode(); // 才开始 request

if (responseCode == HttpURLConnection.HTTP_OK) {

InputStream inputStream = httpURLConnection.getInputStream();

Bitmap bitmap = BitmapFactory.decodeStream(inputStream);

return bitmap;

}

return null;

}

})

// 图片上绘制文字 加水印

.map(new Function<Bitmap, Bitmap>() {

@Override

public Bitmap apply(Bitmap bitmap) throws Exception {

Paint paint = new Paint();

paint.setTextSize(88);

paint.setColor(Color.RED);

return drawTextToBitmap(bitmap, “同学们大家好”, paint, 88, 88);

}

})

// 每次都写这个线程切换比较麻烦,可以考虑进行封装

// .subscribeOn(Schedulers.io()) //给上面代码分配异步线程

// .observeOn(AndroidSchedulers.mainThread()) //给下面代码分配主线程

// 替代上面两行代码

.compose(rxud())

// 订阅 起点 和 终点 订阅起来

.subscribe(new Observer() {

@Override

public void onSubscribe(@NotNull Disposable d) {

Log.i(TAG, "onSubscribe: ");

// 预备 开始 要分发

// TODO 第一步

progressDialog = new ProgressDialog(DownloadActivity.this);

progressDialog.setTitle(“download run”);

progressDialog.show();
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则近万的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

img

img

img

img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)

最后

我一直以来都有整理练习大厂面试题的习惯,有随时跳出舒服圈的准备,也许求职者已经很满意现在的工作,薪酬,觉得习惯而且安逸。

不过如果公司突然倒闭,或者部门被裁减,还能找到这样或者更好的工作吗?

我建议各位,多刷刷面试题,知道最新的技术,每三个月可以去面试一两家公司,因为你已经有不错的工作了,所以可以带着轻松的心态去面试,同时也可以增加面试的经验。

我可以将最近整理的一线互联网公司面试真题+解析分享给大家,大概花了三个月的时间整理2246页,帮助大家学习进步。

由于篇幅限制,文档的详解资料太全面,细节内容太多,所以只把部分知识点截图出来粗略的介绍,每个小节点里面都有更细化的内容!以下是部分内容截图:

部分目录截图

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

mg src=“https://i-blog.csdnimg.cn/blog_migrate/1f6bcc3bcb7ce9450d6f28b2f6235343.jpeg” />

最后

我一直以来都有整理练习大厂面试题的习惯,有随时跳出舒服圈的准备,也许求职者已经很满意现在的工作,薪酬,觉得习惯而且安逸。

不过如果公司突然倒闭,或者部门被裁减,还能找到这样或者更好的工作吗?

我建议各位,多刷刷面试题,知道最新的技术,每三个月可以去面试一两家公司,因为你已经有不错的工作了,所以可以带着轻松的心态去面试,同时也可以增加面试的经验。

我可以将最近整理的一线互联网公司面试真题+解析分享给大家,大概花了三个月的时间整理2246页,帮助大家学习进步。

由于篇幅限制,文档的详解资料太全面,细节内容太多,所以只把部分知识点截图出来粗略的介绍,每个小节点里面都有更细化的内容!以下是部分内容截图:

[外链图片转存中…(img-SSZIr9Bn-1713408564729)]

[外链图片转存中…(img-63jRFOF1-1713408564730)]

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值