Frames Skipping while working with Android Animation - android

I have two fragments on the same screen. One is changing image after every 3 seconds and the other is showing a text with a horizontal animation (from left to right). The image fragment is setting the image from external memory using the method setImageURI.
The problem occurs when a new image is loaded in the view, animated text on the other fragment skips its frames and a kind of glitch appears on that fragment interface.
I have tried changing the image on a Runnable thread as well but that didnt work as well.
Can anyone suggest something better?
And also....can the problem be resolved using a device with a better RAM?

Related

How to reduce the lag of jumping between pages(fragment/activity)

I have a FragmentA(with Recyclerview) and a FragmentB(item). When I click in A to jump to B, the page lags a bit, maybe I updated the content in the main thread? But I don't seem to be doing that.
In FragmentB, I have some text and 3 Recyclerview, maybe the recyclerview cause the lag?
May be I need to load the data after the transition animation is complete? How to finish it?
It can be because of many things. Check if you are using a large image size that can use more power and time to render. Try to move your long operations into coroutines.
As your layouts has one image, 3 recycler view & other stuff there is many cases for layout lagging.
May the image you are loading is larger. (Try: load it with Glide or other image loading libraries)
2.May your 3 recycler listing has large number of items & you are loading them immediately after creating your fragment view. (Try: put them into the runnanble & load them after some delay of a time)

Displaying a stack of images

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.

Out of memory exception on inflating layouts

I am getting random out of memory exceptions in my app caused by inflate exceptions.
I have 7 fragment (difficulty) activities which launch their own activities via buttons. Each fragment has a scroll view with 30 buttons (levels).
I have set it up so that i can swipe across to each fragment and the fragment takes up the entire screen.
Occasionally when i swipe a few times and then select a random button from a group of 30 it will crash. It tries to load the activity and gives an out of memory exception with an inflate exception on a random line. The line always falls on an imageView or imageButton in the xml file. The activities that load are a grid of imageViews and imageButtons.
I do not get the exception much but it is something i want to fix. I have looked at many other out of memory exception questions although none have helped me. I have done a Memory analyser test and it shows nothing out of the ordinary.
I believe that the imageViews and imageButtons are using too much memory, although i only ever have one activity open at once.
It IS because of your images that are loading. When you load an image and you move around the page and view another image the heap increases. As you continue the process of viewing random images the heap grows even more until your app crashes. It's like stacking books on a glass table. You either move(cache) a book(image) or the glass(app) breaks. You should use an imageloader to load your images.
https://github.com/nostra13/Android-Universal-Image-Loader
You've mentioned that it always falls on an ImageView and ImageButton - and this is the clue to solve this problem. You get OOM 'cause background resource of this view has high resolution and takes a lot of memory. Try to lower resolution of this image.
Also you've mentioned that you have a ScrollView and this means that you keep in memory every 30 items. Probably you'd better change it to RecyclerView backed by adapter.
Just had the same problem and I'd like to simplify all the things said here:
Simply: reduce your images sizes.
Don't use 1080X1920 images... It's too high res.
Such image, even if compressed, when deployed will catch about 1080X1920X4B = 8.2MB (The GPU has to deploy it to it's full original resolution... That's why compression won't help but reducing the needed memory size...) and this i RAM that we're talking about..
Take Gimp or Photoshop and down scale the image to, say, 1/4: 540X960, and you won't feel the difference.. Belive me, been there already.
Beware of the memory consumption of images and videos.
Hope this helps,
James

Thread assistance

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.

Managing Memory for showing large bitmaps in android

I know this question is asked several times, but still clarity to my situation would be helpful.
I am showing some images in 2 column grid view. When user taps on an image, I am displaying the image in ViewPager. The Image which is displayed in gridview is about 200X200px and I want to show the same image with enlarged size say about 800X800px in ViewPager in a DialogFragment.
The Actual size of images are huge with different resolutions. I have followed the link http://developer.android.com/training/displaying-bitmaps/index.html and I have scaled down images to size which is required for my app. In ViewPager fragment dialog I am also recycling the image with bitmap.recycle() and calling System.gc() explicitly(I know this is a bad practice) at PagerFragment onDestroy. But even then I am getting Out of memory error. On top of it, I am encountering this issue only in Android 4.2.1 (Nexus 7) but not on Android 4.1.2( Samsung Tab) and Android 4.4(Nexus 7).
For later 2 android versions, I do not have to call even System.gc(). It works very well without this. I have checked Viewpager with some 300 - 400 images. But for former after scrolling 60 images, App is crashing with OutOfmemory error. To resolve this I have used a workaround for now android:largeHeap="true" which I think is very bad for myapp and could not digest it.
I really appreciate if anyone who can help me avoid android:largeHeap="true".
In my app, I have to show around 6000 images in GridView and also an enlarged image in ViewPager linked to GridView. I am loading only 20 images from FileSystem asynchronously while scrolling gridview.
Thanks in advance...
Use an LRUCache to hold the images. Make that the only thing that holds a Bitmap that's not currently on screen. That way the older bitmaps will be kicked out and garbage collected quickly.

Categories

Resources