-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Gridview fast + disk and memory cache
just missing some cleaning of comments and loading bar
- Loading branch information
1 parent
4244038
commit 105b7de
Showing
4 changed files
with
321 additions
and
240 deletions.
There are no files selected for viewing
121 changes: 121 additions & 0 deletions
121
whatsup/src/main/java/com/whatsup/whatsup/ImageAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}); | ||
|
||
} | ||
} |
157 changes: 157 additions & 0 deletions
157
whatsup/src/main/java/com/whatsup/whatsup/ImageDownloaderAsyncTask.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.