I am changing a image in an ImageView while already displaying the image. This COULD leed to a change of the layout as the image may be of a different size.
I draw some arrows/marks to special points on an image and animate them... My problem now is, that I need to wait for the setImage... function of the ImageView to finish so that I can calculate all necessary data under consideration of the new image...
first update of image
On my first displayment of my view I wait with ViewTreeObserver().addOnGlobalLayoutListener of my ImageView and calculate all points afterwards...
on following updates of the image (view is already visible)
I don't know how to do it here...
How can I wait for setImage... to finish drawing and only afterwards do my custom work?
Try listening to the ImageView's layout change instead.
Call addOnLayoutChangeListener(...) on the ImageView.
You can recalculate the points every time onLayoutChange... is called.
Actually, I found the solution and it was quite obvious...
Exactly for this reason, view.post(...) exists and this works quite good... This way, I don't need the OnGlobalLayoutListener at all...
Related
I have a ViewPager of ImageViews, and I overrided the onDraw(..) of the ImageViews so that I draw stuff on top of the imageview. Throughout the run of the app I change these drawings so I manually call invalidate() to make onDraw() get called (and thus updating the drawings).
The weird thing is, the only onDraw() that is called is for the first ImageView and not the others. (Meaning only to the ImageView that is displayed first. After scrolling to the next ImageView, onDraw() stops being called)
I've called setWillNotDraw(false) in the constructor of the ImageView.
Did anyone have the same experience? Why is this happening?
One reason that the onDraw method may not be called for an ImageView is if you haven't set the image resource (i.e. ImageView src parameter) If you're setting it programmatically that would be done by a call to setImageResource. Make sure you're specifying the image resource properly and that the resource exists.
I discovered this issue making a custom control for displaying animated GIFs. For sdk < version 28, I used android.graphic.Movie to accomplish this, and for sdk >= version 28, I an using the new ImageDecode. In order to accomplish that my custom control had to be extended from ImageView. When I did that, my onDraw method no longer got called. The problem went away when I set the image resource. In the case when I using Movie to render the GIF, I'm not really using the ImageView for doing the rendering, but I still had to give Image view a dummy image resource just so I get a call to the onDraw method. The use of Invalidate and SetWillNotDraw(false) methods will not solve this kind of problem.
Is there any way to create a bitmap that, when you use it in on a view in the setImageBitmap method, it looks like no image was actually set at all?
EDIT: To clarify, I want the bitmap to not be visible, or it can be visible, as long as its nothing, as long as the user cant see it im happy.
EDIT2: I cannot use setVisibility because my ImageView also has a setBackground attribute that I want to remain visible. If I set the view to invisible, the background AND the bitmap image are affected, and I dont want that. I only want the bitmap image to be invisible.
You could call imageView.setImageBitmap(null) to remove the current image and keep the background.
You could make a 1x1 pixel bitmap which is nothing but empty canvas (in photoshop perhaps), then use the draw9patch tool to make it stretch.
EDIT: If you just need it not to draw, you can call setVisibility(View.INVISIBLE) on the view like so:
imageView.setVisibilty(View.INVISIBLE)
← Image is here
above is a 50X50 transparent image created in photoshop download and use it,
I use an ImageView to display the entire image. I want to launch the next activity when the sun is touched. Is it possible by using this single image or should i use separate imageview for sun? I know how to do it with separate imageview. I want to know whether it is possible by using only the full image?
Use two imageviews
Easier and less bug-prone
I need to show a certain number of images within a imageview dynamically, every few seconds, I need show in that imageview around 40 images, without onclick events or others, just go changing the image, preferably obtain them from a website, how can I start with this?
really would appreciate your help.
Use a Handler to change the image each time delay you specify. Post a Runnable that changes the image with whatever time delay you wish.
Often people use an async-task for loading things online. I personally have never needed to, but you may need to load the images in an async task because it can slow up the UI and prompt a force-quit. Check on that though.
Bitmap bmp = MediaService.getDisplayImage("image_url", getApplicationContext());
((ImageView)findViewById(R.id.yourimageviewid)).setImageBitmap(bmp);
in this way you can retrieve image from network and set into your ImageView... rest all shuffling images etc detail you can manage with using timer..
My application uses thumbnails downloaded from the internet. The thumbnails are generated by the server, and one specifies in the request what size the thumbnails should be. Is there a way for me to know, programmatically, while inside a ListAdapter's getView(), the dimensions of an ImageView inside the View that is returned, so my thumbnail comes perfectly scaled? I can always hardcode it, but I'd prefer not to.
Alternatively, is there some sort of listener or callback I can use to request the thumb once the View has been laid out?
EDIT: The ImageViews have fixed layout_height and layout_width, however, I'd want to reuse the same code for different parts of the app, which use different size images, which is why hardcoding is particularly bad.
You won't know dimensions at the construction phase of the ImageView in getView.
(obviously convertViews are different).
I have gotten round this by creating a subclassed ImageView that implements the View.OnLayoutChangeListener onLayoutChanged method and triggers loading of the image
via our resizer proxy (and via a local memcache). Whether this is the most elegant way, I don't know but it works. It all seems ugly compared to iOS!
See also:
Android: Finding size (resolution-wise) of a GalleryView.