您的位置:首页 > 运维架构 > 网站架构

利用第3方开源框架 Volley ,实现图片,网站源码的加载

2015-11-23 16:07 681 查看
Android Volley 是一款很好的 Android 的开源框架,其对图片的加载弱于 Glide 开源框架。

Android Volley 的库jar包 Volley.jar 下载连接地址:http://download.csdn.net/detail/zhangphil/9053413

Android Volley的技术文档主页:https://developer.android.com/training/volley/index.html

Android Volley的开源代码库官方主页:https://android.googlesource.com/platform/frameworks/volley

将 jar 包放入 libs 目录下后,现在只是一个简单示例:

activity_main.xml :

<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.android.volleytest.MainActivity" >

<!-- <ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"/> -->

<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollHorizontally="true"/>

</RelativeLayout>


MainActivity.java :

package com.android.volleytest;

import com.android.volley.RequestQueue;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends Activity {

//	ImageView image;

TextView text;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//		image = (ImageView) findViewById(R.id.image);

text = (TextView)findViewById(R.id.text);

addImage();

addString();
}
// 添加图片
private void addImage() {
// 队列
RequestQueue queue = Volley.newRequestQueue(this);

ImageRequest image1 = new ImageRequest("http://www.8mei.cc/uploads/allimg/c120112/13263K3004Z-124106.jpg",
new Listener<Bitmap>() {
@Override
public void onResponse(Bitmap response) {
//		image.setImageBitmap(response);

}
}, 0, 0, Config.RGB_565, new ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// TODO Auto-generated method stub

}

});

queue.add(image1);
}

private void addString(){
RequestQueue queue = Volley.newRequestQueue(this);

StringRequest str = new StringRequest(
"http://www.baidu.com",
new Listener<String>() {
@Override
public void onResponse(String response) {
text.setText(response);
}
},
new ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {

}

});
queue.add(str);
}

}


在 AndroidManifest.xml 中注册权限:

<uses-permission android:name="android.permission.INTERNET"/>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: