一、新建PictureLoader
public class PictureLoader {
private ImageView loadImg;
private String imgUrl;
private byte[] picByte;
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what == 0x123) {
if (picByte != null) {
Bitmap bitmap = BitmapFactory.decodeByteArray(picByte, 0, picByte.length);
loadImg.setImageBitmap(bitmap);
}
}
}
};
public void load(ImageView loadImg, String imgUrl) {
this.loadImg = loadImg;
this.imgUrl = imgUrl;
Drawable drawable = loadImg.getDrawable();
if(drawable != null && drawable instanceof BitmapDrawable) {
Bitmap bitmap = ((BitmapDrawable)drawable).getBitmap();
if(bitmap != null && !bitmap.isRecycled()) {
bitmap.recycle();
}
}
new Thread(runnable).start();
}
Runnable runnable = new Runnable() {
@Override
public void run() {
try {
URL url = new URL(imgUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setReadTimeout(10000);
if (conn.getResponseCode() == 200) {
InputStream in = conn.getInputStream();
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] bytes = new byte[1024];
int length = -1;
while ((length = in.read(bytes)) != -1) {
out.write(bytes, 0, length);
}
picByte = out.toByteArray();
in.close();
out.close();
handler.sendEmptyMessage(0x123);
}
} catch (IOException e) {
e.printStackTrace();
}
}
};
}
二、新建Activity界面
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button showBtn;
private ImageView showImg;
private ArrayList<String> urls;
private int curPos=0;
private PictureLoader loader;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
showBtn=(Button)findViewById(R.id.showBtn);
showImg=(ImageView)findViewById(R.id.img_show);
loader=new PictureLoader();
showBtn.setOnClickListener(this);
urls=new ArrayList<>();
urls.add("https://img1.baidu.com/it/u=2613963793,934854936&fm=253&fmt=auto&app=120&f=JPEG?w=1280&h=800");
urls.add("https://img2.baidu.com/it/u=2453741982,2770329727&fm=253&fmt=auto&app=120&f=JPEG?w=1280&h=800");
urls.add("https://img0.baidu.com/it/u=494561658,4056512075&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=500");
urls.add("https://img2.baidu.com/it/u=2389087729,70628867&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=750");
urls.add("https://k.sinaimg.cn/n/sinakd10100/50/w660h990/20240323/d50c-5faa98beb0d96d35c6ae9444d98b3c0d.jpg/w700d1q75cms.jpg");
urls.add("https://img0.baidu.com/it/u=2673688411,90488494&fm=253&fmt=auto&app=138&f=JPEG?w=480&h=800");
urls.add(" https://img1.baidu.com/it/u=1682400140,1262357671&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500");
urls.add("https://img1.baidu.com/it/u=2064828371,3291434219&fm=253&fmt=auto&app=138&f=JPEG?w=360&h=360");
urls.add("https://img2.baidu.com/it/u=2148980033,2323997411&fm=253&fmt=auto&app=138&f=JPEG?w=759&h=500");
}
@Override
public void onClick(View v) {
switch (v.getId())
{
case R.id.showBtn:
if(curPos>9){
curPos=0;
}
Logger.d(urls.get(curPos));
loader.load(showImg,urls.get(curPos));
curPos++;
break;
}
}
}