Skip to content

Commit

Permalink
Gridview fast + disk and memory cache
Browse files Browse the repository at this point in the history
just missing some cleaning of comments and loading bar
  • Loading branch information
leninbooter committed Aug 9, 2014
1 parent 4244038 commit 105b7de
Show file tree
Hide file tree
Showing 4 changed files with 321 additions and 240 deletions.
121 changes: 121 additions & 0 deletions whatsup/src/main/java/com/whatsup/whatsup/ImageAdapter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package com.whatsup.whatsup;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Point;
import android.os.Handler;
import android.util.Log;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ViewSwitcher;

import java.util.HashMap;

/**
* Created by alenin on 09/08/2014.
*/
public class ImageAdapter extends BaseAdapter {
private Context mContext;
private LayoutInflater mInflater;
private int mResourceId;
private int mQuantity;
private int hw;
private HashMap<Integer, String> mImages = new HashMap<Integer, String>();

private static final int PROGRESSBARINDEX = 0;
private static final int IMAGEVIEWINDEX = 1;

private Handler mHandler;
private ImageLoader mImageLoader = null;

public ImageAdapter(Context mContext, int resourceId, int quantity) {
this.mContext = mContext;
this.mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.mQuantity = quantity;
this.mResourceId = resourceId;
for( int i=0; i<quantity; i++ )
mImages.put( i, null );


Display display = ( (Activity) this.mContext ).getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
hw = size.x / 3;
mImageLoader = new ImageLoader( R.drawable.empty_frame );

}

public int getCount() {
Log.d("getCount:", String.valueOf(mImages.size()));
return mImages.size();
}

public String getItem(int position) {
return null;
}

public long getItemId(int position) {
return 0;
}

// create a new ImageView for each item referenced by the Adapter
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Log.d("getView", "yes");
ImageView imageView;
ViewHolder_GVItem vh;
if (convertView == null) { // if it's not recycled, initialize some attributes
convertView = mInflater.inflate( R.layout.fragment_what_was_here_gv_item, parent, false );
vh = new ViewHolder_GVItem();
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams( hw, hw ));
imageView.setImageResource(R.drawable.empty_frame);
//imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
//imageView.setPadding(0, 0, 0, 0);
vh.icon = (ImageView) convertView.findViewById( R.id.imageview );
vh.icon.getLayoutParams().height = hw;
vh.icon.getLayoutParams().width = hw;
vh.icon.setImageResource(R.drawable.empty_frame);
vh.position = position;
convertView.setTag( vh );
} else {
vh = (ViewHolder_GVItem) convertView.getTag();
//vh = new ViewHolder();
vh.icon.setImageResource( R.drawable.empty_frame);
vh.position = position;
}
mImageLoader.getImage( mImages.get( position ), position, vh, null );
return convertView;
}

public void setImage( int position, String path ) {
mImages.put(position, path);
Log.d("setImage" , "yes");
}


public void handleImageLoaded(
final ViewSwitcher aViewSwitcher,
final ImageView aImageView,
final Bitmap aBitmap) {

// The enqueue the following in the UI thread
mHandler.post(new Runnable() {
public void run() {

// set the bitmap in the ImageView
aImageView.setImageBitmap(aBitmap);

// explicitly tell the view switcher to show the second view
aViewSwitcher.setDisplayedChild(IMAGEVIEWINDEX);
}
});

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
package com.whatsup.whatsup;

import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Point;
import android.os.AsyncTask;
import android.util.Log;
import android.view.Display;
import android.widget.BaseAdapter;
import android.widget.Toast;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.concurrent.Executor;

/**
* Created by alenin on 08/08/2014.
*/
/*public class ImageDownloaderAsyncTask extends AsyncTask<String, Void, String> {
Activity activity;
private onImageDownloaderAsyncTaskListener mListener;
public interface onImageDownloaderAsyncTaskListener {
public void PickDownloadedImage(String path);
}
public ImageDownloaderAsyncTask(Activity activity, WhatWasHereFragmentGV whatWasHereFragmentGV, Executor executor, String... urls) {
this.activity = activity;
mListener = whatWasHereFragmentGV;
//execute(urls);
executeOnExecutor( executor, urls);
}
protected String doInBackground(String... urls) {
InputStream iStream = null;
String imgUrl = Params.CDN + urls[0];
Log.d("ImageLoaderTask will download: ", imgUrl);
URL url;
File tmpFile = null;
try {
File cacheDirectory = activity.getCacheDir();
tmpFile = new File( cacheDirectory.getPath() + "/" + urls[1] );
if( true ) {
Log.d("Image was on cache", "no");
url = new URL(imgUrl);
Bitmap b;
int hw;
Display display = activity.getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.connect();
iStream = urlConnection.getInputStream();
FileOutputStream fOutStream = new FileOutputStream(tmpFile);
hw = size.x / 3;
b = BitmapFactory.decodeStream(iStream);
b = Bitmap.createScaledBitmap(b, hw, hw, false);
b.compress(Bitmap.CompressFormat.JPEG, 90, fOutStream);
fOutStream.flush();
fOutStream.close();
}else {
Log.d("Image was on cache", "yes");
}
} catch (Exception e) {
Log.d("Exception on Imagedownloader task", e.getMessage());
e.printStackTrace();
} catch ( OutOfMemoryError ome ) {
return null;
}
return tmpFile.getPath();
}
protected void onPostExecute(String result) {
if( result != null )
mListener.PickDownloadedImage( result );
else {
Toast.makeText(activity, activity.getString(R.string.no_memory), Toast.LENGTH_SHORT).show();
}
}
}*/

public class ImageDownloaderAsyncTask extends Thread {
Context mContext;
BaseAdapter mBaseAdapter;
String[] urls = new String[3];
int index;

private onImageDownloaderAsyncTaskListener mListener;

public interface onImageDownloaderAsyncTaskListener {
public void PickDownloadedImage(String path);
}

public ImageDownloaderAsyncTask( Context context, BaseAdapter baseAdapter, WhatWasHereFragmentGV whatWasHereFragmentGV, String...urls ) {
mContext = context;
mBaseAdapter = baseAdapter;
mListener = whatWasHereFragmentGV;
this.urls[0] = urls[0]; //full url
this.urls[1] = urls[1]; //image name
this.urls[2] = urls[2]; //index of image on grid
this.index = Integer.valueOf( urls[2] );
}

@Override
public void run() {
InputStream iStream = null;
String imgUrl = Params.CDN + urls[0];
Log.d("ImageLoaderTask will download: ", imgUrl );
URL url;
File tmpFile = null;
try {
File cacheDirectory = ( (Activity) mContext).getCacheDir();
tmpFile = new File( cacheDirectory.getPath() + "/" + urls[2] + "_" + urls[1] );
if( true ) {
Log.d("Image was on cache", "no");
url = new URL(imgUrl);
Bitmap b;
int hw;
Display display = ( (Activity) mContext).getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);

HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.connect();
if( isInterrupted() ) return;
iStream = urlConnection.getInputStream();
FileOutputStream fOutStream = new FileOutputStream(tmpFile);
hw = size.x / 3;
b = BitmapFactory.decodeStream( iStream );
b = Bitmap.createScaledBitmap(b, hw, hw, false);
if( isInterrupted() ) return;
b.compress(Bitmap.CompressFormat.JPEG, 90, fOutStream);
fOutStream.flush();
fOutStream.close();
}else {
Log.d("Image was on cache", "yes");
}
HashMap<String, Object> item = new HashMap<String, Object>();
mListener.PickDownloadedImage( tmpFile.getPath() );
} catch (Exception e) {
Log.d("Exception on Imagedownloader task", e.getMessage());
e.printStackTrace();
} catch ( OutOfMemoryError ome ) {
Log.d("Exception on Imagedownloader task", "OutOfMemoryError");
}

}
}
23 changes: 11 additions & 12 deletions whatsup/src/main/java/com/whatsup/whatsup/ImageLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public class ImageLoader implements ImageLoaderAsyncTask.OnImageLoaderAsyncTaskL
private int mNoLoadedImage;

public ImageLoader(int mNoLoadedImage_in) {
mImageLoaderExecutor = Executors.newSingleThreadExecutor();
mImageLoaderExecutor = Executors.newFixedThreadPool(6);
this.mNoLoadedImage = mNoLoadedImage_in;
final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
// Use 1/8th of the available memory for this memory cache.
Expand All @@ -65,8 +65,6 @@ protected int sizeOf(String key, Bitmap bitmap) {
};
}



public Bitmap getBitmapFromMemCache(String key) {
return mMemoryCache.get(key);
}
Expand All @@ -80,16 +78,17 @@ public void addBitmapToMemoryCache(String key, Bitmap bitmap) {
public void getImage( String pathFile_in, int position_in, ViewHolder_GVItem vh_in, Executor pool ) {
Bitmap bitmap = null;

bitmap = getBitmapFromMemCache(String.valueOf(position_in));
if (bitmap != null) {
Log.d("cached", "yes");
vh_in.icon.setImageBitmap( bitmap );
} else {
Log.d("cached", "no");
vh_in.icon.setImageResource( mNoLoadedImage );
if( pathFile_in != null)
new ImageLoaderAsyncTask( vh_in, position_in, pathFile_in, mImageLoaderExecutor, this );
bitmap = getBitmapFromMemCache(String.valueOf(position_in));
if (bitmap != null) {
Log.d("cached", "yes");
vh_in.icon.setImageBitmap( bitmap );
} else {
Log.d("cached", "no");
vh_in.icon.setImageResource( mNoLoadedImage );
if( pathFile_in != null) {
new ImageLoaderAsyncTask(vh_in, position_in, pathFile_in, mImageLoaderExecutor, this);
}
}
}
}

Loading

0 comments on commit 105b7de

Please sign in to comment.