初始Volley框架 ImageLoader请求Url 并加载

本篇博客内容不多,而且与之前提到的大同小异,一样是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);
    }
}

注释写的挺详细的了,现在网络编程很普遍,也很普通,对于单张图片的请求没有过多的叙述,希望对有需求的朋友有所帮助。

记得添加网络权限哦~



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值