I'm currently developing an app where I have a listview. This listview is populated with text and images fetched from a XML file online. It's populating correctly however, whenever you scroll, anything that wasn't currently on the screen reloads and recalculates the size of the bitmap to go in the listview. This leads to the scrolling being less fluid and annoying. How can I get my activity to populate the listview with the images and load them one time to stay there?
ImageLoader:
public class ImageLoader {
MemoryCache memoryCache = new MemoryCache();
FileCache fileCache;
private Map<ImageView, String> imageViews = Collections.synchronizedMap(new WeakHashMap<ImageView, String>());
ExecutorService executorService;
int size;
int placeholderpic;
public ImageLoader(Context context, int size, int placeholderpic) {
fileCache = new FileCache(context);
executorService = Executors.newFixedThreadPool(5);
this.size = size;
this.placeholderpic = placeholderpic;
}
public void displayImage(String url, ImageView imageView) {
imageViews.put(imageView, url);
Bitmap bitmap = memoryCache.get(url);
if(bitmap != null)
imageView.setImageBitmap(bitmap);
else {
queuePhoto(url, imageView);
imageView.setImageResource(placeholderpic);
}
}
private void queuePhoto(String url, ImageView imageView) {
PhotoToLoad p = new PhotoToLoad(url, imageView);
executorService.submit(new PhotosLoader(p));
}
private Bitmap getBitmap(String url) {
File f = fileCache.getFile(url);
//from SD cache
Bitmap b = decodeFile(f);
if(b != null)
return b;
//from web
try {
Bitmap bitmap = null;
URL imageUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection)imageUrl.openConnection();
conn.setConnectTimeout(30000);
conn.setReadTimeout(30000);
conn.setInstanceFollowRedirects(true);
InputStream is=conn.getInputStream();
OutputStream os = new FileOutputStream(f);
Utils.CopyStream(is, os);
os.close();
bitmap = decodeFile(f);
return bitmap;
} catch (Throwable ex) {
ex.printStackTrace();
if(ex instanceof OutOfMemoryError)
memoryCache.clear();
return null;
}
}
//decodes image and scales it to reduce memory consumption
private Bitmap decodeFile(File f) {
try {
//decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f),null,o);
//Find the correct scale value. It should be the power of 2.
final int REQUIRED_SIZE = size;
int width_tmp = o.outWidth, height_tmp=o.outHeight;
int scale = 1;
while(true){
if(width_tmp/2 < REQUIRED_SIZE || height_tmp/2 < REQUIRED_SIZE)
break;
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}
//decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
} catch (FileNotFoundException e) {}
return null;
}
//Task for the queue
private class PhotoToLoad {
public String url;
public ImageView imageView;
public PhotoToLoad(String u, ImageView i) {
url = u;
imageView = i;
}
}
class PhotosLoader implements Runnable {
PhotoToLoad photoToLoad;
PhotosLoader(PhotoToLoad photoToLoad){
this.photoToLoad = photoToLoad;
}
#Override
public void run() {
if(imageViewReused(photoToLoad))
return;
Bitmap bmp = getBitmap(photoToLoad.url);
memoryCache.put(photoToLoad.url, bmp);
if(imageViewReused(photoToLoad))
return;
BitmapDisplayer bd = new BitmapDisplayer(bmp, photoToLoad);
Activity a = (Activity)photoToLoad.imageView.getContext();
a.runOnUiThread(bd);
}
}
boolean imageViewReused(PhotoToLoad photoToLoad) {
String tag=imageViews.get(photoToLoad.imageView);
if(tag == null || !tag.equals(photoToLoad.url))
return true;
return false;
}
//Used to display bitmap in the UI thread
class BitmapDisplayer implements Runnable {
Bitmap bitmap;
PhotoToLoad photoToLoad;
public BitmapDisplayer(Bitmap b, PhotoToLoad p){
bitmap = b; photoToLoad = p;
}
public void run() {
if(imageViewReused(photoToLoad))
return;
if(bitmap != null)
photoToLoad.imageView.setImageBitmap(bitmap);
else
photoToLoad.imageView.setImageResource(placeholderpic);
}
}
public void clearCache() {
memoryCache.clear();
fileCache.clear();
}
ListView Adapter:
public class LazyNewsAdapter extends BaseAdapter {
private Activity activity;
private ArrayList<News> listData;
private LayoutInflater inflater = null;
public LazyNewsAdapter(Activity activity, ArrayList<News> listData) {
this.activity = activity;
this.listData = listData;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return listData.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
News newsItem = listData.get(position);
View view = convertView;
if(convertView == null)
view = inflater.inflate(R.layout.news_cell, null);
TextView newsTitle = (TextView) view.findViewById(R.id.newsTitle);
TextView newsDate = (TextView) view.findViewById(R.id.newsDate);
ImageView image = (ImageView) view.findViewById(R.id.newsImage);
newsTitle.setText(newsItem.getNewsTitle());
newsDate.setText(newsItem.getNewsDate());
String url = newsItem.getNewsImageUrl();
ImageLoader imageLoader = new ImageLoader(activity, 600, R.drawable.placeholder);
imageLoader.displayImage(url, image);
return view;
}
You must use cache for that kind of things.
See Universal Image loader to load your images in an efficient way.
You must have only one instance of your imageLoader in an activity, dont instantiate it in each getView:
Do that line in your onCreate instead of getView:
ImageLoader imageLoader = new ImageLoader(activity, 600, R.drawable.placeholder);
Try to save online data to Array List variable. Then load it to list view with using notifydatasetChange
Use the listview optimization for the setting image in ListAdapter here's video tutorial that might help you also be sure to use Glide for setting the image as they take a step forward of recycling your Bitmap
https://www.youtube.com/watch?v=mKGoKnhN_Ys&index=91&list=PL1q3ROAofjeOUwh7lPBnGbg__DUodwLN7
Related
below is my imageloader code which work fine if new image are diffrent name but if new image are same name then is not change images show old image only suppose i have image name myimage is display perfectly if i add new image in url with same name and delete previous my code again show previous image not show new image due to cache what is problem in this code help me please
private ImageLoader imgLoader;
private String strUrl = "http://solutionboat.com/work_2/asset/images/rifat.jpg";
private String strUrl1 = "http://solutionboat.com/work_2/asset/images/sobin.jpg";
private String strUrl2 = "http://solutionboat.com/work_2/asset/images/samol.jpg";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView target0 = (ImageView) findViewById(R.id.target0);
ImageView target1 = (ImageView) findViewById(R.id.target1);
ImageView target2 = (ImageView) findViewById(R.id.target2);
imgLoader = new ImageLoader(this);
imgLoader.displayImage(strUrl, target0);
imgLoader.displayImage(strUrl1, target1);
imgLoader.displayImage(strUrl2, target2);
ViewFlipper flipper = (ViewFlipper) findViewById(R.id.viewFlipper1);
flipper.startFlipping();
}
}
ImageLoader class
public class ImageLoader {
MemoryCache memoryCache = new MemoryCache();
FileCache fileCache;
private Map<ImageView, String> imageViews =
Collections.synchronizedMap(new WeakHashMap<ImageView, String>());
ExecutorService executorService;
public ImageLoader(Context context) {
fileCache = new FileCache(context);
executorService = Executors.newFixedThreadPool(5);
}
final int stub_id = R.mipmap.ic_launcher;
public void displayImage(String url, ImageView imageView) {
imageViews.put(imageView, url);
Bitmap bitmap = memoryCache.get(url);
if(bitmap != null) {
imageView.setImageBitmap(bitmap);
} else {
queuePhoto(url, imageView);
imageView.setImageResource(stub_id);
}
}
private void queuePhoto(String url, ImageView imageView) {
PhotoToLoad p = new PhotoToLoad(url, imageView);
executorService.submit(new PhotosLoader(p));
}
private Bitmap getBitmap(String url) {
File f = fileCache.getFile(url);
Bitmap b = decodeFile(f);
if(b != null)
return b;
try {
Bitmap bitmap = null;
URL imageUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection) imageUrl.openConnection();
conn.setConnectTimeout(30000);
conn.setReadTimeout(30000);
conn.setInstanceFollowRedirects(true);
InputStream is = conn.getInputStream();
OutputStream os = new FileOutputStream(f);
Utils.copyStream(is, os);
os.close();
bitmap = decodeFile(f);
return bitmap;
} catch (Throwable ex) {
ex.printStackTrace();
return null;
}
}
private Bitmap decodeFile(File f) {
try {
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f), null, o);
final int REQUIRED_SIZE = 70;
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 1;
while(true) {
if(width_tmp / 2 < REQUIRED_SIZE || height_tmp / 2 < REQUIRED_SIZE) {
break;
}
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return null;
}
private class PhotoToLoad{
public String url;
public ImageView imageView;
public PhotoToLoad(String u, ImageView i) {
url = u;
imageView = i;
}
}
class PhotosLoader implements Runnable {
PhotoToLoad photoToLoad;
PhotosLoader(PhotoToLoad photoToLoad) {
this.photoToLoad = photoToLoad;
}
#Override
public void run() {
if(imageViewReused(photoToLoad)) {
return;
}
Bitmap bmp = getBitmap(photoToLoad.url);
memoryCache.put(photoToLoad.url, bmp);
if(imageViewReused(photoToLoad)) {
return;
}
BitmapDisplayer bd = new BitmapDisplayer(bmp, photoToLoad);
Activity a = (Activity) photoToLoad.imageView.getContext();
a.runOnUiThread(bd);
}
}
boolean imageViewReused(PhotoToLoad photoToLoad) {
String tag = imageViews.get(photoToLoad.imageView);
if(tag == null || !tag.equals(photoToLoad.url)) {
return true;
}
return false;
}
class BitmapDisplayer implements Runnable {
Bitmap bitmap;
PhotoToLoad photoToLoad;
public BitmapDisplayer(Bitmap b, PhotoToLoad p) {
bitmap = b;
photoToLoad = p;
}
#Override
public void run() {
if(imageViewReused(photoToLoad))
return;
if(bitmap != null){
photoToLoad.imageView.setImageBitmap(bitmap);
} else {
photoToLoad.imageView.setImageResource(stub_id);
}
}
}
public void clearCahce() {
memoryCache.clear();
fileCache.clear();
}
}
Use Picasso for any image loading/caching problems.
http://square.github.io/picasso/
Screenshot
Gridview Example
I have been trying to display images from url in this GirdView. Some images appear fine, but some images are not appearing correctly. I am also saving the images in cache for making it show images faster next time. This is my code.
MainActivity.java
package com.example.gridview;
public class MainActivity extends Activity {
ArrayList<String> imageList = new ArrayList<String>();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageList
.add("http://wallpapersus.com/wallpapers/2012/10/Megan-Fox-22-854x960.jpg");
imageList.add("http://www.wallpapersdesign.net
/wallpapers/2013/12/Transformers-Megan-Fox-854x960.jpg");
imageList
.add("http://wallpapersus.com/wallpapers/2012/10/Megan-Fox1-854x960.jpg");
imageList
.add("http://wallpapersus.com/wallpapers/2012/10/Megan-Fox-24-854x960.jpg");
imageList
.add("http://topwalls.net/wallpapers/2013/06/Wallpapers-Megan-Fox-11-854x960.jpg");
imageList
.add("http://topwalls.net/wallpapers/2013/06/Megan-Fox-133-854x960.jpg");
imageList
.add("http://topwalls.net/wallpapers/2013/06/Megan-Fox-89-854x960.jpg");
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this, imageList));
gridview.setOnItemClickListener(new OnImageClickListener(1));
}
class OnImageClickListener implements OnItemClickListener {
public int position;
// constructor
public OnImageClickListener(int position) {
this.position = position;
}
#Override
public void onItemClick(AdapterView<?> v, View arg1, int arg2, long arg3) {
Intent i = new Intent(getApplicationContext(),
FullScreenViewActivity.class);
Bundle basket = new Bundle();
basket.putStringArrayList("url", imageList);
basket.putString("position", String.valueOf(arg2));
i.putExtras(basket);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplicationContext().startActivity(i);
Log.e("Activity ImageClass", "Left from this class");
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.action_settings:
refresh();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void refresh() {
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this, imageList));
gridview.setOnItemClickListener(new OnImageClickListener(1));
}
}
This is the adapter
package com.example.gridview;
public class ImageAdapter extends BaseAdapter {
private Activity mContext;
ArrayList<String> imageList;
ImageLoader imageLoader;
ProgressDialog progress;
int versionCode = 10;
public ImageAdapter(Activity c, ArrayList<String> imageList) {
mContext = c;
this.imageList = imageList;
imageLoader = new ImageLoader(mContext, 200);
}
#Override
public int getCount() {
return imageList.size();
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some
// attributes
imageView = new ImageView(mContext);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {
Log.e("Inside", "if");
imageView.setLayoutParams(new GridView.LayoutParams(185, 185));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
imageView.setId(position);
} else {
Log.e("Inside", "else");
imageView.setLayoutParams(new GridView.LayoutParams(90, 90));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
}
} else {
imageView = (ImageView) convertView;
}
//imageLoader.clearCache();
imageLoader.DisplayImage(imageList.get(position), imageView);
return imageView;
}
}
ImageLoader.java
package com.example.gridview;
public class ImageLoader {
MemoryCache memoryCache = new MemoryCache();
FileCache fileCache;
public int REQUIRED_SIZE;
private Map<ImageView, String> imageViews = Collections
.synchronizedMap(new WeakHashMap<ImageView, String>());
ExecutorService executorService;
Activity context;
public ImageLoader(Activity context, int size) {
fileCache = new FileCache(context, size);
this.context = context;
executorService = Executors.newFixedThreadPool(20);
REQUIRED_SIZE = size;
}
final int stub_id = R.drawable.ic_menu_camera;
public void DisplayImage(String url, ImageView imageView) {
imageViews.put(imageView, url);
Bitmap bitmap = memoryCache.get(url);
if (bitmap != null)
imageView.setImageBitmap(bitmap);
else {
queuePhoto(url, imageView);
imageView.setImageResource(stub_id);
}
}
public void setWallpaper(String url) {
Bitmap bitmap = memoryCache.get(url);
if (bitmap != null) {
WallpaperManager myWallpaperManager = WallpaperManager
.getInstance(context);
// below line of code will set any image which is in the
// drawable
// folder
// myWallpaperManager.setResource(R.drawable.icon);
DisplayMetrics metrics = new DisplayMetrics();
context.getWindowManager().getDefaultDisplay().getMetrics(metrics);
int height = metrics.heightPixels;
int width = metrics.widthPixels;
Bitmap bitmap1 = Bitmap.createScaledBitmap(bitmap, width, height,
true);
WallpaperManager wallpaperManager = WallpaperManager
.getInstance(context);
// wallpaperManager.setWallpaperOffsetSteps(1, 1);
wallpaperManager.suggestDesiredDimensions(width, height);
try {
wallpaperManager.setBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void DisplayImageCircle(String url, ImageView imageView) {
imageViews.put(imageView, url);
Bitmap bitmap = memoryCache.get(url);
if (bitmap != null)
imageView.setImageBitmap(bitmap);
else {
queuePhoto(url, imageView);
imageView.setImageResource(stub_id);
}
}
private void queuePhoto(String url, ImageView imageView) {
PhotoToLoad p = new PhotoToLoad(url, imageView);
executorService.submit(new PhotosLoader(p));
}
private Bitmap getBitmap(String url) {
File f = fileCache.getFile(url);
// from SD cache
Bitmap b = decodeFile(f);
if (b != null)
return b;
// from web
try {
Bitmap bitmap = null;
URL imageUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection) imageUrl
.openConnection();
conn.setConnectTimeout(6000000);
conn.setReadTimeout(6000000);
conn.setInstanceFollowRedirects(true);
InputStream is = conn.getInputStream();
OutputStream os = new FileOutputStream(f);
Utils.CopyStream(is, os);
os.close();
bitmap = decodeFile(f);
return bitmap;
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
// decodes image and scales it to reduce memory consumption
private Bitmap decodeFile(File f) {
try {
// decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f), null, o);
// Find the correct scale value. It should be the power of 2.
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 1;
while (true) {
if (width_tmp / 2 < REQUIRED_SIZE
|| height_tmp / 2 < REQUIRED_SIZE)
break;
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}
// decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
} catch (FileNotFoundException e) {
}
return null;
}
// Task for the queue
private class PhotoToLoad {
public String url;
public ImageView imageView;
public PhotoToLoad(String u, ImageView i) {
url = u;
imageView = i;
}
}
class PhotosLoader implements Runnable {
PhotoToLoad photoToLoad;
PhotosLoader(PhotoToLoad photoToLoad) {
this.photoToLoad = photoToLoad;
}
#Override
public void run() {
if (imageViewReused(photoToLoad))
return;
Bitmap bmp = getBitmap(photoToLoad.url);
memoryCache.put(photoToLoad.url, bmp);
if (imageViewReused(photoToLoad))
return;
BitmapDisplayer bd = new BitmapDisplayer(bmp, photoToLoad);
Activity a = (Activity) photoToLoad.imageView.getContext();
a.runOnUiThread(bd);
}
}
boolean imageViewReused(PhotoToLoad photoToLoad) {
String tag = imageViews.get(photoToLoad.imageView);
if (tag == null || !tag.equals(photoToLoad.url))
return true;
return false;
}
// Used to display bitmap in the UI thread
class BitmapDisplayer implements Runnable {
Bitmap bitmap;
PhotoToLoad photoToLoad;
public BitmapDisplayer(Bitmap b, PhotoToLoad p) {
bitmap = b;
photoToLoad = p;
}
#Override
public void run() {
if (imageViewReused(photoToLoad))
return;
if (bitmap != null)
photoToLoad.imageView.setImageBitmap(bitmap);
else
photoToLoad.imageView.setImageResource(stub_id);
}
}
public void clearCache() {
memoryCache.clear();
fileCache.clear();
}
}
I also tried running imageLoader in doInBackground() but it was of no use.
Please Help !!
set your url also in ImageView
imageView.setTag(yourURL);
imageLoader.DisplayImage(imageList.get(position), imageView);
I have a customized list view in my application, which is showing an image and text (Json). The image I am getting from URL is working, but listview is laggy (scrolling lag).
Maybe my problem is ImageLoader.java class
This is my code:
public class ImageLoader {
MemoryCache memoryCache = new MemoryCache();
FileCache fileCache;
private Map<ImageView, String> imageViews = Collections
.synchronizedMap(new WeakHashMap<ImageView, String>());
ExecutorService executorService;
public ImageLoader(Context context) {
fileCache = new FileCache(context);
executorService = Executors.newFixedThreadPool(5);
}
final int stub_id = R.drawable.aaaaaaaaaaaaaaa;
public void DisplayImage(String url, ImageView imageView) {
imageViews.put(imageView, url);
Bitmap bitmap = memoryCache.get(url);
if (bitmap != null)
imageView.setImageBitmap(bitmap);
else {
queuePhoto(url, imageView);
imageView.setImageResource(stub_id);
}
}
private void queuePhoto(String url, ImageView imageView) {
PhotoToLoad p = new PhotoToLoad(url, imageView);
executorService.submit(new PhotosLoader(p));
}
private Bitmap getBitmap(String url) {
File f = fileCache.getFile(url);
// from SD cache
Bitmap b = decodeFile(f);
if (b != null)
return b;
// from web
try {
Bitmap bitmap = null;
URL imageUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection) imageUrl
.openConnection();
conn.setConnectTimeout(30000);
conn.setReadTimeout(30000);
conn.setInstanceFollowRedirects(true);
InputStream is = conn.getInputStream();
OutputStream os = new FileOutputStream(f);
Utils.CopyStream(is, os);
os.close();
bitmap = decodeFile(f);
return bitmap;
} catch (Throwable ex) {
ex.printStackTrace();
if (ex instanceof OutOfMemoryError)
memoryCache.clear();
return null;
}
}
// decodes image and scales it to reduce memory consumption
private Bitmap decodeFile(File f) {
try {
// decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
FileInputStream stream1 = new FileInputStream(f);
BitmapFactory.decodeStream(stream1, null, o);
stream1.close();
// Find the correct scale value. It should be the power of 2.
final int REQUIRED_SIZE = 70;
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 1;
while (true) {
if (width_tmp / 2 < REQUIRED_SIZE
|| height_tmp / 2 < REQUIRED_SIZE)
break;
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}
// decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
FileInputStream stream2 = new FileInputStream(f);
Bitmap bitmap = BitmapFactory.decodeStream(stream2, null, o2);
stream2.close();
return bitmap;
} catch (FileNotFoundException e) {
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
// Task for the queue
private class PhotoToLoad {
public String url;
public ImageView imageView;
public PhotoToLoad(String u, ImageView i) {
url = u;
imageView = i;
}
}
class PhotosLoader implements Runnable {
PhotoToLoad photoToLoad;
PhotosLoader(PhotoToLoad photoToLoad) {
this.photoToLoad = photoToLoad;
}
#Override
public void run() {
try {
if (imageViewReused(photoToLoad))
return;
Bitmap bmp = getBitmap(photoToLoad.url);
memoryCache.put(photoToLoad.url, bmp);
if (imageViewReused(photoToLoad))
return;
BitmapDisplayer bd = new BitmapDisplayer(bmp, photoToLoad);
Activity a = (Activity) photoToLoad.imageView.getContext();
a.runOnUiThread(bd);
} catch (Throwable th) {
th.printStackTrace();
}
}
}
boolean imageViewReused(PhotoToLoad photoToLoad) {
String tag = imageViews.get(photoToLoad.imageView);
if (tag == null || !tag.equals(photoToLoad.url))
return true;
return false;
}
// Used to display bitmap in the UI thread
class BitmapDisplayer implements Runnable {
Bitmap bitmap;
PhotoToLoad photoToLoad;
public BitmapDisplayer(Bitmap b, PhotoToLoad p) {
bitmap = b;
photoToLoad = p;
}
public void run() {
if (imageViewReused(photoToLoad))
return;
if (bitmap != null)
photoToLoad.imageView.setImageBitmap(bitmap);
else
photoToLoad.imageView.setImageResource(stub_id);
}
}
public void clearCache() {
memoryCache.clear();
fileCache.clear();
}
}
This is a also my BaseAdapter.java class
public class BRIgeAdapter extends BaseAdapter {
private LayoutInflater inflater;
private Activity activity;
public ArrayList<HashMap<String, String>> data;
public ViewHolder holder;
public ImageLoader imageLoader;
HashMap<String, String> itemList;
private int screenSize;
MainActivity main;
public BRIgeAdapter(Activity a, ArrayList<HashMap<String, String>> d,int screenSize) {
this.activity = a;
this.data = d;
this.screenSize = screenSize;
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader = new ImageLoader(activity.getApplicationContext());
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return data.get(position);
}
public long getItemId(int position) {
return (long) position;
}
#Override
public int getViewTypeCount() {
return data.size();
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
holder = new ViewHolder();
main = new MainActivity();
convertView = inflater.inflate(R.layout.list_row, parent, false);
holder.journal = (TextView) convertView
.findViewById(R.id.smalljournal);
holder.statId = (TextView) convertView
.findViewById(R.id.smallstatID);
holder.smallDescription1 = (TextView) convertView
.findViewById(R.id.smallDescription1);
holder.DateTime = (TextView) convertView
.findViewById(R.id.smallDateTime);
holder.thumb_image = (ImageView) convertView
.findViewById(R.id.smallthumb);
holder.title = (TextView) convertView.findViewById(R.id.smalltitle);
holder.description = (TextView) convertView
.findViewById(R.id.smallDescription);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
itemList = data.get(position);
if (itemList != null) {
holder.journal.setText(itemList.get(MainActivity.KEY_journal));
// holder.DateTime.setText(itemList.get(MainActivity.KEY_pubDate));
holder.statId.setText(itemList.get(MainActivity.KEY_statID));
holder.smallDescription1.setText(itemList.get(MainActivity.KEY_description));
holder.journal.setTypeface(MainActivity.tf2);
String titleString = itemList.get(MainActivity.KEY_title);
Calendar cal = new GregorianCalendar(2013, 11, 20);
DateFormat df = new SimpleDateFormat("dd.MM.yyyy");
// DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
String date = df.format(cal.getTime());
String DateTime = itemList.get(MainActivity.KEY_pubDate);
DateTime = date;
holder.DateTime.setText(DateTime);
holder.title.setText(titleString);
holder.title.setTypeface(MainActivity.tf2);
holder.title.setLineSpacing(2, 1.2f);
holder.description.setLineSpacing(2, 1.2f);
if(screenSize == Configuration.SCREENLAYOUT_SIZE_NORMAL)
holder.description.setVisibility(View.INVISIBLE);
else
holder.description.setText(itemList.get(MainActivity.KEY_description));
holder.description.setTypeface(MainActivity.tf2);
imageLoader.DisplayImage(itemList.get(MainActivity.KEY_image),
holder.thumb_image);
}
return convertView;
}
static class ViewHolder {
public TextView journal, title, description,smallDescription1, DateTime, statId;
ImageView thumb_image;
}
}
Is there any possible way I can reduce or remove this lagging?
If you are using xml then set the width and height of the ListView to match_parent.
I see that you use Threads to load images from url. I Recommend you to use Android Universal Image Loader Library. It is pretty flexible and less laggy. You can find the whole information Here
Also, it looks like you are creating objects in the getView method. This means that when each view is drawn the OS must go through all the work creating those objects for you. If are working with a large list (or small amt of ram) you will force the garbage collector to run, causing stuttering of your app. Try to move all the object creation to the constructor method so it will only run once. Then just reassign them as needed in your get view method.
This should provide a noticable speed improvement.
Good luck ;)
Hi am creating an application which pulls resources from Foursquare and displays it on the android phone and am having problems with loading images. The app runs but for some reason it does not load on the phone it only shows me the loader image which I defined
My code are as follows:
public class PicAdapter extends BaseAdapter {
//use the default gallery background image
int defaultItemBackground;
//gallery context
private Context galleryContext;
//array to store bitmaps to display
private Bitmap[] imageBitmaps;
//placeholder bitmap for empty spaces in gallery
Bitmap placeholder;
//constructor
public PicAdapter(Context c) {
//instantiate context
galleryContext = c;
//create bitmap array
//imageBitmaps = new Bitmap[10];
//decode the placeholder image
//placeholder = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
}
//BaseAdapter methods
//return number of data items i.e. bitmap images
public int getCount() {
return PicturesURLlist.size();
}
//return item at specified position
public Object getItem(int position) {
return position;
}
//return item ID at specified position
public long getItemId(int position) {
return position;
}
//get view specifies layout and display options for each thumbnail in the gallery
public View getView(int position, View convertView, ViewGroup parent) {
//create the view
ImageView imageView = new ImageView(galleryContext);
//specify the bitmap at this position in the array
//imageView.setImageBitmap(imageBitmaps[position]);
ImageLoader imgloader = new ImageLoader(getApplicationContext());
int loader = R.drawable.ic_launcher;
HashMap<String, String> song = new HashMap<String, String>();
song = PicturesURLlist.get(position);
//Log.d("Links", song.get(picurl));
imgloader.DisplayImage(song.get(picurl), loader, imageView );
//imageView.setImageBitmap((imgloader.DisplayImage(song.get(picurl), loader, imageView)));
//set layout options
imageView.setLayoutParams(new Gallery.LayoutParams(400, 600));
//scale type within view area
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
//set default gallery item background
imageView.setBackgroundResource(defaultItemBackground);
//return the view
return imageView;
}
//custom methods for this app
//helper method to add a bitmap to the gallery when the user chooses one
}
My imagloader class
public class ImageLoader {
MemoryCache memoryCache=new MemoryCache();
FileCache fileCache;
private Map<ImageView, String> imageViews=Collections.synchronizedMap(new WeakHashMap<ImageView, String>());
ExecutorService executorService;
public ImageLoader(Context context){
fileCache=new FileCache(context);
executorService=Executors.newFixedThreadPool(5);
}
int stub_id = R.drawable.ic_launcher;
public String DisplayImage(String url, int loader, ImageView imageView)
{
stub_id = loader;
imageViews.put(imageView, url);
Bitmap bitmap=memoryCache.get(url);
Log.d("Image loader succes", url);
if(bitmap!=null){
imageView.setImageBitmap(bitmap);
Log.d("Image loader succes", "true");
}
else
{
queuePhoto(url, imageView);
imageView.setImageResource(loader);
Log.d("Image loader succes", "false");
}
return null;
}
private void queuePhoto(String url, ImageView imageView)
{
PhotoToLoad p=new PhotoToLoad(url, imageView);
executorService.submit(new PhotosLoader(p));
}
private Bitmap getBitmap(String url)
{
File f=fileCache.getFile(url);
//from SD cache
Bitmap b = decodeFile(f);
if(b!=null)
return b;
//from web
try {
Bitmap bitmap=null;
URL imageUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection)imageUrl.openConnection();
conn.setConnectTimeout(30000);
conn.setReadTimeout(30000);
conn.setInstanceFollowRedirects(true);
InputStream is=conn.getInputStream();
OutputStream os = new FileOutputStream(f);
Utils.CopyStream(is, os);
os.close();
bitmap = decodeFile(f);
return bitmap;
} catch (Exception ex){
ex.printStackTrace();
return null;
}
}
//decodes image and scales it to reduce memory consumption
private Bitmap decodeFile(File f){
try {
//decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f),null,o);
//Find the correct scale value. It should be the power of 2.
final int REQUIRED_SIZE=70;
int width_tmp=o.outWidth, height_tmp=o.outHeight;
int scale=1;
while(true){
if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE)
break;
width_tmp/=2;
height_tmp/=2;
scale*=2;
}
//decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize=scale;
return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
} catch (FileNotFoundException e) {}
return null;
}
//Task for the queue
private class PhotoToLoad
{
public String url;
public ImageView imageView;
public PhotoToLoad(String u, ImageView i){
url=u;
imageView=i;
}
}
class PhotosLoader implements Runnable {
PhotoToLoad photoToLoad;
PhotosLoader(PhotoToLoad photoToLoad){
this.photoToLoad=photoToLoad;
}
public void run() {
if(imageViewReused(photoToLoad))
return;
Bitmap bmp=getBitmap(photoToLoad.url);
memoryCache.put(photoToLoad.url, bmp);
if(imageViewReused(photoToLoad))
return;
BitmapDisplayer bd=new BitmapDisplayer(bmp, photoToLoad);
Activity a=(Activity)photoToLoad.imageView.getContext();
a.runOnUiThread(bd);
}
}
boolean imageViewReused(PhotoToLoad photoToLoad){
String tag=imageViews.get(photoToLoad.imageView);
if(tag==null || !tag.equals(photoToLoad.url))
return true;
return false;
}
//Used to display bitmap in the UI thread
class BitmapDisplayer implements Runnable
{
Bitmap bitmap;
PhotoToLoad photoToLoad;
public BitmapDisplayer(Bitmap b, PhotoToLoad p){bitmap=b;photoToLoad=p;}
public void run()
{
if(imageViewReused(photoToLoad))
return;
if(bitmap!=null)
photoToLoad.imageView.setImageBitmap(bitmap);
else
photoToLoad.imageView.setImageResource(stub_id);
}
}
public void clearCache() {
memoryCache.clear();
fileCache.clear();
}
}
I've been working with custom listView containing 1 imageView and 3 textViews.The image is loaded from url,due to which slows down my scrolling.Any help is appreciated..
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) LayoutInflater
.from(context);
convertView = inflater.inflate(R.layout.customvenue, parent, false);
}
Bitmap bitmap = null;
try {
bitmap = BitmapFactory.decodeStream((InputStream) new URL(
VenueArray.get(position).get("VenueImage"))
.getContent());
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
iconImage.setImageBitmap(bitmap);
return convertView;
}
Here VenueArray is hash map array that contains all data
add this class, it will help you out, works like a charm for me
public class ImageLoader {
MemoryCache memoryCache = new MemoryCache();
FileCache fileCache;
private Map<ImageView, String> imageViews = Collections
.synchronizedMap(new WeakHashMap<ImageView, String>());
private ProgressBar indicator;
public ImageLoader(Context context) {
photoLoaderThread.setPriority(Thread.NORM_PRIORITY - 1);
fileCache = new FileCache(context);
}
public void DisplayImage(String url, Context activity, ImageView imageView,
ProgressBar pbar) {
imageViews.put(imageView, url);
Bitmap bitmap = memoryCache.get(url);
if (bitmap != null) {
this.indicator = pbar;
indicator.setVisibility(View.INVISIBLE);
// imageView.setImageBitmap(getRoundedCornerBitmap(bitmap,13));
imageView.setImageBitmap(bitmap);
} else {
this.indicator = pbar;
indicator.setVisibility(View.INVISIBLE);
queuePhoto(url, activity, imageView);
imageView.setImageBitmap(null);
}
}
private void queuePhoto(String url, Context activity, ImageView imageView) {
// This ImageView may be used for other images before. So there may be
// some old tasks in the queue. We need to discard them.
photosQueue.Clean(imageView);
PhotoToLoad p = new PhotoToLoad(url, imageView);
synchronized (photosQueue.photosToLoad) {
photosQueue.photosToLoad.push(p);
photosQueue.photosToLoad.notifyAll();
}
// start thread if it's not started yet
if (photoLoaderThread.getState() == Thread.State.NEW)
photoLoaderThread.start();
}
private Bitmap getBitmap(String url) {
File f = fileCache.getFile(url);
// from SD cache
Bitmap b = decodeFile(f);
if (b != null)
return b;
// from web
try {
Bitmap bitmap = null;
URL imageUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection) imageUrl
.openConnection();
conn.setConnectTimeout(30000);
conn.setReadTimeout(30000);
InputStream is = conn.getInputStream();
OutputStream os = new FileOutputStream(f);
Utils.CopyStream(is, os);
os.close();
bitmap = decodeFile(f);
return bitmap;
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
// decodes image and scales it to reduce memory consumption
private Bitmap decodeFile(File f) {
try {
// decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f), null, o);
// Find the correct scale value. It should be the power of 2.
final int REQUIRED_SIZE = 70;
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 1;
while (true) {
if (width_tmp / 2 < REQUIRED_SIZE
|| height_tmp / 2 < REQUIRED_SIZE)
break;
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}
// decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
} catch (FileNotFoundException e) {
}
return null;
}
// Task for the queue
private class PhotoToLoad {
public String url;
public ImageView imageView;
public PhotoToLoad(String u, ImageView i) {
url = u;
imageView = i;
}
}
PhotosQueue photosQueue = new PhotosQueue();
public void stopThread() {
photoLoaderThread.interrupt();
}
// stores list of photos to download
class PhotosQueue {
private Stack<PhotoToLoad> photosToLoad = new Stack<PhotoToLoad>();
// removes all instances of this ImageView
public void Clean(ImageView image) {
for (int j = 0; j < photosToLoad.size();) {
if (photosToLoad.get(j).imageView == image)
photosToLoad.remove(j);
else
++j;
}
}
}
class PhotosLoader extends Thread {
public void run() {
try {
while (true) {
// thread waits until there are any images to load in the
// queue
if (photosQueue.photosToLoad.size() == 0)
synchronized (photosQueue.photosToLoad) {
photosQueue.photosToLoad.wait();
}
if (photosQueue.photosToLoad.size() != 0) {
PhotoToLoad photoToLoad;
synchronized (photosQueue.photosToLoad) {
photoToLoad = photosQueue.photosToLoad.pop();
}
Bitmap bmp = getBitmap(photoToLoad.url);
memoryCache.put(photoToLoad.url, bmp);
String tag = imageViews.get(photoToLoad.imageView);
if (tag != null && tag.equals(photoToLoad.url)) {
//System.out.println("indicator ::" + indicator);
BitmapDisplayer bd = new BitmapDisplayer(bmp,
photoToLoad.imageView, indicator);
Activity a = (Activity) photoToLoad.imageView
.getContext();
a.runOnUiThread(bd);
}
}
if (Thread.interrupted())
break;
}
} catch (InterruptedException e) {
// allow thread to exit
}
}
}
PhotosLoader photoLoaderThread = new PhotosLoader();
// Used to display bitmap in the UI thread
class BitmapDisplayer implements Runnable {
Bitmap bitmap;
ImageView imageView;
// private ProgressBar indicator1;
public BitmapDisplayer(Bitmap b, ImageView i, ProgressBar indicator) {
this.bitmap = b;
this.imageView = i;
// this.indicator1 = indicator;
}
public void run() {
if (bitmap != null) {
imageView.setImageBitmap(bitmap);
}
}
public void clearCache() {
memoryCache.clear();
fileCache.clear();
}
}
Use Universal Image Downloader to display the images.
It works fine for me hope will work for u...!