Rx
| implementation 'io.rectivex.rxjava2:rxandroid:2.0.1' |
| implementation 'io.rectivex.rxjava2:rxjava:2.0.7' |
Rx思维下载图片
| <uses-permission android:name="android.permission.INTERNET"/> |
| <?xml version="1.0" encoding="utf-8"?> |
| <LinearLayout android:layout_width="match_parent" |
| android:layout_height="match_parent" |
| android:orientation="vertical" |
| xmlns:android="http://schemas.android.com/apk/res/android"> |
| |
| <ImageView |
| android:layout_width="100dp" |
| android:layout_height="100dp" |
| android:id="@+id/iv"/> |
| |
| <Button |
| android:layout_width="match_parent" |
| android:layout_height="wrap_content" |
| android:text="显示图片下载" |
| android:onClick="showImageAction"/> |
| |
| <Button |
| android:layout_width="match_parent" |
| android:layout_height="wrap_content" |
| android:text="常用操作符" |
| android:onClick="action"/> |
| |
| </LinearLayout> |
| package com.example.rxjava; |
| |
| import androidx.appcompat.app.AppCompatActivity; |
| |
| import android.annotation.SuppressLint; |
| import android.app.ProgressDialog; |
| import android.graphics.Bitmap; |
| import android.graphics.BitmapFactory; |
| import android.graphics.Canvas; |
| import android.graphics.Color; |
| import android.graphics.Paint; |
| import android.os.Bundle; |
| import android.util.Log; |
| import android.view.View; |
| import android.widget.ImageView; |
| import android.widget.ProgressBar; |
| |
| import java.io.InputStream; |
| import java.net.HttpURLConnection; |
| import java.net.MalformedURLException; |
| import java.net.URL; |
| |
| import io.reactivex.Observable; |
| import io.reactivex.Observer; |
| import io.reactivex.Scheduler; |
| import io.reactivex.android.schedulers.AndroidSchedulers; |
| import io.reactivex.disposables.Disposable; |
| import io.reactivex.functions.Consumer; |
| import io.reactivex.functions.Function; |
| import io.reactivex.schedulers.Schedulers; |
| |
| public class MainActivity extends AppCompatActivity { |
| |
| |
| private final static String PATH = "https://i1.hdslb.com/bfs/face/2eac165893715bfa5a35f76a0f94e5a3a99c16d1.jpg@160w_160h_1c.webp"; |
| |
| |
| private ProgressDialog progressDialog; |
| |
| private ImageView imageView; |
| |
| @Override |
| protected void onCreate(Bundle savedInstanceState) { |
| super.onCreate(savedInstanceState); |
| setContentView(R.layout.activity_main); |
| |
| imageView = findViewById(R.id.iv); |
| } |
| |
| public void showImageAction(View view) { |
| |
| |
| Observable.just(PATH) |
| |
| |
| |
| .map(new Function<String, Bitmap>() { |
| @Override |
| public Bitmap apply(String s) throws Exception { |
| try { |
| URL url = new URL(s); |
| HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); |
| httpURLConnection.setConnectTimeout(5000); |
| int responseCode = httpURLConnection.getResponseCode(); |
| if(responseCode == HttpURLConnection.HTTP_OK){ |
| InputStream inputStream = httpURLConnection.getInputStream(); |
| Bitmap bitmap = BitmapFactory.decodeStream(inputStream); |
| return bitmap; |
| } |
| } catch (MalformedURLException e) { |
| e.printStackTrace(); |
| } finally { |
| } |
| return null; |
| } |
| }) |
| |
| |
| .map(new Function<Bitmap, Bitmap>() { |
| @Override |
| public Bitmap apply(Bitmap bitmap) throws Exception { |
| Paint paint = new Paint(); |
| paint.setColor(Color.RED); |
| paint.setTextSize(80); |
| Bitmap bitmap1 = drawTextToBitmap(bitmap, "哈哈", paint, 80, 80); |
| return bitmap1; |
| } |
| }) |
| |
| |
| |
| |
| .subscribeOn(Schedulers.io()) |
| |
| |
| .observeOn(AndroidSchedulers.mainThread()) |
| |
| |
| .subscribe( |
| |
| new Observer<Bitmap>() { |
| |
| |
| @Override |
| public void onSubscribe(Disposable d) { |
| |
| progressDialog = new ProgressDialog(MainActivity.this); |
| progressDialog.setTitle("正在加载图片"); |
| progressDialog.show(); |
| } |
| |
| |
| |
| @Override |
| public void onNext(Bitmap bitmap) { |
| imageView.setImageBitmap(bitmap); |
| } |
| |
| |
| @Override |
| public void onError(Throwable e) { |
| |
| } |
| |
| |
| |
| @Override |
| public void onComplete() { |
| progressDialog.dismiss(); |
| } |
| }); |
| |
| } |
| |
| @SuppressLint("CheckResult") |
| public void action(View view) { |
| String[] strings = {"aaa", "bbb", "ccc"}; |
| Observable.fromArray(strings) |
| .subscribe(new Consumer<String>() { |
| @Override |
| public void accept(String s) throws Exception { |
| Log.d("wmj", "accept: " + s); |
| } |
| }); |
| |
| } |
| |
| 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; |
| } |
| } |
解决服务器响应问题
| private int id; |
| private String name; |
| private SuccessBean data; |
| private int code; |
| private String message; |
| package com.example.rxjava2; |
| |
| import io.reactivex.Observer; |
| import io.reactivex.disposables.Disposable; |
| |
| public abstract class CustomObserver implements Observer<ResponseResult> { |
| |
| public abstract void success(SuccessBean successBean); |
| public abstract void error(String message); |
| |
| @Override |
| public void onSubscribe(Disposable d) { |
| |
| } |
| |
| @Override |
| public void onNext(ResponseResult responseResult) { |
| if(responseResult.getData() == null){ |
| error(responseResult.getMessage() + "请求失败"); |
| }else{ |
| success(responseResult.getData()); |
| } |
| } |
| |
| @Override |
| public void onError(Throwable e) { |
| error(e.getMessage() + "错误详情"); |
| } |
| |
| @Override |
| public void onComplete() { |
| |
| } |
| } |
| package com.example.rxjava2; |
| |
| import io.reactivex.Observable; |
| |
| public class LoginEngine { |
| |
| public static Observable<ResponseResult> login(String name, String pwd){ |
| ResponseResult responseResult = new ResponseResult(); |
| if("wmj".equals(name) && "666".equals(pwd)){ |
| |
| SuccessBean successBean = new SuccessBean(); |
| successBean.setId(11111); |
| successBean.setName("wmj登录成功"); |
| |
| responseResult.setData(successBean); |
| responseResult.setCode(200); |
| responseResult.setMessage("登录成功"); |
| }else{ |
| responseResult.setData(null); |
| responseResult.setCode(404); |
| responseResult.setMessage("登录失败"); |
| } |
| |
| |
| return Observable.just(responseResult); |
| } |
| } |
| package com.example.rxjava2; |
| |
| import androidx.appcompat.app.AppCompatActivity; |
| |
| import android.os.Bundle; |
| import android.util.Log; |
| |
| public class MainActivity extends AppCompatActivity { |
| |
| @Override |
| protected void onCreate(Bundle savedInstanceState) { |
| super.onCreate(savedInstanceState); |
| setContentView(R.layout.activity_main); |
| |
| LoginEngine.login("wmj", "666") |
| .subscribe(new CustomObserver() { |
| @Override |
| public void success(SuccessBean successBean) { |
| Log.d("wmj", "成功bean:" + successBean.toString()); |
| } |
| |
| @Override |
| public void error(String message) { |
| Log.d("wmj", "错误信息: " + message); |
| } |
| }); |
| } |
| } |
本文作者:n1ce2cv
本文链接:https://www.cnblogs.com/sprinining/p/14955071.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步