Skip to content

Commit

Permalink
Gridview with lazy image loader
Browse files Browse the repository at this point in the history
Lazy loader works really slowly with a thread and a service ppool
  • Loading branch information
leninbooter committed Aug 1, 2014
1 parent fbf918a commit f00e7ae
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 31 deletions.
48 changes: 40 additions & 8 deletions whatsup/src/main/java/com/whatsup/whatsup/GVImageLoader.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,54 @@
package com.whatsup.whatsup;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.ImageView;

import java.io.File;

/**
* Created by alenin on 28/07/2014.
*/
public class GVImageLoader extends AsyncTask<WhatWasHereFragmentGV.ViewHolder, Void, Bitmap> {

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() {
if (this.mVh.position == this.mPosition) {
mVh.icon.setImageBitmap(BitmapFactory.decodeFile(mAddr));
/*this.mContext.runOnUiThread(new Runnable() {
@Override
public void run() {
}
});*/
}
}
}
/*
public class GVImageLoader extends AsyncTask<ViewHolder_GVItem, Void, Bitmap> {
private ImageView iv;
private WhatWasHereFragmentGV.ViewHolder vh;
private ViewHolder_GVItem vh;
private String addr;
private int position;
public GVImageLoader( String pathFile, WhatWasHereFragmentGV.ViewHolder vh, int position ) {
public GVImageLoader( String pathFile, ViewHolder_GVItem vh, int position ) {
addr = pathFile;
//this.iv = iv;
this.vh = vh;
Expand All @@ -24,18 +57,17 @@ public GVImageLoader( String pathFile, WhatWasHereFragmentGV.ViewHolder vh, int
}
@Override
protected Bitmap doInBackground(WhatWasHereFragmentGV.ViewHolder... viewHolders) {
protected Bitmap doInBackground(ViewHolder_GVItem... viewHolders) {
Log.d("doInBackground", "on GVImageLoader");
return BitmapFactory.decodeFile(addr);
}
@Override
protected void onPostExecute( Bitmap result ) {
Log.d("onPostExecute", "on GVImageLoader");
if( result != null )
Log.d("result", "null");
if( this.vh.position != this.position )
if( this.vh.position == this.position )
this.vh.icon.setImageBitmap( result );
}
}
}*/

12 changes: 12 additions & 0 deletions whatsup/src/main/java/com/whatsup/whatsup/ViewHolder_GVItem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.whatsup.whatsup;

import android.widget.ImageView;

/**
* Created by alenin on 31/07/2014.
*/

public class ViewHolder_GVItem {
int position;
ImageView icon;
}
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,11 @@ public void onPause() {
super.onPause();
Log.d("on", "onPause");
mPaused = true;
if( downloadTask != null )
downloadTask.cancel( true );
if( listViewLoaderTask != null )
listViewLoaderTask.cancel( true );

}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,15 @@ public class WhatWasHereFragmentGV extends Fragment {
private Params parameters;
private DownloadTask downloadTask;
private ListViewLoaderTask listViewLoaderTask;
private ImageLoaderTask[] imageLoaderTask;
private ImageDownloaderTask[] imageLoaderTask;
private String data = null;
private List<HashMap<String, Object>> mPictures;
private int mDownloadesPictures = 0;
private JSONObject jObject;
private GridView[] gridView;
private ImageAdapter imgAdapter;
private ExecutorService service;
private ExecutorService mImageLoaderPoolThread;

public interface OnWwhGvsFragmentListener {
public void setCurrentFragmentTag(String tag);
Expand Down Expand Up @@ -111,6 +113,7 @@ public void onAttach(Activity activity) {
@Override
public void onResume() {
super.onResume();
Log.d("onResume:", "from wwhgv");
mListener.setCurrentFragmentTag("events_pictures");
}
@Override
Expand All @@ -120,6 +123,9 @@ public void onPause() {
if( service != null) {
shutdownAndAwaitTermination( service );
}
if( mImageLoaderPoolThread != null ) {
shutdownAndAwaitTermination( mImageLoaderPoolThread );
}

}

Expand Down Expand Up @@ -179,6 +185,7 @@ public void onActivityCreated(Bundle savedInstanceState) {
downloadTask = new DownloadTask();
String strUrl = parameters.REST_SERVER + "/whatsup/slim/public/index.php/place/getpicturesevents/" + getArguments().getString(EVENT_ID) + "/null/null";
downloadTask.execute(strUrl);

}
}

Expand Down Expand Up @@ -275,21 +282,20 @@ protected List<HashMap<String, Object>> doInBackground(String... strJson) {
@Override
protected void onPostExecute(List<HashMap<String, Object>> pictures) {
try {
getView().findViewById(R.id.loadingPanel).setVisibility(View.GONE);
if (pictures.size() == 0) {
getView().findViewById(R.id.no_info_not_rellay).setVisibility(View.VISIBLE);
} else {
imgAdapter = new ImageAdapter(getActivity(), R.layout.fragment_what_was_here_gv_item, pictures.size());
GridView gridview = (GridView) getView().findViewById(R.id.gridview);
GridView gridview = (GridView) mWhatWasHereListView.findViewById(R.id.gridview);
getView().findViewById(R.id.loadingPanel).setVisibility(View.GONE);
gridview.setAdapter(imgAdapter);
gridview.setOnItemClickListener(getImageClickListener());
gridview.setFastScrollEnabled(true);
gridview.setFastScrollAlwaysVisible(false);
imageLoaderTask = new ImageLoaderTask[pictures.size()];
service = Executors.newFixedThreadPool(100);
for (int i = 0; i < imageLoaderTask.length; i++) {
for (int i = 0; i < pictures.size(); i++) {
Log.d("new thread for image: ", mPictures.get(i).get("source").toString());
service.submit( new ImageLoaderTask(mPictures.get(i).get("source").toString(),
service.submit( new ImageDownloaderTask(mPictures.get(i).get("source").toString(),
mPictures.get(i).get("source").toString().substring(1, mPictures.get(i).get("source").toString().length()),
String.valueOf(i))
);
Expand Down Expand Up @@ -323,11 +329,11 @@ protected void onPostExecute(String result) {
}
}
}
private class ImageLoaderTask extends Thread {
private class ImageDownloaderTask extends Thread {
String[] urls = new String[3];
int index;

public ImageLoaderTask( String...urls ) {
public ImageDownloaderTask( String...urls ) {
this.urls[0] = urls[0];
this.urls[1] = urls[1];
this.urls[2] = urls[2];
Expand All @@ -345,6 +351,7 @@ public void run() {
File cacheDirectory = getActivity().getCacheDir();
tmpFile = new File( cacheDirectory.getPath() + "/" + urls[2] + "_" + urls[1] );
if( !tmpFile.exists() ) {
Log.d("Image was on cache", "no");
url = new URL(imgUrl);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.connect();
Expand All @@ -356,6 +363,8 @@ public void run() {
b.compress(Bitmap.CompressFormat.JPEG, 30, fOutStream);
fOutStream.flush();
fOutStream.close();
}else {
Log.d("Image was on cache", "yes");
}
HashMap<String, Object> item = new HashMap<String, Object>();
item.put( "source", tmpFile.getPath() );
Expand All @@ -368,7 +377,7 @@ public void run() {
}
synchronized ( imgAdapter ) {
imgAdapter.setImage(index, mPictures.get(index).get("source").toString());
imgAdapter.notifyDataSetChanged();

}
}
}
Expand Down Expand Up @@ -422,6 +431,7 @@ protected void onPostExecute(Integer index) {

private class ImageAdapter extends BaseAdapter{
private Context mContext;
private LayoutInflater mInflater;
private int mResourceId;
private int mQuantity;
private int hw;
Expand All @@ -436,6 +446,7 @@ private class ImageAdapter extends BaseAdapter{

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++ )
Expand All @@ -456,9 +467,11 @@ protected int sizeOf(String key, Bitmap bitmap) {
Point size = new Point();
display.getSize(size);
hw = size.x / 3;
mImageLoaderPoolThread = Executors.newCachedThreadPool();
}

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

Expand All @@ -474,26 +487,32 @@ public long getItemId(int position) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
ViewHolder vh;
ViewHolder_GVItem vh;
if (convertView == null) { // if it's not recycled, initialize some attributes
vh = new ViewHolder();
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;
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 {
//imageView = (ImageView) convertView;
vh = new ViewHolder();
vh.icon = (ImageView) convertView;
vh = (ViewHolder_GVItem) convertView.getTag();
//vh = new ViewHolder();
vh.icon.setImageResource(R.drawable.empty_frame);
vh.position = position;
}
if( mImages.get(position) == null ) {
//vh.icon.setImageResource(R.drawable.empty_frame);
}else {
//vh.icon.setImageResource( R.drawable.empty_frame );
//new GVImageLoader( mImages.get( position ), vh, position );
mImageLoaderPoolThread.submit( new GVImageLoader(getActivity(), mImages.get( position ), vh, position ) );
/*Bitmap bitmap = getBitmapFromMemCache(String.valueOf(position));
if (bitmap != null) {
imageView.setImageBitmap(bitmap);
Expand All @@ -507,12 +526,12 @@ public View getView(int position, View convertView, ViewGroup parent) {
//imageView.setImageDrawable( Drawable.createFromPath( mImages.get(position) ) );*/
}

return vh.icon;
return convertView;
}

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

public Bitmap getBitmapFromMemCache(String key) {
Expand Down Expand Up @@ -543,11 +562,6 @@ public void run() {

}
}

static class ViewHolder {
int position;
ImageView icon;
}
}


0 comments on commit f00e7ae

Please sign in to comment.