I am using Picasso for background loading of images (list view and ImagePager). I can set loading image and errorimage with Picasso, but I am unable to show a "loading in progress" Image during background loading.
Has anybody an idea how to get this done? In my PagerAdapter Class I have the following method:
#Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imageView = new ImageView(context);
int padding = context.getResources().getDimensionPixelSize(R.dimen.padding_medium);
imageView.setPadding(padding, padding, padding, padding);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
// Set default Image before loading real image
imageView.setImageResource(R.drawable.img_def);
imageView.setId(imgViewId);
imageView.setClickable(true);
imageView.setOnClickListener(this);
((ViewPager) container).addView(imageView, 0);
// Set default Image before loading real image
Picasso.with(context).load(R.drawable.img_def).into(imageView);
// Load real image with Picasso and show error image if failed
Picasso.with(context).load(GalImgUrls[position]).error(R.drawable.no_img).into(imageView);
return imageView;
}
However, it does not show the "loading..." image. Any solutions/ideas?
Thanks!
Looks like I found a solution myself. Use "placeholder" image:
Picasso.with(context).load(tnu).error(R.drawable.no_img).placeholder(R.drawable.img_def).into(holder.immoPic);
Works like charm....
Related
Into this adapter, I build dynamically the imageview size into the viewholder constructor:
ContentViewHolder(View view, Context context) {
super(view);
ButterKnife.bind(this, view);
// Set image width + height
mImageView.getLayoutParams().width = Math.round(UIUtils.getScreenWidth() / 4f);
mImageView.getLayoutParams().height = Math.round(
mImageView.getLayoutParams().width / Defines.FORM_LIST_FORMAT);
}
To show images, I use Glide (with crossfade effect; I tried without and the result is the same...).
And the result is not good: the first images have a bad image displaying...
Could you give some ways to fix it please?
Add android:scaleType="centerCrop" in your XML layout, or mImageView.setScaleType(ImageView.ScaleType.CENTER_CROP) if you want to do it programmatically.
I am using PhotoView with Picasso and when I load images into ListView
1) It makes some images smaller.
2) not all images are loaded on launch, when i scroll down an then up then all the images are loaded.
Without PhotoView images loads just fine. I tried so many ImageView zooming libraries but PhotoView works better and I have also tried it with Glide the result is same.
Is there anyway I could force PhotoView to fit to ImageView attributes (which are Match_Parent)?
Here is my Adapter
#Override
public void onBindViewHolder( final ViewHolder holder, int position) {
Picasso.with(context)
.load((Integer) data.get(position))
.resize(530,999)
.onlyScaleDown()
.centerInside()
.into(holder.image);
holder.mAttacher = new PhotoViewAttacher(holder.image);
holder.image.setTag(holder);
}
#Override
public int getItemCount() {
return data.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public PhotoView image;
public PhotoViewAttacher mAttacher;
public ViewHolder(View view) {
super(view);
image = (PhotoView) view.findViewById(R.id.imagview);
I am loading images from resources/drawable not from any url
Unfortunately, there is an issue at Picasso, more details at issue #364
I switched to another library Glide
You can change your attacher scale type:
holder.mAttacher = new PhotoViewAttacher(holder.image);
holder.mAttacher.setScaleType(ImageView.ScaleType.CENTER_CROP);
CENTER_CROP: Scale the image uniformly (maintain the image's aspect ratio) so that
both dimensions (width and height) of the image will be equal to or
larger than the corresponding dimension of the view (minus padding).
The image is then centered in the view.
i cannot load an image in picasso from my drawable due to resons known only to picasso, so whever picasso fails to load i want to load a default image please help
#Override
public Object instantiateItem(ViewGroup container, final int position) {
final Context context =getApplicationContext();
final ImageView imageView = new ImageView(getApplicationContext());
int padding = context.getResources().getDimensionPixelSize(
R.dimen.padding_medium);
imageView.setPadding(padding, padding, padding, padding);
PicassoTools.clearCache(Picasso.with(context));
((ViewPager) container).addView(imageView, 0);
imageView.setTag("myview" + position);
Picasso.with(context).load(mImages[position]).resize(320,280).centerInside().placeholder(placeholderDrawable)
.into(imageView,new Callback() {
#Override
public void onError() {
// TODO Auto-generated method stub
imageView.setImageResource(R.drawable.c3);
}
#Override
public void onSuccess() {
// TODO Auto-generated method stub
}
});
iv included callback in the hope to do something but my brain is not working, any1 help
Picasso.with(context).load(www.google.com/image/1).placeholder(context.getResources().getDrawable(R.drawable.default_person_image)).error(context.getResources().getDrawable(R.drawable.default_person_image)).into(pictureView);
This is what i'm currently using (placeholder URL of course). It will try and load the image you provide in the "load()" part, will show the "placeholder()" part before it has downloaded the image, and if it fails it will show the "error()" part.
Personally i have both the placeholder() and error() part to show the same image, but you can load two different images.
I have a GridView with 4 columns and n-rows. Each cell is simply an ImageView. My custom adapter's getView() code looks like this:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
} else {
imageView = (ImageView) convertView;
}
imageView.setLayoutParams(new GridView.LayoutParams(100, 100));
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setPadding(1, 1, 1, 1);
//imageView.setAdjustViewBounds(true);
if(imageLoader != null) {
String url = randomImage();
imageView.setContentDescription(url);
imageLoader.DisplayImage(url, imageView);
}
return imageView;
}
The imageLoader will pick a random URL and in the background, go fetch it and update the ImageView accordingly. That works fine and dandy. The problem is that the resulting images seem to be using a scale type of FIT_START instead of FIT_XY. I'm explicitly setting this to FIT_XY and even set it again inside of the code that sets the imageView's drawable... still its not FIT_XY and i'm stumped. Ideas?
Edited to remove the call to setAdjustViewBounds().
So I just found the issue. I was using Romain Guy's code for creating a RoundedDrawable from a bitmap -- to get rounded corners, ala iOS, as shown here: curious-creature.org/2012/12/11/… ... when i skip the RoundedDrawable conversion, it works fine. Something in the RoundedDrawable code is what is throwing this whole thing off. I'll just need to find another method to round the corners of the imageview. I am using CENTER_CROP now thanks to #kcoppock ! :)
one again facing a "strange" problem.
Im using a Viewpager to display 2 separate images (Viewpager contains just a layout and 2 imageviews).
The concept would be, display a low resoltion image from local file cache (immediately) and load the high-res picutre meanwhile and show it.
the problem is: only using low res pictures, pictures are showing immediately and everything perfect, but as soon as high-res pictures are enabled (to be shown),
if the user swipes really fast, the screen stays black for "a short time" (500ms to 1,5s) and low resolution images are never displayed.
just the high-res pictures..
maybe anyone faced a similar problem, any assistance appriciated :) thank you!
ViewPager code:
/**
* Create and add a new page of the image at the given position.
*
* #param collection
* #param position
*/
#Override
public Object instantiateItem (View collection, final int position) {
Log.v(TAG, "instantiateItem: pos: " +position);
final Context context = collection.getContext();
RelativeLayout layout = new RelativeLayout(context);
ImageViewTouch imageView = new ImageViewTouch(context);
ImageViewTouch imageView2 = new ImageViewTouch(context);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT);
layout.addView(imageView, lp);
layout.addView(imageView2, lp);
imageView.setOnSingleTapConfirmedListener((OnImageViewSingleTapConfirmedListener) context);
imageView2.setOnSingleTapConfirmedListener((OnImageViewSingleTapConfirmedListener) context);
imageView2.setVisibility(View.GONE);
if (position == restorePageNumber) {
loadBitmap(context, position, imageView, imageView2, restoreZoomLevel);
Log.w(TAG, "zoom level regocnized for pos: " + position + " resetting...");
restorePageNumber = Constants.INVALID_INT;
restoreZoomLevel = Constants.INVALID_LONG;
} else {
loadBitmap(context, position, imageView, imageView2, Constants.INVALID_LONG);
}
imageView.setFitToScreen(true);
imageView2.setFitToScreen(true);
activePages.put(position, imageView2);
((ViewPager) collection).addView(layout);
return layout;
}
protected void loadBitmap (Context context, int position,
ImageViewTouch imageView, ImageViewTouch imageView2,
float restoreZoomLevel) {
Photo photo = getPhotoAtPosition(position);
Log.v(TAG, "loading photo. pos: " + position + " id: " + photo.getId());
// show small image first
if (!(photo instanceof CameraPhoto)) {
StorageManager.retrieveBitmapBackgroundWithImageview(context, photo,
Photo.SIZE_SMALL, imageView, true, Constants.INVALID_LONG);
}
// afterwards replace with big image
StorageManager.retrieveBitmapBackgroundWithImageview(context, photo,
Photo.SIZE_LARGE, imageView2, true, restoreZoomLevel);
}
in Those methods (retrieveBitmapBackgroundWithImageview) Images are loaded in Background, and afterwards set to the imageview.
It seems that it has some problem with setting the large bitmap.
Even if the Imageview with the large bitmap stays hidden (View.GONE), and only local cache images are shown, the ViewPager stays black for some "time" (as above, 500ms to 1.5s) on loading pages, if swiping fast :)
thx :)
If you mean swiping fast to alot of pages it just has problems with loading all those views. Do you cancel/interrupt the loadBitmap tasks that are out of the range of the ViewPager (not in view or cache)?
And, do you handle concurrency in threads? If you always want to load the pages that are in the ViewPager's view or cache first, you should Override Comparator and let it compare on the page. The comparator can be used in a PriorityBlockingQueue which can be used in a Executor