network imageview android
I have written more about volley in my previous tutorial .Please refer json parsing using volley.This is sample for loading images in image view in using volley lib network image view.
Steps to follow:
Steps to follow:
1.Create new project.
2.add volley.jar in your libs.clickhere to download vollery.jar.
3.Create AppController.java Singleton class where we initialize all the volley core libs.
public class AppController extends Application { public static final String TAG = AppController.class.getSimpleName(); private RequestQueue mRequestQueue; private ImageLoader mImageLoader; private static AppController mInstance; @Override public void onCreate() { super.onCreate(); mInstance = this; } public static synchronized AppController getInstance() { return mInstance; } public RequestQueue getRequestQueue() { if (mRequestQueue == null) { mRequestQueue = Volley.newRequestQueue(getApplicationContext()); } return mRequestQueue; } public ImageLoader getImageLoader() { getRequestQueue(); if (mImageLoader == null) { mImageLoader = new ImageLoader(this.mRequestQueue, new LruBitmapCache()); } return this.mImageLoader; } } |
4.add the AppController.java into the Application tag in manifest.xml to execute this at app launch.and also add internet permission.
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.volleynetworkimageview" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="21" /> <uses-permission android:name="android.permission.INTERNET" /> <application android:name="com.example.volleynetworkimageview.AppController" 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> |
5.add the NetworkImageView View into the main layout.
<com.android.volley.toolbox.NetworkImageView android:id="@+id/networkImageView" android:layout_width="fill_parent" android:layout_height="130dp" android:layout_margin="10dp" android:scaleType="fitXY" > </com.android.volley.toolbox.NetworkImageView> |
public class LruBitmapCache extends LruCache<String, Bitmap> implements ImageCache { public static int getDefaultLruCacheSize() { final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024); final int cacheSize = maxMemory / 8; return cacheSize; } public LruBitmapCache() { this(getDefaultLruCacheSize()); } public LruBitmapCache(int sizeInKiloBytes) { super(sizeInKiloBytes); } @Override protected int sizeOf(String key, Bitmap value) { return value.getRowBytes() * value.getHeight() / 1024; } @Override public Bitmap getBitmap(String url) { return get(url); } @Override public void putBitmap(String url, Bitmap bitmap) { put(url, bitmap); } } |
7.Load the image using network imageview.
imgLoader=AppController.getInstance().getImageLoader(); networkView.setImageUrl(imageUrl, imgLoader); |
imgLoader.get(imageUrl, new ImageListener() { @Override public void onErrorResponse(VolleyError error) { / TODO Auto-generated method stub } @Override public void onResponse(ImageContainer response, boolean arg1) { // TODO Auto-generated method stub if(response.getBitmap()!=null){ imageView1.setImageBitmap(response.getBitmap()); } } }); |
imgLoader.get(imageUrl, ImageLoader.getImageListener(imageView2, R.drawable.loading_image, R.drawable.loading_image) ); |
Screenshot:
DOWNLOAD FULL SOURCE CODE
No comments:
Post a Comment