private final String PATH = "http://pic33.nipic.com/20131007/13639685_123501617185_2.jpg";
/**
* todo 不使用RxJava去下载图片
* @param view
*/
public void r03(View view) {
progressDialog = new ProgressDialog(this);
progressDialog.setMessage("正在下载中...");
progressDialog.show();
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(2000);
URL url = new URL(PATH);
URLConnection urlConnection = url.openConnection();
HttpURLConnection httpURLConnection = (HttpURLConnection) urlConnection;
httpURLConnection.setConnectTimeout(5000);
int responseCode = httpURLConnection.getResponseCode();
if (HttpURLConnection.HTTP_OK == responseCode) {
Bitmap bitmap = BitmapFactory.decodeStream(httpURLConnection.getInputStream());
Message message = handler.obtainMessage();
message.obj = bitmap;
handler.sendMessage(message);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
private Handler handler = new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(Message msg) {
Bitmap bitmap = (Bitmap) msg.obj;
imageView.setImageBitmap(bitmap);
// 隐藏加载框
if (progressDialog != null)
progressDialog.dismiss();
return false;
}
});
/**
* todo 使用RxJava去下载图片
* @param view
*/
public void r04(View view) {
// 起点
// 上游 被观察者 Observable
Observable.just(PATH) // 内部发射
// String Path 变换 Bitmap
.map(new Function<String, Bitmap>() {
@Override
public Bitmap apply(String s) throws Exception {
try {
Thread.sleep(2000);
URL url = new URL(PATH);
URLConnection urlConnection = url.openConnection();
HttpURLConnection httpURLConnection = (HttpURLConnection) urlConnection;
httpURLConnection.setConnectTimeout(5000);
int responseCode = httpURLConnection.getResponseCode();
if (HttpURLConnection.HTTP_OK == responseCode) {
Bitmap bitmap = BitmapFactory.decodeStream(httpURLConnection.getInputStream());
return bitmap;
}
} catch (Exception e) {
e.printStackTrace();
}
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(30);
Bitmap bitmapSuccess = drawTextToBitmap(bitmap, "同学们大家好", paint, 60, 60);
return bitmapSuccess;
}
})
// 比如:增加一个 日志纪录功能,只需要添加要给 变换操作符
.map(new Function<Bitmap, Bitmap>() {
@Override
public Bitmap apply(Bitmap bitmap) throws Exception {
Log.d(TAG, "apply: 下载的Bitmap 是这个样子的" + bitmap);
return bitmap;
}
})
.subscribeOn(Schedulers.io()) // todo 给上游分配 异步线程
.observeOn(AndroidSchedulers.mainThread()) // todo 给下游分配 主线程
.subscribe(new Observer<Bitmap>() { // 下游
@Override
public void onSubscribe(Disposable d) {
progressDialog = new ProgressDialog(MainActivity8.this);
progressDialog.setMessage("RxJava下载图片中..");
progressDialog.show();
}
@Override
public void onNext(Bitmap bitmap) {
if (imageView != null)
imageView.setImageBitmap(bitmap);
}
@Override
public void onError(Throwable e) { // 发生异常
// if (imageView != null)
// imageView.setImageResource(R.mipmap.ic_launcher); // 下载错误的图片
}
@Override
public void onComplete() { // 终点
if (progressDialog != null)
progressDialog.dismiss();
}
});
}
//图片上绘制文字
private 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;
}
网友评论