初始网络加载比较耗时,读者可以更加优化代码
xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#465"
android:orientation="vertical" android:layout_width="match_parent"
android:gravity="center"
android:layout_height="match_parent">
<ImageView
android:id="@+id/bitmap_image"
android:layout_gravity="center"
android:layout_width="310dp"
android:layout_height="200dp">
</ImageView>
<Button
android:id="@+id/img_button"
android:textColor="#fff"
android:background="#5456"
android:textSize="15sp"
android:text="下一张"
android:layout_marginTop="20dp"
android:layout_width="160dp"
android:layout_height="50dp">
</Button>
</LinearLayout>
注意加上权限:
注意加上权限
<uses-permission android:name="android.permission.WRITE_HISTORY_BOOKMARKS>"></uses-permission>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
activity:
package fengxing.primer.shouce.net_image;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.io.IOException;
import java.io.InputStream;
import java.util.LinkedList;
import java.util.List;
import fengxing.primer.shouce.R;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class NetImagesActivity extends Activity {
private final static int SHOW_NET_IMAGE = 0;
private final static String TAG="NetImagesActivity";
private List<Bitmap> bitmapList = new LinkedList<Bitmap>();
private Button button;
private ImageView imageView;
private List<String> urlList = new LinkedList<String>();
private OkHttpClient okHttpClient = new OkHttpClient();
private InputStream inputStream = null;
private Response response=null;
private Request request=null;
private int index = 0;
private Handler handler = new Handler(){
@Override
public void handleMessage(@NonNull Message msg) {
super.handleMessage(msg);
switch (msg.what){
case SHOW_NET_IMAGE:
{
switchImages();
}
break;
default:
break;
}
}
};
/**
* 初始化界面
* @param savedInstanceState
*/
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_net_image);
initData();
initView();
loadImages();
}
/**
* 切换图片
*/
private void switchImages() {
if (bitmapList.size()>0) {
if (bitmapList.size()==urlList.size()) {
Toast.makeText(NetImagesActivity.this, "第: " + index + "张", Toast.LENGTH_SHORT).show();
if (index < bitmapList.size()) {
imageView.setImageBitmap(bitmapList.get(index));
index++;
if (index >= bitmapList.size()) {
index = 0;
}
}
}
}else{
Toast.makeText(NetImagesActivity.this,"图片正在加载...",Toast.LENGTH_SHORT).show();
}
}
/**
* 加载网络图片
*/
private void loadImages() {
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0 ; i < urlList.size();i++){
request = new Request.Builder()
.get()
.url(urlList.get(i))
.build();
try {
response = okHttpClient.newCall(request).execute();
if (response.isSuccessful()){
inputStream = response.body().byteStream();
bitmapList.add(BitmapFactory.decodeStream(inputStream));
}
} catch (IOException e) {
e.printStackTrace();
}
}
button.setText("加载完成(显示图片)");
}
}).start();
}
/**
* 初始化控件
*/
private void initView() {
button = findViewById(R.id.img_button);
button.setText("图片加载中...");
imageView=findViewById(R.id.bitmap_image);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Message message = new Message();
message.what=SHOW_NET_IMAGE;
handler.sendMessage(message);
}
});
}
/**
* 初始化数据
*/
private void initData() {
urlList.add("https://i.loli.net/2019/10/07/CcSrLmoInxZtedp.jpg");
urlList.add("https://i.loli.net/2019/10/07/nfdZYmVGzawI1yP.jpg");
urlList.add("https://i.loli.net/2019/10/07/hewX2FuEfDmKQTI.jpg");
}
/**
* 关闭资源
*/
@Override
protected void onDestroy() {
super.onDestroy();
if (inputStream!=null){
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 关闭activity之前保存数据
*/
@Override
protected void onStop() {
super.onStop();
}
}
效果图