Image view reference in adapter using volley android - java

Hi suppose i am loading image in imageview using volley in adapter. if image loading completed then onResponse(ImageLoader.ImageContainer response, boolean arg1) fn will be called. my problem is that whether i will get the correct reference of the corresponding image view which is loaded inside onResponse ??
i need to change the scale type of image view after image is loaded.
netImageLoader.get(thumbUrl, new ImageLoader.ImageListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.e("Error", "Image Load Error: " + error.getMessage());
holder.postImage.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
holder.postImage.setImageResource(R.drawable.placeholder_video_image);
}
#Override
public void onResponse(ImageLoader.ImageContainer response, boolean arg1) {
if (response.getBitmap() != null) {
// load image into imageview
holder.postImage.setScaleType(ImageView.ScaleType.CENTER_CROP);
holder.postImage.setImageBitmap(response.getBitmap());
}
}
});

Related

How to display an image after a gif

I am new to android. I implemented a GIF in my activity.
I found a way to make it loop only once. So far everything is fine but I would like to display the image of this gif at the end of the animation of this one, this time in image and not in gif. Basically I would like the gif to loop once and then become an image again
I implemented this on my build.gradle :
implementation 'com.github.bumptech.glide:glide:4.12.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.12.0'
After that I set up my imageView :
<ImageView
android:id="#+id/fuse"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="#dimen/_4sdp"
android:layout_marginLeft="#dimen/_4sdp"
android:padding="#dimen/_8sdp"
android:src="#drawable/fuseev4"
tools:ignore="InvalidId" />
Now this is my java file to loop once:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityChatRoomBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
imageView = binding.fuse;
try {
setListeners();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void setListeners() throws InterruptedException {
binding.LayoutSend.setOnClickListener(view -> {
try {
Glide.with(this)
.asGif()
.load(R.drawable.fuseev4) //Your gif resource
.apply(RequestOptions.diskCacheStrategyOf(DiskCacheStrategy.NONE))
.listener(new RequestListener<GifDrawable>() {
#Override
public boolean onLoadFailed(#Nullable #org.jetbrains.annotations.Nullable GlideException e, Object model, Target<GifDrawable> target, boolean isFirstResource) {
return false;
}
#Override
public boolean onResourceReady(GifDrawable resource, Object model, Target<GifDrawable> target, DataSource dataSource, boolean isFirstResource) {
resource.setLoopCount(1);
return false;
}
})
.into(imageView);
}
catch (Exception e){
System.out.println(e);
}
afterListeners();
});
}
public void afterListeners(){
Glide.with(this)
.asBitmap()
.load(R.drawable.fuseev4)
.into(imageView);
}
I couldn't find a way to solve my problem.
Thanks in advance for your help !
Try this in your onResourceReady() callback:
resource.registerAnimationCallback(new Animatable2Compat.AnimationCallback() {
#Override
public void onAnimationEnd(Drawable drawable) {
super.onAnimationEnd(drawable);
// your code here
}});
1)as i read you are using bumptech.glide liabrary so try code like below,
private val image = "abc.png"
Glide.with(this).load(image).placeholder(R.drawable.placeholder) .into(binding.imageView); //here loading gif
comment if any query

Android Spinner load with empty value (popup is shown before populating data)

I am using https://github.com/Chivorns/SmartMaterialSpinner Library for spinner.
When i first click the spinner no adapter is attached to it or spinner items shows empty list..
like
but when i close it and again select it.. data are shown so I believe that .. due to time consuming data fetching .. spinner show empty dialog.. Now I need to handle that.. How can i only show spinner when data is populated or available.. I have tried async task and handler but not get working.. any hint would be appreciated.. Thank you
Edited ...
I have call api on spinner onTouch event to populate data
{
private void ProvinceSpinnerCode(boolean IsEditCase) {
if (IsEditCase) {
//edit case condition
} else {
provinceSpinner.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
mService = RetrofitManager.getApiClient().create(RetrofitInstance.class);
Call<List<SelectModel>> provinceCall = mService.GetProvinces();
provinceCall.enqueue(new Callback<List<SelectModel>>() {
#Override
public void onResponse(Call<List<SelectModel>> call, Response<List<SelectModel>> response) {
if (response.body().size() >= 0) {
provinceSpinner.setAdapter(new SpinnerAdapter(response.body(),AddCustomerActivity.this));
}
}
#Override
public void onFailure(Call<List<SelectModel>> call, Throwable t) {
}
});
return false;
}
});
}
provinceSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(AddCustomerActivity.this, "Id is : " + id, Toast.LENGTH_SHORT).show();
provinceId = (int) id;
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
}
You are making an API call inside on touch event which will be fired when you will touch the spinner so to solve the problem just do Api call out side that event (may be inside Oncreate).
Right now provinceSpinner has a touch listerner so when you are touching it to open for the first time it is Fetching data so when you are clicking it again the alert is showing data (the data you fetched when you clicked first time)

How to wait Picasso to finish load Images in Android?

So, I have an activity, lets say a PetDetailActivity that shows a carousel with some Bitmaps in it (I use com.synnapps:carouselview:0.1.5 to handle my carousel). The problem is that the PetDetailActivity loaded with 0 sized carousel, which maybe the images is still being processed by a thread. How to wait Picasso to finish processing URLs, and then show it up in the new Activity?
This is the code of PetDetailActivity:
import ...
public class PetDetailActivity extends AppCompatActivity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pet_detail);
Intent i = getIntent();
Pet targetPet = (Pet)i.getSerializableExtra("PetObject");
ActionBar actionBar = getSupportActionBar();
if(actionBar!=null) actionBar.setDisplayHomeAsUpEnabled(true);
//Creating a BitmapHandler object to download image from URL to Bitmap object using picasso.
BitmapHandler bitmapHandler = new BitmapHandler(targetPet.getCarouselImageUrl());
final ArrayList<Bitmap> petCarouselBitmaps = bitmapHandler.getProcessedBitmap();
//The bitmap is being downloaded in other thread, so the activity is up and
//CarouselView is still empty (petCarouselBitmaps.size() == 0)
//So how to wait the bitmaps is processed, like show a loading screen on the UI?
CarouselView petCarousel = findViewById(R.id.petCarousel);
petCarousel.setPageCount(petCarouselBitmaps.size());
petCarousel.setImageListener(new ImageListener() {
#Override
public void setImageForPosition(int position, ImageView imageView) {
imageView.setImageBitmap(petCarouselBitmaps.get(position));
}
});
}
...
}
And Here is the BitmapHandler Class that downloads image from URL to a Bitmap using picasso:
public class BitmapHandler extends Thread {
ArrayList<String> urlList;
private ArrayList<Bitmap> loadedBitmap;
public BitmapHandler(ArrayList<String> list){
this.urlList = list;
this.loadedBitmap = new ArrayList<>();
}
public ArrayList<Bitmap> getProcessedBitmap(){
this.run();
//Returning the loaded bitmap as a ArrayList<Bitmap> Object.
return loadedBitmap;
}
#Override
public void run() {
Target target = new Target() {
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
loadedBitmap.add(bitmap);
}
#Override
public void onBitmapFailed(Exception e, Drawable errorDrawable) {}
#Override
public void onPrepareLoad(Drawable placeHolderDrawable) {}
};
for (String url : urlList) {
Picasso.get().load(url).into(target);
}
}
}
Thank you for any helps!
Problem: . How to wait Picasso to finish processing URLs
Solution:
I think you can go with Target callback:
private Target target = new Target() {
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
}
#Override
public void onBitmapFailed() {
}
}
And while loading image, you need to write:
Picasso.with(this).load(myURL).into(target);
Just for the information:
onBitmapLoaded() mostly used to perform image operations before we load into the view actually.
Picasso.LoadedFrom describes where the image was loaded from, whether it's MEMORY, DISK or NETWORK.
I think you can use placeholder & then the image is loaded it will show in Image View.
And if you want to delay use can use Thread.sleep(5000).

Picasso keeping the old image until new image loaded

I have a fragment that i'm calling again and again it has Picasso in it to load images from url when I pop back same fragment with different url to load image from, the previous image shown until new image loaded, how can i solve this problem, here is my code:
Picasso.with(context)
.load(url)
.networkPolicy(NetworkPolicy.NO_CACHE)
.into(ivParallex, new com.squareup.picasso.Callback() {
#Override
public void onSuccess() {
getProductDetail(productId);
} else {
}
#Override
public void onError() {
}
});

Wrong Position Is Returned

This is the strangest thing. I have an Arraylist being called into an imageview. The image returned is in the position it should be when the image is clicked but when I use a Log, the image returned is the next image, instead of the image in the position.
Ion.with(getApplicationContext())
.load(mImages.get(position))
.noCache()
.progressBar(progressBar)
.progressHandler(new ProgressCallback() {
#Override
public void onProgress(long downloaded, long total) {
progressBar.setVisibility(View.VISIBLE);
}
})
.intoImageView(imageView)
.setCallback(new FutureCallback<ImageView>() {
#Override
public void onCompleted(Exception e, ImageView imageView) {
progressBar.setVisibility(View.GONE);
}
});
And the log right underneath:
Log.d("position of url = ", (mImages.get(position));
This is all placed in a PagerAdapter with the position of the viewpager sourced from a gridview. What could be wrong?
I figured this out.
position=viewPager.getCurrentItem();
and it works fine now!

Categories