I have an ImageView. When user presses button I want to change image (it is animation-list) and run this animation. I used to use setImageResource, but it blocks UI thread and causes lags. I can't predict what image I would set befor user presses button. I tried to preload drawables but it causes OOM, because I have about 30 xmls with animation-list. How can I solve It? To sum up, I want to fastly change image in my ImageView and then start frame animation on it.
I think you are performing a fetch operation on the UI thread for the image(either from storage or network call). That is what is causing the lag.
You might look into using a library to load up your images. There is one that is really easy to implement called Picasso. It's been around for a while too, so it should be easy to see some examples.
Related
I have an application in java swing and geotools. I have displayed a map image and trying to add images as layers to it. When it adds layer it blinks and after some time at the call to addLayer function map image and other few layers disappear for some time. How to stop this? I am loging all the information at the same time in SQLite and then to my own customised file.
I have stopped trying refreshing all after addLayer function call. The all process should work smoothly without blinking and disappearing. Please Help.
The gt-swing module is really only intended for simple demonstrations. If you intend to use it in production you should add in off screen buffering to allow "fast" refreshes of the map without rereading all the displayed data.
I am trying to make an App which is going to display Some Images and Videos. So I am planning to add a splash screen of around 2seconds. After 2 seconds the user will be taken to the Main Screen of the App.
I want to start the loading of the Images, Video when then user is at the splash screen itself so that the user should wait for the least time when he is at the Main Screen.
So the loading will be started at the Splash Screen and then after two second the user will be taken to main screen irrespective of the completion of the loading.
Now since this involves two activities should I use a Async task or should I Use a service with an Async Task(For the callback of completion of code) within it?
Which one would be better. Also in Android 8.0 are there any restriction in using Services?
I think using a Async Task between two screens may cause Memory leak if not coded properly.
Any help would be really grateful.
EDIT: My app is having one more feature hence cannot make the user wait in the Splash Screen till the loading is over.
It is not very good to use AsyncTask for sharing results between 2 activities, because AsyncTack created in Splash activity will be destoyed (stopped) when switched to Main activity. Better to use service in this case and Main screen will subscribe for result.
So basically you want to start the download in the splash screen and continue the download in the activity that follows. In this way, you still have to implement a loading animation. In your case, I would recommend finishing your splash screen, as soon as everything is downloaded. In that way, you don't have to download anything anymore inside the app's lifecycle.
AsyncTasks continue to run even after switching to a new activity. You can try the following flow:
1. Splash screen
2. Trigger Async Task
3. Main Activity
4. Show Images/Videos
The only catch is, you will not be able to fix a time for #2 to complete to be able to start #4. This is the nature of AsyncTasks. You can workaround by using the OnPostExecute within AsyncTask.
Example: OnPostExecute call another method that will enable a button. Users can click on the button to view Images/Videos. But then, this might not be a good user experience to see some button suddenly getting enabled.
In that case I would rather create some Singleton with own Handler (that works in separate Thread), that will be started at Splash screen. After Main screen will start, it should ask that Singleton about respective data or should sign himself for receiving that data.
I have an Android app that offers syntax highlighting for source code files. It's performing very poorly for large files such as this and freezing the UI for 10+ seconds. I have profiled my app and a method out of my control appears to be taking up a lot of time on the main thread. I would like to implement lazy rendering / 'pagination' so that instead of coloring the whole file when the user loads it, I only color what the user sees at first, and color in uniformly-sized chunks as (s)he scrolls down. What are some approaches I can take to implementing this? Thanks.
I have an animation for my splash screen and on some phones which take longer to load things, it starts mid animation.
The splash screen is really an activity that has a View, in which the animation is drawn. Is it possible to wait for the screen to load and then start the thread in the View? I mean can I somehow check if I can begin the animation and it will be shown, the whole thing
Do yourself a favor and DON'T use a splash screen. On Android guidelines Google is extremely against splash screens because they give some sort of slowish UI perception to the user, and it's not a good thing at all.
If you absolutely need to wait for some sort of data to finish loading/network query you can do it much more elegantly by letting the app load normally and give the user a loading indicator (e.g. SwipeToRefreshLayout from the v7 support lib).
Not actually answering your question but this is a much more elegant solution for a completely wrong approach for a good UX. The Animation.AnimationListener has a callback called onAnimationEnd where you can do whatever after the animation has finished.
I have a listview which contains ImageView i. Now I have an array list which contains URLS inside it like www.abc.jpg, www.def.jpg and so on. Now I want to insert these URLs as the source for the image view. Is it possible.?
ImageView doesn't have any API that allows setting of urls that will be downloaded later on. You need to separately from the ImageView setup an asynchronous task that will download this image from the url, convert it to a Bitmap and then set it on ImageView with setImageBitmap(Bitmap).
As noted, you're doing network I/O in the main thread. The stack trace identifies it as happening in onPostExecute. In there, I see this code:
BitmapFactory.decodeStream(newurl.openConnection() .getInputStream()
That certainly looks like network I/O to me.
What I'm wondering is, you've got this lovely worker thread already, but you're doing a lot of work in onPostExecute() (main thread). To prevent janky UI, I suggest you move as much of that computation into doInBackgroud() as possible.