三步解决网络稳定加载图片:使用到谷歌开源的Volley

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);
	}

}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值