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

来源:互联网 发布:计算机四级有用吗 知乎 编辑:程序博客网 时间:2024/04/30 13:36

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;@Overrideprotected 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>() {@Overridepublic void onResponse(Bitmap response) {//image.setImageBitmap(response);}}, 0, 0, Config.RGB_565, new ErrorListener() {@Overridepublic 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>() {@Overridepublic void onResponse(String response) {text.setText(response);}}, new ErrorListener() {@Overridepublic void onErrorResponse(VolleyError error) {}});queue.add(str);}}

在 AndroidManifest.xml 中注册权限:

<uses-permission android:name="android.permission.INTERNET"/>




0 0
原创粉丝点击