Android homescreen widget animations - android

I'm looking into creating a widget that supports animation, ideally via the android.view.animation framework, otherwise by setting properties on the remote views in code triggered from a background service.
Does anyone have any experience with either of these approaches, and is what I'm trying doable, or am I heading up a blind alley?

It's actually possible to animate RemoteView widgets. The problem is it is super restrictive which is by design because of the security implications of running custom code in a system process.
What I mean by this is that Android will only work with animations that are expressed in res/anim xml files that are tied to layouts via xml. Some RemoteView widgets support this
An example of this is the News and Weather app widget that comes on a stock android system. What it is doing is using a ViewFlipper to cycle through each news story every 10 seconds or so.
<ViewFlipper android:layout_width="match_parent" android:layout_height="wrap_content" android:measureAllChildren="true" android:flipInterval="10000" android:autoStart="true"
android:inAnimation="#android:anim/fade_in" android:outAnimation="#android:anim/fade_out" android:animateFirstView="true">
<TextView android:id="#+id/Description1TextView" style="#style/AWCText.Centered" android:layout_width="match_parent" android:layout_height="wrap_content"/>
<TextView android:id="#+id/Description2TextView" style="#style/AWCText.Centered" android:layout_width="match_parent" android:layout_height="wrap_content"/>
<TextView android:id="#+id/Description3TextView" style="#style/AWCText.Centered" android:layout_width="match_parent" android:layout_height="wrap_content"/>
<TextView android:id="#+id/Description4TextView" style="#style/AWCText.Centered" android:layout_width="match_parent" android:layout_height="wrap_content"/>
</ViewFlipper>
In this example you can tie pending intents to each TextView. So when a user clicks on any one a different action can occur.
Lastly, Android has been slowly adding support for animated views in each version. For example TransitionDrawables (cross fading selector drawable) don't cross-fade until Android 3.0.

It's possible, but use it with caution, since it is very heavy to the default homescreen implementation and you shouldn't use it very often.
In the Mario Coin Block widget, I am using such technique to do the animation, you can checkout the source code: http://code.google.com/p/mario-coin-block/source/browse/trunk/MarioWidget.CoinBlock/src/com/gueei/mario/coinBlock/view/CoinBlockView.java
Basically the idea is manually draw on an offscreen Bitmap, and replace the BitmapView's bitmap with it using RemoveViews Call.

I agree with the other answers here, so I won't re-iterate - limited animation on a widget is possible, but heavy on resources, it might make the home screen slow and less responsive, and battery drainer. From my experience - it doesn't run smooth.
So the bottom line is - it's ok if it's only few frames changing from time to time, or for some effects seldom upon an event (for example user press or some event from your service.
But here's an idea that probably does not directly answer your question, but may be a suitable alternative (I don't know your use case, it might be not relevant at all)
Did you consider implementing a live wallpaper?
pros -
highest quality animation,
can be controlled from the background
cons -
not interactive,
replaces the user's wallpaper... and it's hard to satisfy everyone's taste

You can have only simple animations like fadeIn or fadeOut on your widget, it's very simple, you don't need any layout animations, just use ViewFlipper(it took me 3 days of investigation to find out that it is so easy).
Yet it's imposiible to write something really great without using custom launchers

Animations are impossible on RemoteViews, and RemoteViews updates can occur at a rate of once every 30 minutes (or manually)...
Anyway, you can try the following link: is-there-a-way-to-animate-on-a-home-widget

Related

Material Design - Expanding (Transition) a Card to full screen

I'm struggling to implement the expand feature of the card view described by the Material Design for Android.
In their design guidelines they show off different layouts for the Card component, but one example shows a card transition to fullscreen onClick.
This is the transition shown on their website:
I've tried out implementing a feature like this, but it would require much more work than what their guideline examples are suggesting... How does Material Design accomplish this? Is there a built-in feature for this, should I just manually translate and fit the card to fit the screen, or should I use an entirely new fragment or activity for the full-card-view?
Here are the Design guidelines, which contain that example, but nothing is said about the transition, neither on the documented Develop page, which is minimal really.
TL;DR
In the case of the gif image you've attached above, the RecyclerView and the detailed CardView should have their own separate Fragments which are operated in one single Activity.
Jump to the links at the end for the animation part.
Detail
Why so? Well, we had three choices:
Keep both Views in one Activity and overlap the detailed CardView on top of RecyclerView on click event. (This one is stupid, and not a good practice)
Create separate Activities for both Views (Recycler & fullscreen-Card)
The one I mentioned above.
RecyclerView and Detailed View shown as two separate Fragments
Now the reason for not choosing the 2nd option was because it is more performance intensive as compared to the 3rd one. We may not notice this in a small scale app but it certainly makes an impact when the app scales. Plus, creating fragments is more effective as we are sharing common views and variables in between the Views. So the best choice is number three. Note that this isn't a universal case and the usage will differ according to your requirement.
Using Fragments can be overwhelming at first but it keeps the code more organised when you get a hang of it. You should try to keep your app divided into few broadly divided activities and within those should be as many fragments as you want.
Here's a few links that helped me implement the exact same thing you're looking for.
MDC: Material Motion
Implementing Motion with MM
Building Transitions with MM
Hands-on experience in Codelab
All three of them helped me gain a better understanding on how the whole Material Motion framework works and how to implement it in my program.

Android Difference between Scenes and Transitions, Animations, and when to use them

So I recently have been reading into transitions and animations on the developer site:
Animation
http://developer.android.com/training/animation/index.html
Scenes and transitions
http://developer.android.com/training/transitions/index.html
I do not see the big differences between these and think they are relatively the same.
I know transitions are more of switching between views and animations are more for adding a wow factor by for example making a button pop up when holding your finger on it, However I believe there is much more to it then just these.
I am looking for a detailed answer if possible on the differences between the two and when you should be using each?
I came across this article the other day trying to find the best practices for doing animations while using data binding. The author explained 2 approaches, in which he uses animation (with BindingAdapter, see approach No.1) and transition (with onRebindCallback, see approach No.2) respectively. I think the summary/comparison he wrote at the end answers your question at a high-level, too. I personally think the most important points are that animations provide more fine-grained control while transitions are reusable (even if your view slightly changes).
Advantages of the BindingAdapter mechanism:
Fine-grained control — only the views you want to animate will animate
Less overhead than transitions (performance)
Very flexible — you can create whatever animation you want
Advantages of the OnRebindCallback mechanism:
Simple to use
Don’t have to use custom attributes (or override default behavior)
Can animate many things with the same code (see Transition subclasses)

Alternative to overridePendingTransition() - Android

I just discovered the android overrivePendingTransition() method. It works fine but I have the following problem :
In the Settings/Dislpay menu, you can choose to show no animations, some animations or all animations, and the method only works when it is set to all animations.
Can I bypass that ?
The settings you're talking about are user preferences. If one of your users wanted to turn off all animations, why would you want to find a workaround to continue showing animations in your app? It doesn't seem very user-friendly.
At any rate, overridePendingTransition is used to animate between Activities, as opposed to Views which is part of why you can turn them off, and was introduced in Android 2.0. There isn't another SDK method available that does the same thing; however, you can try using a LayoutAnimation to animate the layout you provide for each Activity you're creating. It wouldn't be exactly the same as overridePendingTransition, but I think it's going to be one of the closest things you'll find to an alternative.
your_animation.xml:
<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
android:animation="#anim/THE_ANIMATION_YOU_WANT_TO_USE" />
your_layout.xml:
android:layoutAnimation="#anim/your_animation"

Create a wave animation like the one on the Android 4.0 lock screen

How can one create a wave like animation, like the one which appears when one touches the screen on an Android 4.0 lock screen.
I have something similar in one of my app. The way I did is putting the whole wave as one big image then you repeatedly change it's source in background to give it animated look & feel.
Basically, when onTouchUp you start the animation and have it loop and update the image sources until finish. Then you switch its source to the original image in onAnimationFinish.
That's just from the top of my head so I'm not sure if it's accurate or not. I reckon you get the idea anyway. If there's easier way to pull it off then I'm willing to listen as well.
here is the solution you want to implement let me know if it doesn't work.
Code sample :-
Add a TitanicTextView to your layout:
<com.romainpiel.titanic.TitanicTextView
android:id="#+id/titanic_tv"
android:text="#string/loading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#212121"
android:textSize="70sp"/>
To start the animation:
titanic = new Titanic();
titanic.start(myTitanicTextView);
You may want to keep track of the titanic instance after the animation is started if you want to stop it.
To stop it:
titanic.cancel();

Is there a way to animate on a Home Widget?

I want to use an animation on a Home page Widget, i.e. an AppWidgetProvider. I was hoping to use the "Frame Animation" technique:
http://developer.android.com/guide/topics/graphics/2d-graphics.html#frame-animation
which I've used successfully in an activity. But I can't translate that code to an AppWidgetProvider.
Basically, in an AppWidgetProvider, I create and work with a RemoteViews object, which AFAIK doesn't provide me with a method to get a reference to an ImageView in the layout for me to call start() on the animation. There is also not a handler or a callback for when the widget displays so I can make the start() call.
Is there another way this can be done? I suppose that I can probably do the animation on my own with very fast onUpdate() calls on the widget, but that seems awfully expensive.
Do not animate app widgets, unless you write you own home screen app.
You are correct that you have no way to manipulate an AnimationDrawable or an Animation to have them work with an app widget.
You are also correct that "very fast onUpdate() calls on the widget...seems awfully expensive", because it is. Updates to app widgets involve inter-process communication, between your AppWidgetProvider and the process hosting the home screen. This system is designed for updates every 30 minutes or so, not 30 frames per second.
I am currently creating an widget that "need" sprite animation, and I have put a blog post on how to animate home widget. Yes, it is expensive to do, so I do it only when the widget is needed. BTW, original android animation is not supported in remote views.
Edit:
Demo Project is up.
One Widget that is available for use in a RemoteView is the ProgressBar. It will animate itself and will not chew up resources. An in-determinant ProgressBar which is a square will overlay quite well on a homescreen appwidget. See sample code from Android site
Another option to animate a widget is the use of ViewFlipper, where one can use inAnimationand outAnimation:
<ViewFlipper xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:flipInterval="5000"
android:autoStart="true"
android:inAnimation="#android:anim/fade_in"
android:outAnimation="#android:anim/fade_out"
android:animateFirstView="true"/>
Create custom animation.
Create ProgressBar and set in android:indeterminateDrawable your animation.
Add ProgressBar to your widget layout and make it visible(invisible)

Categories

Resources