Animate layout background image changes - android

Is it possible to animate the background image of a layout (RelativeLayout, LinearLayout, etc.) in Android? In one of my applications I am fetching an image from the internet and displaying it as the background image once it's loaded. It would be really cool if the background image was swapped out using a fade effect or something like that.
I've tried quite a bit, but couldn't seem to find a solution. It's always the layout that's animating and not the image itself.

Might be your best bet to just layer your layout in a FrameLayout or RelativeLayout with a ViewSwitcher behind it. See the demo (and related layouts) in the Android API Demos app (that uses an ImageSwitcher, which is a special-case adapter-based subclass of ViewSwitcher). It does mean an extra ViewGroup level in your app, but unless you're doing something especially complicated (nesting these in ListView rows or something) it should perform just fine, and handles the transition animations for you.
You could also just use WebImageView from DroidFu, which does most of the annoying legwork for you.

Related

How to smooth animate between CardViews of different sizes?

Background
I have only CardViews in a ViewAnimator (or anything else that would allow me to switch between CardViews), and I want to switch between them nicely, using scaling animation, so that it will look like a single CardView that changes its size.
The problem
Each card has a different size and content, and some are generated dynamically , so it's problematic to set it all via XML.
The question
Is there a built in way to do this?
Maybe not even use a ViewAnimator?
Or use a transition instead?
I just want to animate the cardViews. The content-switching will probably have a fade-in&out effects or no effects at all.
Is it maybe possible by using a single CardView and animate it?

Setting a background resource for many linearlayouts in scrollview causes lag

I am dynamically adding around 150 linearlayouts to a scrollview in a grid-like layout. If I set the background resource to a drawable for each of them using setBackgroundResource(R.drawable.x), the scrollview shows extremely noticeable lag and choppiness, even though the drawable is a simple colour and border.
If I remove the call to setBackgroundResource, the scrollview is smooth again.
Is this expected to happen with so many views containing backgrounds? If so, how would I go about making a grid with custom backgrounds for each cell?
You're going to want to use a list view in your scroll, and you're going to want to use a ListAdapater:
http://developer.android.com/guide/topics/ui/layout/listview.html
http://developer.android.com/reference/android/widget/Adapter.html
Basically what's going on is you're loading a large number of images into memory, and the scroll view by default doesn't do a very good job of managing releasing and inflating these resources.
Using methods similar to the above, with some custom image management, I've managed to get thousands of views running smoothly on a scroll.
It seems like you're trying to create your own list view implementation so that you can set your own layouts for each row. I don't recommend doing this. Instead, use the default list view implementation provided by Android and instead of setting a default ArrayAdapter instance on the list view, subclass ArrayAdapter, override the getView method, and return your custom layout.
I highly recommend you check out this tutorial for a more thorough explanation:
http://www.ezzylearning.com/tutorial.aspx?tid=1763429

View Flipping in Android using same activity

I am new to android therefore I got a little bit lost with all those ViewFlipper, ViewSwitcher, ViewAnimator and ViewPager. Moreover, I am not sure what's happening under the hood.
Basically I have an activity which shows some data. With swipe (or button, doesnt matter) I would like to scroll the view and get to another page (as seen in the picture below).
Is it possible to implement something like that without changing to another activity?
I am a little bit confused regarding views and access to the design elements. How those pages are located each to another? e.g. If I am currently seeing Page1, can I modify content of Page3? Or plainly saying, are all page views loaded all together? As if I set setContentView(R.layout.xlayout); then I can access only xlayout elements.
But if I use same activity, then I have a thread there which updates a counter on Page1, if I change view to Page 2, the counter will not find Page1 Counter TextView and will complain.
As I Understand Android 4.0 has ViewPager which is similar to the seen in the picture. I am using GB. Should I use support library or can I just go around and implement something similar without importing any libraries?
(Sorry, my description is a little bit messy)
Yes, you can use ViewSwitcher, ViewFlipper and ImageSwitcher depending on your requirements.
ViewSwitcher may have two childs at max. And these child might be a View or an object of subclass of view.
ViewFlipper: May have as many childs you want. and these child might be a View or an object of subclass of view.
ImageSwitcher might be used to switch images over.
By using view flipper you can display one item at a time, and adding a gesture overlay, you can apply sliding effect. To apply View Flipper, you need to add all the views to ViewFlipper, and showNext and showPrevious methods are used to show next and previous child.
You need to use a ViewPager to have the same behaviour as in Google Play.
it only available on recent version of Android, but you can use the Compatibility Package to make it available for older version.
With a ViewFlipper you can't "scroll" between two pages.

Using a FrameLayout as a Live Wallpaper

I'm trying to use a FrameLayout as the content of a Live Wallpaper. According to this answer by Romain Guy, it should be possible, but I didn't have much luck calling .measure() and .layout() on the view. Could anyone give me a brief example of how to use a regular layout inside a Live Wallpaper?
It's a wallpaper that you can mess around with, right? If you are going above the usual views, you may have to implement some custom logic, especially if you are drawing images. A FrameLayout has background and foreground drawable functions. You can use XML or Java to add regular views into a FrameLayout
You also can create a custom Drawable object to do a lot of fun things with regards to custom painting. The Drawable object simply gets sent the view's canvas.
If you want to measure views, you are going to have to do it after the view runs through its measure and layout codes. This means doing this in onCreate() returns 0. Lots of people have had better luck doing it in a callback like onWindowFocusedChanged

What is the purpose of FrameLayout and in which scenario should we use it?

What purpose does FrameLayout serve in Android? Is there any specific scenario for which it is designed for?
In my Android application, I have a scenario where I have to show two ImageViews, one over the other. This is a .png image file with a 9-patch drawable over this image.
Which ViewGroup should I use for this purpose: RelativeLayout or FrameLayout?
I also want to know the different scenarios that each of the ViewGroups should be used for.
I don't recall how i got to this question but here is my answer that could help anyone:
As you can see here
FrameLayout is designed to block out an area on the screen to display a single item. Generally, FrameLayout should be used to hold a single child view, because it can be difficult to organize child views in a way that's scalable to different screen sizes without the children overlapping each other.
In general is not useful to have 2 views one over the other because you're going to have overdraw effect (an area of the screen that is getting drawn by the gpu more than once which is really useless).
If you really have to, then you need to use the onDraw method (you can see an example here) and Canvas API (have a look here) and the clipRect method (you can see an example here)
Regarding FrameLayout or RelativeLayout, if you want to keep things simple or your activity has already enough nested layouts, better use FrameLayout. In 2017, there is the constraint layout which could be of some help as well.
Yes, you can use a FrameLayout in your scenario.

Categories

Resources