1. AndroidManifest.xml 添加网络权限
<uses-permission android:name="android.permission.INTERNET" />
<!--usesCleartextTraffic 使用明文网络流量 不添加 API 28或更高级别的应用会报错-->
<application android:usesCleartextTraffic="true">
2. build.gradle 添加引用库
dependencies {
//HTTP 请求
implementation 'com.android.volley:volley:1.2.1'
//加载网络图片
implementation 'com.github.bumptech.glide:glide:4.13.2'
annotationProcessor 'com.github.bumptech.glide:compiler:4.13.2'
//上拉刷新下拉加载
implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.1.0"
}
3. Volley 的使用请求文本
3.1 布局文件 activty_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView"
android:textSize="16sp" />
</LinearLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
3.2 测试方法
//Volley 请求文本
private void stringRequest() {
String url = "http://www.baidu.com";
//String url = "https://pixabay.com/api/?key=30070990-cfc31c9f778ceeef4009d910d&q=yellow+flowers&image_type=photo";
TextView textView = findViewById(R.id.textView);
RequestQueue queue = Volley.newRequestQueue(this);
StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.e(TAG, response);
textView.setText(response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "onErrorResponse", error);
}
});
queue.add(stringRequest);
}
4. Volley 的使用请求显示图片
4.1 布局文件 activity_image.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/swipeRefreshLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/imageView"
android:layout_width="0dp"
android:layout_height="160dp"
app:layout_constraintBottom_toTopOf="@+id/imageView2"
app:layout_constraintEnd_toEndOf="@+id/imageView2"
app:layout_constraintStart_toStartOf="@+id/imageView2"
app:layout_constraintTop_toTopOf="parent"
tools:srcCompat="@tools:sample/avatars" />
<com.android.volley.toolbox.NetworkImageView
android:id="@+id/imageView2"
android:layout_width="0dp"
android:layout_height="160dp"
app:layout_constraintBottom_toTopOf="@+id/imageView3"
app:layout_constraintEnd_toEndOf="@+id/imageView3"
app:layout_constraintStart_toStartOf="@+id/imageView3"
app:layout_constraintTop_toBottomOf="@+id/imageView"
tools:srcCompat="@tools:sample/avatars" />
<ImageView
android:id="@+id/imageView3"
android:layout_width="0dp"
android:layout_height="160dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView2"
tools:srcCompat="@tools:sample/avatars" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
4.2 测试方法
//Volley 请求图片 2
private void imageNetworkRequest(String url) {
NetworkImageView imageView = findViewById(R.id.imageView2);
RequestQueue queue = Volley.newRequestQueue(this);
ImageLoader imageLoader = new ImageLoader(queue, new ImageLoader.ImageCache() {
private LruCache<String, Bitmap> cache = new LruCache<>(50);
@Nullable
@Override
public Bitmap getBitmap(String url) {
return cache.get(url);
}
@Override
public void putBitmap(String url, Bitmap bitmap) {
cache.put(url, bitmap);
}
});
imageView.setImageUrl(url, imageLoader);
}
//Volley 请求图片 1
private void imageRequest(String url) {
ImageView imageView = findViewById(R.id.imageView);
RequestQueue queue = Volley.newRequestQueue(this);
ImageLoader imageLoader = new ImageLoader(queue, new ImageLoader.ImageCache() {
private LruCache<String, Bitmap> cache = new LruCache<>(50);
@Nullable
@Override
public Bitmap getBitmap(String url) {
return cache.get(url);
}
@Override
public void putBitmap(String url, Bitmap bitmap) {
cache.put(url, bitmap);
}
});
imageLoader.get(url, new ImageLoader.ImageListener() {
@Override
public void onResponse(ImageLoader.ImageContainer response, boolean isImmediate) {
imageView.setImageBitmap(response.getBitmap());
}
@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "onErrorResponse", error);
}
});
}
5. Glide 的使用,测试方法
//加载图片三
private void imageGlideRequest(String url) {
ImageView imageView = findViewById(R.id.imageView3);
Glide.with(this)
.load(url)
.centerInside()
.listener(new RequestListener<Drawable>() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
if (refreshLayout.isRefreshing()) {
refreshLayout.setRefreshing(false);
}
return false;
}
@Override
public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
if (refreshLayout.isRefreshing()) {
refreshLayout.setRefreshing(false);
}
return false;
}
})
.into(imageView);
}
6. Swiperefreshlayout 下拉刷新,测试方法
public class MainActivity extends AppCompatActivity {
private String TAG = "My_Tag";
private SwipeRefreshLayout refreshLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image);
imageRequest(getUrl());
imageNetworkRequest(getUrl());
imageGlideRequest(getUrl());
swipeRefreshLayout();
}
//随机取到一张图片
private String getUrl() {
String url[] = {"https://pixabay.com/get/g07fe87b77f48e5c8e5feeea776649bddd1736eae915f292a7d1ca15e63ba07fee19a3617baa91f0e20ffb14735c519b8_640.jpg",
"https://pixabay.com/get/g25723945d392fc250698fd026b08a0af91c9685958cf872a7a90bb880f4eed23bca46139908ffdd894e4376e7dc174256b8667d0e23d90a4feea497b11f7bfc2_640.jpg",
"https://pixabay.com/get/gb262f27570217960266c37c66d22dcab167b820902079b5713f99f4bdb049897feed1190711f92ffe85a7ce9fedc3bcf78e52958151a46e21337fe8461d29425_640.jpg",
"https://pixabay.com/get/g024ffcdd19dd265dd12743250fcc5e018b67efc018f2ebeb76698b91e8c9e54f424485317e3a0e97bb801cc929ccdf0d762eaf9469befde9277af72e62bd7f61_640.jpg",
"https://pixabay.com/get/ga038ad002124f678c5558862b653a5b1814599f1683dc9e84249b902df76419ad7720ac58fab7e5e23653c35fed8ec80_640.jpg",
"https://pixabay.com/get/gbf6b1efe3a02668c078f6a6d8baecc659941edff08b0b7234ced89a249043d4efaac8c41321eb3c3edfc72bae0400648b209d5f567f4658796813b88add3856f_640.jpg"};
int index = new Random().nextInt(6);
return url[index];
}
//上拉下拉
private void swipeRefreshLayout() {
refreshLayout = findViewById(R.id.swipeRefreshLayout);
refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
imageRequest(getUrl());
imageNetworkRequest(getUrl());
imageGlideRequest(getUrl());
}
});
}
//加载图片三
private void imageGlideRequest(String url) {
ImageView imageView = findViewById(R.id.imageView3);
Glide.with(this)
.load(url)
.centerInside()
.listener(new RequestListener<Drawable>() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
if (refreshLayout.isRefreshing()) {
refreshLayout.setRefreshing(false);
}
return false;
}
@Override
public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
if (refreshLayout.isRefreshing()) {
refreshLayout.setRefreshing(false);
}
return false;
}
})
.into(imageView);
}
//Volley 请求图片 2
private void imageNetworkRequest(String url) {
NetworkImageView imageView = findViewById(R.id.imageView2);
RequestQueue queue = Volley.newRequestQueue(this);
ImageLoader imageLoader = new ImageLoader(queue, new ImageLoader.ImageCache() {
private LruCache<String, Bitmap> cache = new LruCache<>(50);
@Nullable
@Override
public Bitmap getBitmap(String url) {
return cache.get(url);
}
@Override
public void putBitmap(String url, Bitmap bitmap) {
cache.put(url, bitmap);
}
});
imageView.setImageUrl(url, imageLoader);
}
//Volley 请求图片 1
private void imageRequest(String url) {
ImageView imageView = findViewById(R.id.imageView);
RequestQueue queue = Volley.newRequestQueue(this);
ImageLoader imageLoader = new ImageLoader(queue, new ImageLoader.ImageCache() {
private LruCache<String, Bitmap> cache = new LruCache<>(50);
@Nullable
@Override
public Bitmap getBitmap(String url) {
return cache.get(url);
}
@Override
public void putBitmap(String url, Bitmap bitmap) {
cache.put(url, bitmap);
}
});
imageLoader.get(url, new ImageLoader.ImageListener() {
@Override
public void onResponse(ImageLoader.ImageContainer response, boolean isImmediate) {
imageView.setImageBitmap(response.getBitmap());
}
@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "onErrorResponse", error);
}
});
}
}
7. 效果图