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..
Related
Given:
A number of images (10 - 15) residing in assets folder (as practice shows, the better approach is to keep high-resolution images in assets)
Android UI thread (caching drawables in advance is already made in a background thread)
The issue:
Need to display all the images one on another smoothly and not blocking UI thread after the images are drawn.
Already used approaches:
Dynamically create a required number of ImageView and then call .setImageDrawable(). This takes a lot of time but the worst thing is that the UI thread is being blocked even after all the images are drawn on their ImageView.
Create a LayerDrawable object and pass as argument an array of the required Drawables. Then put it on an ImageView also by calling .setImageDrawable(). This option behaves the same like the one described above.
Is there a way to solve this issue? Or Android devices not capable to cope with it?
Try Picasso library:
http://square.github.io/picasso/
It has support for resources:
Picasso.with(context).load(R.drawable.landing_screen).into(imageView1);
Picasso.with(context).load("file:///android_asset/DvpvklR.png").into(imageView2);
Picasso.with(context).load(new File(...)).into(imageView3);
If you are requiring a smooth transition between images, please try viewFlipper
https://developer.android.com/reference/android/widget/ViewFlipper.html
Basically you can add imageView inside the viewflipper and start animation to move from previous to next image. I am not sure if you need user interactions like scrolling or not. You can try viewPager if user need to scroll betweens images.
Please note that Glide does provide some transition effects for switching between placeholder and image to display. But transition between images are not included, you can have a look here
http://bumptech.github.io/glide/doc/transitions.html
I hope this helps because i am not sured that if you are asking the transition between images or rendering images.
I need some help,
I am creating an app, and want it to run faster I mean when the app is started first it shows a blank white screen for o 1-2 seconds and then loads images. I have a layout background image, and 4 imageviews which are clickable and take you to the next activity. I read somewhere i should use threads to load images and it will load them on a separate thread faster, but i have some problem using it.
So here are the problems and android studio explanations:
Thread thread=new Thread(
public void run(){
ImageView tipka=(ImageView)findViewById(R.id.tipkaproba);
tipka.setImageResource(R.drawable.instructions);
LinearLayout asd=(LinearLayout)findViewById(R.id.layoutproba);
asd.setBackgroundResource(R.drawable.backfround123);
}
).start();
Now the android studio says:
After
"Thread(" )expected
Before
"public void run(){"
; expected
On ").start();"
Invalid method declaration; return type required, Missing method body, or declare abstract.
Now i would like to know:
Does this speed up loading images, ( if not how to do it then)
How to fix my errors.
Thanks anyway !
Do all images fit in the screen?! If users need to scroll to view other images why load all of them at once? Use a grid view with adapter to load the image. In this way when the app start only images on the screen will be loaded and then when the user scrolls other images show up!
another thing you can do is to have 2 version of each image. one low quality with small size that loads first and one for high quality image. that will load latter. You can also calculate the base color for each image (use open source code or do it manually). then set the background image of iamgeview to this color. So when the image finally loads on the screen. The difference is not as dramatic as it was before.
If you want to use thread try AsyncTask first. Using AsyncTask is simpler than defining thread yourself.
Try to decrease the size of your images! If you be able to do this. it works better than any other trick! It's mobile, you don't have to show supper high quality images. users don't even notice most of the time
You probably took care of this but it worth mentioning that you need to provide different images for different screen densities and it's critical for performance as well as quality.
I was looking over internet a lot, but only found examples how to text is changed every second or show even miliseconds.
I was using those examples to change picture when second was change, but timer was not accurate anymore. On image change the app respond with random delay for about 20 milisec or 200 milisec.
I was using class Runnable with postDelay and AsynTask where onPostExecute refresh UI, but both working exactly the same with lag.
What is the best practice to change picture every second?
And in the future I have an idea to start animation of 5 images every second (more fancy clock timer).
I would post my code, but now is really a mess of all examples I found till now.
well you have not shared the code so I have to guess only,
I would suggest to have the images as bitmap or drawable initialized already, may be in a hashtable.
After that, start your timer and place the code to replace the image at right place.
Point is you should not spend time other than replacing image in you thread.
Edited:
If you want to do it via code :
before starting your animation create an array of Bitmaps not drawable ids (or the object of Drawble class).
Why because you need to save time spent by system in creating the bitmaps from the ids you provide in array and then set to you image view.
Also, you can use AnimateDrawbles for this which is very easy optimized and reliable.
here is a sample.
Just tune the duration variable in the xml according to your need.
I have a listactivity and each row has a different image that i load from the web and some text.
I'd like to implement asynctask so the images would load on the background.
I've searched many tutorials but haven't found none that use different images per row. The problem is the method doInBackground that is supposed to load the images from the internet. But since they are all different and they depend on its row, how can the thread load them?
Global variables: Bitmap bm; BitmapFactory.Options bmOptions;
In my getView method, I have
ImageView img = (ImageView) v.findViewById(R.id.icon);
if (img!=null){
bm = LoadImage(o.getLink(), bmOptions);
img.setImageBitmap(bm);
}
along with the text load... this img should be load on the doInBackground method, but then I'd have no access to the o (of type User) object that I know because getView gives me the position.
Does anyone have any idea on how to solve this problem?
Thanks,
Rita
There are two ways you can do this: the easy way or the boring way.
If you can add extra dependencies, then I would strongly recommend you take the easy route and use GreedDroid. You can use the item framework they have in there or use the AsyncImageView. Personally I'd use the latter.
The boring way, on the other hand, would be to break out the HttpClient and download the image manually, then use BitmapFactory.decodeStream method to create a Bitmap.
The downloading needs to happen away from the UI thread, so it may feel natural to use an AsyncTask to run it all, and the use a Handler to actually poke the Bitmap into the ImageView (being careful not to set the Bitmap on a view which has already been recycled).
Unfortunately, on many implementations, the ExecutorService that runs the AsyncTasks are limited to using one Thread. This gives an unfortunate feel to the List where the images load one by one, instead of the as-many-as-possible-all-at-once feel to it you may be expecting.
In that case, you'll probably be wanting to (essentially) roll your own AsyncTask.
I'm not a massive fan of AsyncTasks, as you can tell.
The above is such a common task, and such an annoying thing to get right, I'd very much recommend using someone else's library.
I believe you will need to write your own List Adapter so that as more and more list items are displayed (by scrolling down for example), the adapter creates more AsyncTasks and displays the images once they have been fetched.
http://developer.android.com/reference/android/widget/ListAdapter.html
This is where images will be downloaded using async task just have look
http://developer.android.com/resources/articles/painless-threading.html
In my Android App I have a listview containing 30 rows, and each row consists of several textviews of which one is spannable and sometimes contains a lot of formatted text and images.
Those images are loaded from the web asynchroneously: A placeholder is displayed until the image has been downloaded is then replaced by the image.
Unfortunately, the rows of the listview are loaded when I scroll over them. This makes the whole thing very slow. Also, those images are loaded from the web again and again, whenever I scroll over the row.
How can I turn it off, that the ListView rows are loaded when I scroll over them? They should be loaded once when I start the activity and never again.
Best regards and thanks in advance,
Jan Oliver
When you do a lazy-loading ListView, is because you want to speed it up your app. Turn it off is not the best solution. So, what you can do is implementing a basic cache system in order to avoid downloading and setting the ImageView again and again.
The easiest way to do so is implementing a HashMap with URLs as keys and Bitmaps as values. Something like this:
Map cache = new HashMap();
// then, on your lazy loader
Bitmap image = cache.get(urlOfTheImage);
if( image == null ){
// download and decode the image as normal,
// then assign the decoded bitmap to
// the 'image' variable
cache.put(image);
}
imageView.setImageBitmap(image);
If those images will be the same always, meaning that each time you open the app the same images will be downloaded, then you better save those images in the filesystem and use them from there.
On the other hand, if the images tend to change, you could implement some interesting stuff: use SoftReferences. There's an explanation in this video. This can also be used if you are loading images from the filesystem.
Edit
With regards to your comment, I highly recommend you watching the video I posted. It's one hour long, but it really worths the effort. When using an adapter, checking if the convertView is null is just a simple way to improve performance, though there are some other techniques that will improve your app even more. Also, if you had have problems while using that trick, is because you are probably implementing it the wrong way. Remember: even if you don't re-inflate the views, you do have to set the value of each one of the children views, otherwise you will experience some problems.
If you can, start with an Image Array full of the "placeholder images", then download the images in to an Array firing an AsyncTask during on Create. During row view building just refer to the array. That way if it has the new image it will load it, if not it will get the placeholder.
If you have a lot of data its gonna get real slow and be a crappy expirience for the user.
Create a list of objects that represent each row. Create a loader as a background thread that updates the objects as it loads the data. Your list view will draw data from the objects.
(Not a good idea if you have hundreds of rows and a huge amount of data in each row - in that case, you should only load data within a few rows of the currently active row and have some sort of MRU cache).