本篇博客内容不多,而且与之前提到的大同小异,一样是volley的入门级操作。
这里应博友的要求,写了一个volley中ImageLoader加载url地址的demo。如下:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.ynu.imageloaderforvolley.MainActivity">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#585858"
android:textColor="#fff"
android:text="ImageLoader"
android:id="@+id/ImageLoader"
android:onClick="mImageLoader"/>
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:id="@+id/imageView"/>
</LinearLayout>
MainActivity.java
import android.graphics.Bitmap;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;
public class MainActivity extends AppCompatActivity {
RequestQueue queue;
ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv= (ImageView) findViewById(R.id.imageView);
queue= Volley.newRequestQueue(MainActivity.this);
}
//Button onclick事件
public void mImageLoader(View view){
ImageLoader imageLoader=new ImageLoader(queue,new ImageLoader.ImageCache() {
@Override
public void putBitmap(String url, Bitmap bitmap) {
}
@Override
public Bitmap getBitmap(String url) {
return null;
}
});
//getImageListener后边两个参 第二个指默认显示的图片 第三个指回调失败时调用的图片
ImageLoader.ImageListener imageListener=ImageLoader.getImageListener(iv,R.mipmap.ic_launcher,R.mipmap.ic_launcher);
//加载图片→imageview
imageLoader.get("http://tb.himg.baidu.com/sys/portrait/item/70814439?t=1457174442",imageListener);
}
}
注释写的挺详细的了,现在网络编程很普遍,也很普通,对于单张图片的请求没有过多的叙述,希望对有需求的朋友有所帮助。
记得添加网络权限哦~