Skip to content

Commit

Permalink
grid view fast
Browse files Browse the repository at this point in the history
  • Loading branch information
leninbooter committed Aug 1, 2014
1 parent f00e7ae commit 4244038
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 223 deletions.
73 changes: 0 additions & 73 deletions whatsup/src/main/java/com/whatsup/whatsup/GVImageLoader.java

This file was deleted.

171 changes: 72 additions & 99 deletions whatsup/src/main/java/com/whatsup/whatsup/ImageLoader.java
Original file line number Diff line number Diff line change
@@ -1,122 +1,95 @@
package com.whatsup.whatsup;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Looper;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.support.v4.util.LruCache;
import android.util.Log;
import android.widget.ImageView;
import android.widget.ViewSwitcher;

public class ImageLoader extends Thread {

public interface ImageLoadListener {

void handleImageLoaded(ViewSwitcher aViewSwitcher, ImageView aImageView, Bitmap aBitmap);
}

private static final String TAG = ImageLoader.class.getSimpleName();
ImageLoadListener mListener = null;
private Handler handler;

/**
* Image loader takes an object that extends ImageLoadListener
* @param lListener
*/
ImageLoader(ImageLoadListener lListener){
mListener = lListener;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

/**
* Created by alenin on 28/07/2014.
*/
/*
public class GVImageLoader extends Thread {
private ViewHolder_GVItem mVh;
private String mAddr;
private int mPosition;
private Activity mContext;
public GVImageLoader(Activity context, String pathFile, ViewHolder_GVItem vh, int position ) {
this.mContext = context;
this.mAddr = pathFile;
this.mVh = vh;
this.mPosition = position;
run();
}
@Override
public void run() {
try {

// preparing a looper on current thread
// the current thread is being detected implicitly
Looper.prepare();
if (this.mVh.position == this.mPosition) {
mVh.icon.setImageBitmap(BitmapFactory.decodeFile(mAddr));
// Looper gets attached to the current thread by default
handler = new Handler();

Looper.loop();
// Thread will start

} catch (Throwable t) {
Log.e(TAG, "ImageLoader halted due to a error: ", t);
}
}
}

/**
* Method stops the looper and thus the thread
*/
public synchronized void stopThread() {

// Use the handler to schedule a quit on the looper
handler.post(new Runnable() {

public void run() {
// This runs on the ImageLoader thread
Log.i(TAG, "DownloadThread loop quitting by request");

Looper.myLooper().quit();
}*/

public class ImageLoader implements ImageLoaderAsyncTask.OnImageLoaderAsyncTaskListener {
private LruCache<String, Bitmap> mMemoryCache;
private Executor mImageLoaderExecutor;
private List< ImageLoaderAsyncTask > mAsyncTask = new ArrayList<ImageLoaderAsyncTask>();
private int mNoLoadedImage;

public ImageLoader(int mNoLoadedImage_in) {
mImageLoaderExecutor = Executors.newSingleThreadExecutor();
this.mNoLoadedImage = mNoLoadedImage_in;
final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
// Use 1/8th of the available memory for this memory cache.
final int cacheSize = maxMemory / 2;
mMemoryCache = new LruCache<String, Bitmap>(cacheSize) {
@Override
protected int sizeOf(String key, Bitmap bitmap) {
// The cache size will be measured in kilobytes rather than
// number of items.
return bitmap.getByteCount() / 1024;
}
});
};
}

/**
* Method queues the image at path to load
* Note that the actual loading takes place in the UI thread
* the ImageView and ViewSwitcher are just references for the
* UI thread.
* @param aPath - Path where the bitmap is located to load
* @param aImageView - The ImageView the UI thread will load
* @param aViewSwitcher - The ViewSwitcher that needs to display the imageview
*/
public synchronized void queueImageLoad(
final String aPath,
final ImageView aImageView,
final ViewSwitcher aViewSwitcher) {

// Wrap DownloadTask into another Runnable to track the statistics
handler.post(new Runnable() {
public void run() {
try {

synchronized (aImageView){
// make sure this thread is the only one performing activities on
// this imageview
BitmapFactory.Options lOptions = new BitmapFactory.Options();
lOptions.inSampleSize = 1;
Bitmap lBitmap = BitmapFactory.decodeFile(aPath, lOptions);
//aImage.setImageBitmap(lBitmap);

// Load the image here
signalUI(aViewSwitcher, aImageView, lBitmap);
}
}
catch(Exception e){
e.printStackTrace();
}
}
});
public Bitmap getBitmapFromMemCache(String key) {
return mMemoryCache.get(key);
}

/**
* Method is called when the bitmap is loaded. The UI thread adds the bitmap to the imageview.
* @param aViewSwitcher - The ViewSwitcher that needs to display the imageview
* @param aImageView - The ImageView the UI thread will load
* @param aImage - The Bitmap that gets loaded into the ImageView
*/
private void signalUI(
ViewSwitcher aViewSwitcher,
ImageView aImageView,
Bitmap aImage){

if(mListener != null){
// we have an object that implements ImageLoadListener

mListener.handleImageLoaded(aViewSwitcher, aImageView, aImage);
public void addBitmapToMemoryCache(String key, Bitmap bitmap) {
if (getBitmapFromMemCache(key) == null) {
mMemoryCache.put(key, 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 );
}
}
}

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

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.util.Log;

import java.util.concurrent.Executor;

/**
* Created by alenin on 01/08/2014.
*/
public class ImageLoaderAsyncTask extends AsyncTask<Integer, Void, Bitmap> {

public interface OnImageLoaderAsyncTaskListener {
public void addBitmapToMemoryCache(String key, Bitmap bitmap);
}

OnImageLoaderAsyncTaskListener mCallBback;
private ViewHolder_GVItem vh;
private int position;
private String addr;

public ImageLoaderAsyncTask( ViewHolder_GVItem vh, int position, String pathFile, Executor pool, ImageLoader callbackclass) {
this.vh = vh;
this.position = position;
this.addr = pathFile;
mCallBback = (OnImageLoaderAsyncTaskListener) callbackclass;
execute();
}

protected Bitmap doInBackground(Integer... positions) {
Log.d("doInBackground", "on ImageLoaderAsyncTask");
return BitmapFactory.decodeFile( this.addr );
}

@Override
protected void onPostExecute( Bitmap result ) {
Log.d("onPostExecute", "on ImageLoaderAsyncTask");
if( this.vh.position == this.position ) {
this.vh.icon.setImageBitmap(result);
mCallBback.addBitmapToMemoryCache( String.valueOf( vh.position ), result );
}
}
}
Loading

0 comments on commit 4244038

Please sign in to comment.