|
17 | 17 | import com.light.core.Utils.MatrixUtil;
|
18 | 18 | import com.light.core.Utils.MemoryComputeUtil;
|
19 | 19 | import com.light.core.Utils.http.HttpDownLoader;
|
| 20 | +import com.light.core.listener.OnCompressFinishListener; |
20 | 21 |
|
21 | 22 | import java.io.BufferedOutputStream;
|
22 | 23 | import java.io.File;
|
|
32 | 33 | import io.reactivex.schedulers.Schedulers;
|
33 | 34 |
|
34 | 35 | public class NetActivity extends AppCompatActivity {
|
35 |
| - ImageView ivCompress; |
36 |
| - ImageView ivImage; |
37 |
| - TextView tvInfo; |
38 |
| - TextView tvInfo1; |
39 |
| - TextView tvInfo2; |
40 |
| - TextView tvSize; |
41 |
| - Button button1; |
42 |
| - Button button2; |
43 |
| - Button button3; |
44 |
| - String path1 = Environment.getExternalStorageDirectory().getAbsolutePath() + "/1.jpg";//压缩后 |
45 |
| - String path2 = Environment.getExternalStorageDirectory().getAbsolutePath() + "/2.jpg";//原图片 |
46 |
| - final static String info1 = "压缩后:\n高度:%d,宽度:%d,占用内存:%dKB"; |
47 |
| - final static String info2 = "原图片:\n高度:%d,宽度:%d,占用内存:%dKB"; |
48 |
| - ProgressDialog progressDialog; |
49 |
| - @Override |
50 |
| - protected void onCreate(Bundle savedInstanceState) { |
51 |
| - super.onCreate(savedInstanceState); |
52 |
| - setContentView(R.layout.activity_net); |
53 |
| - ivCompress = findViewById(R.id.image_compress); |
54 |
| - ivImage = findViewById(R.id.image); |
55 |
| - tvInfo = findViewById(R.id.tv_info); |
56 |
| - tvInfo1 = findViewById(R.id.tv_info1); |
57 |
| - tvInfo2 = findViewById(R.id.tv_info2); |
58 |
| - button1 = findViewById(R.id.btn1); |
59 |
| - button2 = findViewById(R.id.btn2); |
60 |
| - button3 = findViewById(R.id.btn3); |
61 |
| - tvSize = findViewById(R.id.tv_size); |
62 |
| - tvInfo1.setVisibility(View.VISIBLE); |
63 |
| - tvInfo2.setVisibility(View.VISIBLE); |
64 |
| - progressDialog = new ProgressDialog(this); |
65 |
| - progressDialog.setTitle("图片加载中"); |
66 |
| - progressDialog.setMessage("Loading..."); |
67 |
| - progressDialog.setCancelable(false); |
68 |
| - button1.setOnClickListener(new View.OnClickListener() { |
69 |
| - @Override |
70 |
| - public void onClick(View v) { |
71 |
| - progressDialog.show(); |
72 |
| - Uri uri = Uri.parse("http://img52.fooww.com:9999/group5/M01/00/99/ZyjCGlrtH9uEPlSIAAAAAN9Juaw140.jpg"); |
73 |
| - showImg(uri); |
74 |
| - } |
75 |
| - }); |
| 36 | + ImageView ivCompress; |
| 37 | + ImageView ivImage; |
| 38 | + TextView tvInfo; |
| 39 | + TextView tvInfo1; |
| 40 | + TextView tvInfo2; |
| 41 | + TextView tvSize; |
| 42 | + Button button1; |
| 43 | + Button button2; |
| 44 | + Button button3; |
| 45 | + String path1 = Environment.getExternalStorageDirectory().getAbsolutePath() + "/1.jpg";//压缩后 |
| 46 | + String path2 = Environment.getExternalStorageDirectory().getAbsolutePath() + "/2.jpg";//原图片 |
| 47 | + final static String info1 = "压缩后:\n高度:%d,宽度:%d,占用内存:%dKB"; |
| 48 | + final static String info2 = "原图片:\n高度:%d,宽度:%d,占用内存:%dKB"; |
| 49 | + ProgressDialog progressDialog; |
76 | 50 |
|
77 |
| - button2.setOnClickListener(new View.OnClickListener() { |
78 |
| - @Override |
79 |
| - public void onClick(View v) { |
80 |
| - progressDialog.show(); |
81 |
| - Uri uri = Uri.parse("https://raw.githubusercontent.com/JavaNoober/Light/master/pic2.jpg"); |
82 |
| - showImg(uri); |
83 |
| - } |
84 |
| - }); |
| 51 | + @Override |
| 52 | + protected void onCreate(Bundle savedInstanceState) { |
| 53 | + super.onCreate(savedInstanceState); |
| 54 | + setContentView(R.layout.activity_net); |
| 55 | + ivCompress = findViewById(R.id.image_compress); |
| 56 | + ivImage = findViewById(R.id.image); |
| 57 | + tvInfo = findViewById(R.id.tv_info); |
| 58 | + tvInfo1 = findViewById(R.id.tv_info1); |
| 59 | + tvInfo2 = findViewById(R.id.tv_info2); |
| 60 | + button1 = findViewById(R.id.btn1); |
| 61 | + button2 = findViewById(R.id.btn2); |
| 62 | + button3 = findViewById(R.id.btn3); |
| 63 | + tvSize = findViewById(R.id.tv_size); |
| 64 | + tvInfo1.setVisibility(View.VISIBLE); |
| 65 | + tvInfo2.setVisibility(View.VISIBLE); |
| 66 | + progressDialog = new ProgressDialog(this); |
| 67 | + progressDialog.setTitle("图片加载中"); |
| 68 | + progressDialog.setMessage("Loading..."); |
| 69 | + progressDialog.setCancelable(false); |
| 70 | + button1.setOnClickListener(new View.OnClickListener() { |
| 71 | + @Override |
| 72 | + public void onClick(View v) { |
| 73 | + progressDialog.show(); |
| 74 | + Uri uri = Uri.parse("http://img52.fooww.com:9999/group5/M01/00/99/ZyjCGlrtH9uEPlSIAAAAAN9Juaw140.jpg"); |
| 75 | + showImg(uri); |
| 76 | + } |
| 77 | + }); |
85 | 78 |
|
86 |
| - button3.setOnClickListener(new View.OnClickListener() { |
87 |
| - @Override |
88 |
| - public void onClick(View v) { |
89 |
| - progressDialog.show(); |
90 |
| - Uri uri = Uri.parse("https://raw.githubusercontent.com/JavaNoober/Light/master/pic3.jpg"); |
91 |
| - showImg(uri); |
92 |
| - } |
93 |
| - }); |
94 |
| - } |
| 79 | + button2.setOnClickListener(new View.OnClickListener() { |
| 80 | + @Override |
| 81 | + public void onClick(View v) { |
| 82 | + progressDialog.show(); |
| 83 | + Uri uri = Uri.parse("http://k.zol-img.com.cn/sjbbs/7692/a7691515_s.jpg"); |
| 84 | + showImg(uri); |
| 85 | + } |
| 86 | + }); |
95 | 87 |
|
96 |
| - private void showImg(Uri uri) { |
| 88 | + button3.setOnClickListener(new View.OnClickListener() { |
| 89 | + @Override |
| 90 | + public void onClick(View v) { |
| 91 | + progressDialog.show(); |
| 92 | + Uri uri = Uri.parse("http://pic31.nipic.com/20130705/9527735_231540074000_2.jpg"); |
| 93 | + showImg(uri); |
| 94 | + } |
| 95 | + }); |
| 96 | + } |
97 | 97 |
|
98 |
| - new Thread(new Runnable() { |
99 |
| - @Override |
100 |
| - public void run() { |
101 |
| - byte[] bytes = HttpDownLoader.downloadImage(uri); |
102 |
| - BitmapFactory.Options options = new BitmapFactory.Options(); |
103 |
| - Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length, options); |
104 |
| - File file = new File(path2); |
105 |
| - OutputStream output = null; |
106 |
| - try { |
107 |
| - output = new FileOutputStream(file); |
108 |
| - BufferedOutputStream bufferedOutput = new BufferedOutputStream(output); |
109 |
| - bufferedOutput.write(bytes); |
110 |
| - } catch (FileNotFoundException e) { |
111 |
| - e.printStackTrace(); |
112 |
| - } catch (IOException e) { |
113 |
| - e.printStackTrace(); |
114 |
| - } |
115 |
| - runOnUiThread(new Runnable() { |
116 |
| - @Override |
117 |
| - public void run() { |
118 |
| - progressDialog.dismiss(); |
119 |
| - float scaleX = MatrixUtil.getScale(1920, 1080, options.outWidth, options.outHeight); |
120 |
| - ivImage.setImageBitmap(new MatrixUtil.Build().scale(scaleX, scaleX).bitmap(bitmap).build()); |
121 |
| - Light.getInstance().compress(path2, path1); |
122 |
| - tvInfo2.setText(String.format(Locale.CHINA, info2, options.outHeight, |
123 |
| - options.outWidth, MemoryComputeUtil.getMemorySize(bitmap))); |
124 |
| - tvSize.setText("原图片文件大小:" + file.length() / 1024 +"KB;压缩后图片文件大小:" + |
125 |
| - new File(path1).length() / 1024 + "KB"); |
126 |
| - } |
127 |
| - }); |
128 |
| - } |
129 |
| - }).start(); |
130 |
| - Flowable.just(uri).compose(RxLight.compressForUriHttp()) |
131 |
| - .subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()) |
132 |
| - .subscribe(bitmap -> { |
133 |
| - progressDialog.dismiss(); |
134 |
| - ivCompress.setImageBitmap(bitmap); |
135 |
| - tvInfo1.setText(String.format(Locale.CHINA, info1, bitmap.getHeight(), |
136 |
| - bitmap.getWidth(), MemoryComputeUtil.getMemorySize(bitmap))); |
137 |
| - }); |
138 |
| - } |
| 98 | + private void showImg(Uri uri) { |
| 99 | + |
| 100 | + new Thread(new Runnable() { |
| 101 | + @Override |
| 102 | + public void run() { |
| 103 | + byte[] bytes = HttpDownLoader.downloadImage(uri); |
| 104 | + BitmapFactory.Options options = new BitmapFactory.Options(); |
| 105 | + Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length, options); |
| 106 | + File file = new File(path2); |
| 107 | + OutputStream output = null; |
| 108 | + try { |
| 109 | + output = new FileOutputStream(file); |
| 110 | + BufferedOutputStream bufferedOutput = new BufferedOutputStream(output); |
| 111 | + bufferedOutput.write(bytes); |
| 112 | + } catch (FileNotFoundException e) { |
| 113 | + e.printStackTrace(); |
| 114 | + } catch (IOException e) { |
| 115 | + e.printStackTrace(); |
| 116 | + } |
| 117 | + runOnUiThread(new Runnable() { |
| 118 | + @Override |
| 119 | + public void run() { |
| 120 | + progressDialog.dismiss(); |
| 121 | + float scaleX = MatrixUtil.getScale(1920, 1080, options.outWidth, options.outHeight); |
| 122 | + ivImage.setImageBitmap(new MatrixUtil.Build().scale(scaleX, scaleX).bitmap(bitmap).build()); |
| 123 | + Light.getInstance().compress(path2, path1); |
| 124 | + tvInfo2.setText(String.format(Locale.CHINA, info2, options.outHeight, |
| 125 | + options.outWidth, MemoryComputeUtil.getMemorySize(bitmap))); |
| 126 | + tvSize.setText("原图片文件大小:" + file.length() / 1024 + "KB;压缩后图片文件大小:" + |
| 127 | + new File(path1).length() / 1024 + "KB"); |
| 128 | + } |
| 129 | + }); |
| 130 | + } |
| 131 | + }).start(); |
| 132 | + Flowable.just(uri).compose(RxLight.compressForUriHttp()) |
| 133 | + .subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()) |
| 134 | + .subscribe(bitmap -> { |
| 135 | + progressDialog.dismiss(); |
| 136 | + ivCompress.setImageBitmap(bitmap); |
| 137 | + tvInfo1.setText(String.format(Locale.CHINA, info1, bitmap.getHeight(), |
| 138 | + bitmap.getWidth(), MemoryComputeUtil.getMemorySize(bitmap))); |
| 139 | + }); |
| 140 | + |
| 141 | + //缓存测试 |
| 142 | +// new Thread(() -> Light.getInstance().compressFromHttp(uri, true, new OnCompressFinishListener() { |
| 143 | +// @Override |
| 144 | +// public void onFinish(byte[] bytes) { |
| 145 | +// Bitmap bitmap = Light.getInstance().compress(bytes); |
| 146 | +// runOnUiThread(() -> { |
| 147 | +// progressDialog.dismiss(); |
| 148 | +// ivCompress.setImageBitmap(bitmap); |
| 149 | +// tvInfo1.setText(String.format(Locale.CHINA, info1, bitmap.getHeight(), |
| 150 | +// bitmap.getWidth(), MemoryComputeUtil.getMemorySize(bitmap))); |
| 151 | +// }); |
| 152 | +// } |
| 153 | +// |
| 154 | +// @Override |
| 155 | +// public void onError(Throwable throwable) { |
| 156 | +// |
| 157 | +// } |
| 158 | +// })).start(); |
| 159 | + } |
139 | 160 |
|
140 | 161 | }
|
0 commit comments