I've got a BubbleTextView which is basically a TextView with some additions used in Launcher3 to display icons on the homescreen. I'm trying to animate this view via this XML
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/anticipate_interpolator"
android:duration="#android:integer/config_shortAnimTime"
android:startOffset="350"
android:fillAfter="true" >
<alpha
android:fromAlpha="1.0"
android:toAlpha="0.0" />
<translate
android:fromYDelta="0"
android:toYDelta="200" />
</set>
But whenever I start the animation it'll only translate the image and the alpha part won't work, so there is no fade out animation. What am I doing wrong here?
I figured out that BubbleTextView overrides onSetAlpha
#Override
protected boolean onSetAlpha(int alpha) {
if (mPrevAlpha != alpha) {
mPrevAlpha = alpha;
super.onSetAlpha(alpha);
}
return true;
}
Seems to be some relict of older revisions.
Since the implementation is not needed anymore it's safe to either delete the method or return false instead.
Related
I have been working on adding animation to the card game I have been programming lately, and recently added in these xml files:
slide_up_left.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/accelerate_interpolator" >
<translate
android:duration="#integer/config_slide_time"
android:fromYDelta="100%p"
android:toYDelta="0" />
<alpha
android:duration="#integer/config_slide_time"
android:fromAlpha="0.0"
android:toAlpha="1.0" />
<rotate
android:duration="#integer/config_slide_time"
android:fromDegrees="25"
android:pivotX="0"
android:pivotY="0"
android:toDegrees="0" />
</set>
slide_up_right.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/accelerate_interpolator" >
<translate
android:duration="#integer/config_slide_time"
android:fromYDelta="100%p"
android:toYDelta="0" />
<alpha
android:duration="#integer/config_slide_time"
android:fromAlpha="0.0"
android:toAlpha="1.0" />
<rotate
android:duration="#integer/config_slide_time"
android:fromDegrees="-25"
android:pivotX="100%"
android:pivotY="0"
android:toDegrees="0" />
</set>
integers.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer name="config_slide_time">800</integer>
</resources>
Shortly after, I came to the realize that my cards are drawables, so the only way I can get motion is by moving the entire canvas, and that's going to create too many problems. Is there any way I can move the cards individually without the canvas, or am I going to have to reset all the cards as images instead of drawables?
Since the android.view.animation package is meant for animating Views, you will have to manage the animations yourself, although you can still use your animations that you have defined in XML.
I'm assuming you are doing the drawing in some custom View.
First, define a Transformation variable and an Animation variable in your View class:
private Animation mAnimation;
private Transformation mTransformation = new Transformation();
When you want to start the Animation, inflate one:
mAnimation = AnimationUtils.loadAnimation(getContext(), R.anim.slide_up_left);
Then, set the start time and initialize the size:
mAnimation.setStartTime(getDrawingTime());
Rect drawableBounds = mDrawable.getBounds();
mAnimation.initialize(drawableBounds.width(), drawableBounds.height(), getWidth(), getHeight());
Now, in onDraw(), pass in the current time and the transformation to update the transformation, and then apply it to the canvas before drawing:
public void draw(Canvas canvas) {
mAnimation.getTransformation(getDrawingTime(), mTransformation);
s.save();
s.concat(mTransformation.getMatrix());
mDrawable.setAlpha((int) (mTransformation.getAlpha() * 0xFF));
mDrawable.draw(canvas);
s.restore();
}
That's a basic approach to doing it yourself. You'll have to manage the state of the animation, clean it up when the animation is over, create and position the Drawable(s), and handle nulls and other View state, but this should give you a starting point.
I would also highly recommend looking into ObjectAnimator, which is the new and better way to do animations.
I have a problem with applying an animation
Given the following animation code
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:fromXDelta="0"
android:interpolator="#anim/cycle_10"
android:toXDelta="10" />
after starting the application this animation can be applied only once
public void onClick{
button.setAnimation(shake);
}
how to apply this animation more than once?
View.startAnimation(Animation) ?
add following lines in animation xml-
android:repeatMode="reverse"
android:repeatCount="infinite"
I am trying to start an animation AFTER 1 second. I have used the attribute "android:startOffset" in my XML file, but it does not work completely the way I expected. I was expecting the view to NOT EVEN BE DRAW in its initial position (that is, the position set in the attributes "fromXDelta" and "fromYDelta") before the offset I set has passed. Here is my XML:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:ordering="sequentially"
android:shareInterpolator="false" >
<translate
android:duration="2000"
android:startOffset="1000"
android:fromXDelta="-70%p"
android:fromYDelta="0%p"
android:interpolator="#android:anim/linear_interpolator"
android:toXDelta="+0%p"
android:toYDelta="0%p" />
</set>
If I try to move my view using the above animation, the view is drawn IMMEDIATELY at the position -70% of the screen. Then the one second passes and then, as expected, the animation kicks in and starts to move the view. However, I DO NOT want the view to be drawn at all before that 1 second!. How can I achieve this?
Thank you in advance.
UPDATE
I am calling the above XML just after a startActivity call (the *R.anim.animation_coming_in* below), like this:
startActivity(new Intent(this, ThankYouActivity.class));
overridePendingTransition(R.anim.animation_coming_in, R.anim.animation_coming_out);
You could try using a pair of alpha animations with very short duration so that the view is hidden until it's needed. Something like this:
<set ...>
<alpha
android:fromAlpha="0.0"
android:toAlpha="0.0"
android:duration="1"
android:startOffset="0" />
<alpha
android:fromAlpha="0.0"
android:toAlpha="1.0"
android:duration="1"
android:startOffset="1000" />
<translate
...
/>
</set>
Alternatively, you could implement this set of animations in code. Doing so would enable you to use a Handler to start the animation after a delay so that the view is hidden until the animation starts.
I am trying to make a TextView scale and fade out. My TextView is inside a layout file that is included into my activity's layout with
<include android:id="#+id/hud" layout="#layout/hud" android:layout_alignParentBottom="true"/>
Now, I can apply a scale animation from within the Java code like this:
TextView multiplier = (TextView)findViewById(R.id.hudMultiplier);
ScaleAnimation s = new ScaleAnimation(1.0f, 3.0f, 1.0f,3.0f);
s.setDuration(5000);
multiplier.startAnimation(s);
And it works nicely, but I want that animation (and a bunch of others) from an xml file. So I made this file:
<?xml version="1.0" encoding="utf-8"?>
<set
xmlns:android="http://shemas.android.com/apk/res/android"
android:shareInterpolator="false">
<scale
android:pivotX="50%"
android:pivotY="100%"
android:fromXScale="1.0"
android:fromYScale="1.0"
android:toXScale="10.0"
android:toYScale="10.0"
android:duration="5000"
android:interpolator="#android:anim/accelerate_decelerate_interpolator"
/>
<alpha
android:fromAlpha="1.0"
android:toAlpha="0.1"
android:duration="2000"
android:startOffset="2000">
</alpha>
</set>
I am trying to apply the animation with this code:
TextView multiplier = (TextView)findViewById(R.id.hudMultiplier);
AnimationSet an = (AnimationSet) AnimationUtils.loadAnimation(getApplicationContext(), R.anim.multiplier);
multiplier.startAnimation(an);
What happens now is that the TextView blinks for a fraction of a second and nothing actually happens.
I've tried:
removing the alpha animation - no change
removing the start offset - no change
change the animation with one from the android documentation - no change
change AnimationSet to Animation - no change
change getApplicationContext() to this, MyActivity.this - no change
change getApplicationContext() to null - kills the application
What am I missing?
The project is targeted at Android 1.6 and I'm testing in a 2.3 emulator.
This seems to make a difference, for me anyway!
Change shemas to schemas in the Set element.
<?xml version="1.0" encoding="utf-8"?>
<set
xmlns:android="http://shemas.android.com/apk/res/android"
android:shareInterpolator="false">
try this:
an = AnimationUtils.loadAnimation(this, android.R.anim.fade_out);
I want to have a View that initially is invisible and when I press a button, it becomes visible with a fade in animation. I'm using the AlphaAnimation for the fading effect. The problem is that if I make the view invisible the animation can't be seen.
Suppose you have an ImageView named imageView and an animation file your_fade_in_anim.xml inside your res\anim\ folder:
ImageView imageView = (ImageView) findViewById(R.id.imageView);
Animation fadeInAnimation = AnimationUtils.loadAnimation(this, R.anim.your_fade_in_anim);
// Now Set your animation
imageView.startAnimation(fadeInAnimation);
Your XML
<?xml version="1.0" encoding="UTF-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:fromAlpha="0.0"
android:toAlpha="1.0"
android:interpolator="#android:anim/accelerate_interpolator"
android:duration="[duration (in milliseconds)]"
android:repeatCount="infinite" />
</set>
Replace the brackets with your actual duration.
Provide an AnimationListener to the Animation and make the View visible as soon as the Animation starts.
http://developer.android.com/reference/android/view/animation/Animation.AnimationListener.html
Instead of the infinite repeat count and hiding/viewing your View, I suggest to just not repeat the animation and initially start with the alpha channel set to maximum. Then you can use:
<?xml version="1.0" encoding="UTF-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:fromAlpha="0.0"
android:toAlpha="1.0"
android:interpolator="#android:anim/accelerate_interpolator"
android:duration="[duration (in milliseconds)]"
android:repeatCount="0" />
</set>
And you're done. No need for a Listener, hiding or showing. Just as simple.