Is there a way to animate on a Home Widget? - android

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)

Related

Overriding Android Chronometer class

I want to override the android chronometer class. I want it to inflate a custom XML layout and to also have a quicker update interval.
I've found one that modifies the update interval from the default 1s to 0.1s on github.
But I want to do more and have imageViews or Buttons display the time instead of the default textView it uses. I haven't really done this before except for a recycle view, so some explanation would be nice.
My end goal is to have an efficient stopwatch with custom digits and display, like the one found on HTC one M7 in the default clock app or any Samsung phone. I also need it to run in its own Fragment and be able to handle orientation changes and the activities onPause() or onStop() without losing any time.
Would it be better to use Intent Service and a Results Reciever? if so how would i accomplish this?
Chronometer is good for certain extent, but instead of customizing it you can create your own custom layout and display the time based on your requirements using a handler which would be the best solution for your problem.Find the link below for example
http://www.shawnbe.com/index.php/tutorial/tutorial-3-a-simple-stopwatch-lets-add-the-code/

How to implement a snapchat like circular animated countdown timer component in React Native?

I was wondering if anyone knows how to implement a circular countdown timer in React Native shown on the attached image. Would be ideal if it was for both ios and android. Thanks!
I can at least point you in the direction of implementing one yourself.
There does indeed exist a 'react-countdown-timer' component, however it might be worth implementing yourself or altering their code in order to make the timer appear like above. To do so, I would use a combination of timer-mixins in addition to a circular view with text inside. Next, I would keep track of the state of the timer using the timer-mixins in addition to the setState function. For each interval, I would update the time remaining of the timer and then update the button component that you have created to reflect whatever time is left.
As per your requirement I would like to suggest you
Use https://www.npmjs.com/package/react-native-av-countdown-circle this component

scrollable background in viewpager

I'm trying to create a highscore viewpager in my game.
In the highscore viewpager I would like to have a horizontaly scrollable background.
I found the parralex background, but I could not get it to work.
In another thread here on stackoverflow I found a link to a custom viewpager class.
This also uses parralex background, but I dont want to replace the entire viewpager class.
This is the link
How can I get a scrollable background in a viewpager ?
Thanks in advance !
implement ViewPager.OnPageChangeListener in your Activity and in onPageScrolled change your background to match the current position
I've implemented something similar to what you want:
AsyncBackgroundViewPager
This is a widget that allows you to asynchronously load an image via a network call, cache it to a device. The same image will then be decoded when you need to display it again. Unfortunately, the widget has a dependency on a image caching library that I have written (which does have bugs in it). The widget does work though and it does pretty much provide a way to load a large background over a ViewPager.
You can hack it also to any requirements that you need. I will be uploading a version with out dependencies once I've finished it, which will be soon, so keep an eye out if you want.
Depending on how much of the background you want to show, you can simply change the on draw method to draw some specified width (currently it covers all of the ViewPager).
The project has a working MainActivity and you should be able to run right off the bat after downloading.

android, use camera as a background for an activity

I have a request that background for my activity must be "transparent", ie. background of an activity should be camera's current view. What is the easiest way to implemet this?
I have all the activities already implemented, with all the logic already implemented. Ideally, solution wouldn't include a lot of modification of an existing code.
Normally this is done with SurfaceView. Use it via camera.setPreviewDisplay(surfaceView)
To use it in your app, the simplest way would be to use FrameLayout as a top layout of your Activity and then put SurfaceView any you current top layout inside it.
`FrameLayout`
`SurfaceView`
`yourExistingLayout`
Of course your existing layout must be transparent to see the underlying SurfaceView. Note also that this alpha blending takes a lot of processor power so it will eat battery.

Android homescreen widget animations

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

Categories

Resources