1. 到百度云http://pan.baidu.com/s/1gf4l06v下载一个java文件和Volley的jar包
2. 先把jar包添加到你的lib工程的lib目录下 , java文件添加到你的任何一个package下,然后在java类改改包名,不报错就可以
3.然后开始写代码了
首先初始化你的UI,在布局文件添加一个ImageView进来
<RelativeLayout 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: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.example.myvolley.MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<ImageView
android:id="@+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/textView1"
android:layout_marginRight="108dp"
android:layout_marginTop="124dp"
android:src="@drawable/ic_launcher" />
</RelativeLayout>
然后在activity中写两句话即可就可以加载啦,是不是很简单呢?
package com.example.myvolley;
import org.json.JSONObject;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.ImageView;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
public class MainActivity extends ActionBarActivity {
private ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
// 异步加载图片
//Context context(上下文,一般默认传this或者getApplicationContext...等等即可), String ImageRequestUrl(请求图片地址),
// ImageView iv(要加载的图片位置), int defImage(找不到默认返回的图片,R.drawable.xxx), int errorImage(网络错误返回的图片,R.drawable.xxx格式)
String imageUrl = "http://img02.sogoucdn.com/app/a/100520024/dccd39c71b75d37d3d7345f4bd46c454";
ImageVolley.loadImage(this, imageUrl, iv, R.drawable.ic_launcher, R.drawable.ic_launcher);
}
private void init() {
iv = (ImageView) findViewById(R.id.iv);
}
}