Volley_ImageLoader

package com.example.volley_1;


import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.Volley;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageView;

/** * @author HD * @date 2015-11-23 * @package_name com.example.volley_1 * @file_name MainActivity.java */
public class MainActivity extends Activity {
    private static final String TAG = "MainActivity";
    private ImageView imagaView;
    private NetworkImageView networkimageview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        RequestQueue mQueue = Volley.newRequestQueue(MainActivity.this);
        imagaView = (ImageView) findViewById(R.id.imageView);
        ImageLoader imageLoader = new ImageLoader(mQueue, new BitmapCache());
        networkimageview = (NetworkImageView) findViewById(R.id.network_image_view);
        networkimageview.setDefaultImageResId(R.drawable.ic_launcher);
        networkimageview.setErrorImageResId(R.drawable.mypic_2);
        networkimageview
                .setImageUrl(
                        "http://imgsrc.baidu.com/forum/pic/item/5fe3e103738da977c9825feab051f8198718e332.jpg",
                        imageLoader);

        // ImageListener listener = ImageLoader.getImageListener(imagaView,
        // R.drawable.ic_launcher, R.drawable.mypic_2);
        //
        // imageLoader
        // .get("http://imgsrc.baidu.com/forum/w%3D580/sign=0c1b13ef0c3387449cc52f74610ed937/bf94b9315c6034a8a4e0f53ecb13495408237644.jpg",
        // listener, 100, 100);

    }
}
package com.example.volley_1;

import android.graphics.Bitmap;
import android.support.v4.util.LruCache;

import com.android.volley.toolbox.ImageLoader.ImageCache;

/** * @author HD * @date 2015-11-23 * @package_name com.example.volley_1 * @file_name BitmapCache.java */
public class BitmapCache implements ImageCache {
    private LruCache<String, Bitmap> mCache;

    public BitmapCache() {
        // TODO 自动生成的构造函数存根
        int maxSize = 10*1024*1024;
        mCache = new LruCache<String, Bitmap>(maxSize){
            @Override
            protected int sizeOf(String key, Bitmap value) {
                // TODO 自动生成的方法存根
                return value.getRowBytes()*value.getHeight();
            }
        };
    }

    @Override
    public Bitmap getBitmap(String url) {
        // TODO 自动生成的方法存根
        return mCache.get(url);
    }

    @Override
    public void putBitmap(String url, Bitmap bitmap) {
        // TODO 自动生成的方法存根
        mCache.put(url, bitmap);
    }

}
<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.volley_1.MainActivity" >

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="match_parent" />

    <com.android.volley.toolbox.NetworkImageView
        android:id="@+id/network_image_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.volley_1" android:versionCode="1" android:versionName="1.0" >

    <uses-sdk  android:minSdkVersion="14" android:targetSdkVersion="21" />
    <uses-permission android:name="android.permission.INTERNET"/>

    <application  android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" >
        <activity  android:name=".MainActivity" android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

你可能感兴趣的:(Volley)