In my android application I have five imageviews when I click any one of that all images are animating. I set the zoomout and Zoomin animation for all the images. once the animation is finished, the selected image view is invisible. After image invisible, when i click on that imageview location it again start the animation and the image is invisible.
Zoom-In Animation:
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXScale="1"
android:toXScale="5"
android:fromYScale="1"
android:toYScale="5"
android:pivotX="50%"
android:pivotY="50%"
android:duration="1000"
android:fillAfter="true">
</scale>
Zoom-out Animation
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXScale="5"
android:toXScale="1"
android:fromYScale="5"
android:toYScale="1"
android:pivotX="50%"
android:pivotY="50%"
android:duration="1000"
android:fillAfter="true">
</scale>
zoomin =AnimationUtils.loadAnimation(this, R.anim.zoom);
zoomout=AnimationUtils.loadAnimation(this, R.anim.zoomout);
ImageView v2 = (ImageView) findViewById(R.id.image2);
v2.setOnClickListener(new View.OnClickListener()
{
#Override public void onClick(View v)
{
v2.setAnimation(zoomin);
v2.startAnimation(zoomin);
v2.setAnimation(zoomout);
v2.startAnimation(zoomout);
v2.clearAnimation();
}
});
Add this code to your animation objects:
animation.setAnimationListener(new AnimationListener()
{
#Override
public void onAnimationStart(Animation animation) { }
#Override
public void onAnimationRepeat(Animation animation) { }
#Override
public void onAnimationEnd(Animation animation)
{
v2.setImageResource(R.drawable.some_transparent_image);
}
});
This issue is occuring because you added
android:fillAfter="true"
in both Animation XML files.
Either remove "android:fillAfter="true" from both XMLs or keep " android:fillAfter="false" in both files.
You could simply change the transparency of the layer.
v2.setAlpha(0f);
you have to handle the animation listener for that animation.
zoomin.setAnimationListener(new AnimationListener()
{
#Override
public void onAnimationStart(Animation animation) { }
#Override
public void onAnimationRepeat(Animation animation) { }
#Override
public void onAnimationEnd(Animation animation)
{
v2.setVisibility(View.GONE);
}
});
You need to use a Transparent Image
All you need to do is when your Animation ends you need to set a transparent Image as background/scr of that image thereby replacing the previous one.
Changing the visibility of view won't solve your problem
v2.setVisibility(View.GONE);
v2.setVisibility(View.INVISIBLE);
As both above lead to make your view non-clickable and you won't be able to click again.
You must first thing use image.clearAnimation() then hide it
image.visibility = View.GONE
Related
I've an ImageButton that I want to move when pressed and when animation finish I want that this button stops in the new position.
This is button code:
<ImageButton
android:id="#+id/move_button"
android:layout_width="120dp"
android:layout_height="35dp"
android:layout_centerInParent="true"
android:layout_alignParentTop="true"
android:layout_marginTop="0dp"
android:scaleType="fitCenter"
android:background="#drawable/background_button"
android:src="#drawable/move_button"
android:onClick="MoveButton" />
I've found a code to do that in this site:
public void MoveButton(final View view) {
TranslateAnimation anim = new TranslateAnimation(0, 0, 100, 0);
anim.setDuration(300);
anim.setAnimationListener(new TranslateAnimation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) { }
#Override
public void onAnimationRepeat(Animation animation) { }
#Override
public void onAnimationEnd(Animation animation)
{
FrameLayout.LayoutParams params = (FrameLayout.LayoutParams)view.getLayoutParams();
params.topMargin += -100;
view.setLayoutParams(params);
}
});
view.startAnimation(anim);
}
When button it's pressed it start the animation, but when animation is complete button return to initial position and application crashes.
What can be the problem?
This is work Definitely.
Button im= (Button) findViewById(R.id.button);
//set position TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta
final Animation animation = new TranslateAnimation(0,100,0,0);
// set Animation for 5 sec
animation.setDuration(5000);
//for button stops in the new position.
animation.setFillAfter(true);
im.startAnimation(animation);
Use anim.setFillAfter(true) to situated the View at the position where Animation ends.
One thing more you are animating your ImageButton from 100 to 0 in Y coordinates, thats why your ImageButton comes to intial position because 0 is its intial position.
Try below code in this code I used anim.setFillAfter(true) and animate the ImageButton from 0 to 100 in Y coordinates.
public void MoveButton(final View view) {
TranslateAnimation anim = new TranslateAnimation(0,0,0,100);
anim.setDuration(300);
anim.setFillAfter(true);
anim.setAnimationListener(new TranslateAnimation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
}
});
view.startAnimation(anim);
}
Let me know if this is helpful for you.
Use ObjectAnimator
ObjectAnimator animation = ObjectAnimator.ofFloat(YOUR_VIEW, "translationX", 100f);
animation.setDuration(2000);
animation.start();
This code will move the View 100 pixles to the right, over a period of 2 seconds.
If you need more information go to Developers Guide
Import the necessary libraries:
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
Get the button you need to use:
Button button = findViewById(R.id.button_id);
Now in order to animate button you will need to use the TranslateAnimation(fromXDelta, toXDelta, fromYDelta, toYDelta) function which takes 4 float arguments.
Animation Example:
Animation your_animation = new TranslateAnimation(0,100,0,0);
The first argument specifies the start of the x position and the second one the end of the x position, Same thing for the other two but the y position instead.
You will also need to set your animation's duration in milliseconds using
your_animation.setDuration(1000);
Your button will get back to its previous position when the animation ends, in order to make the button stay to its new position, use this:
your_animation.setFillAfter(true)
For more information on animations https://developer.android.com/training/animation
In my android application I am using an ObjectAnimator to translate an imageview like so:
ObjectAnimator transitionX = ObjectAnimator.ofFloat(v, "TranslationY", 190);
Is there an option like fillAfter that will reset the position of the view to the new position, after the animation is complete?
Nope, I'd use the property animation system instead, assuming v is a view:
v.animate().translationY(190).withEndAction(new Runnable() {
#Override
public void run() {
v.setTranslationY(v.getTranslationY()-190);
}
}).start();
Nope, there is no option for that. However you can achieve the same effect using a listener where you can manually change it to the initial position in the onAnimationEnd(). Here is an example for scaling an imageview:
scaleDown.addListener(new Animator.AnimatorListener() {
#Override
public void onAnimationStart(Animator animation) {
}
#Override
public void onAnimationEnd(Animator animation) {
//Initial property values
tempIV.setScaleX(1f);
tempIV.setScaleY(1f);
tempIV.setAlpha(1f);
}
#Override
public void onAnimationCancel(Animator animation) {
}
#Override
public void onAnimationRepeat(Animator animation) {
}
});
I think the right way to handle this is to use an AnimationSet to merge two animations, one to translate and one to translate back the view.
Something like this
ObjectAnimator translateTo190= ObjectAnimator.ofFloat(v, "TranslationY", 190);
ObjectAnimator translateBack = ObjectAnimator.ofFloat(v, "TranslationY", 0);
AnimatorSet translate= new AnimatorSet();
translate.play(translateTo190).before(translateBack);
translate.start();
I have had a problem like this in writing a card dealing animation where the cards are dealt quickly from a central "deck". After much trial-and-error, the way I've got this working is using the ObjectAnimator in the following way.
I add a single "deck" card at the source of the animation, and then a second card that I'm actually animating. I use the following deal_from_drawpile.xml to animate the card in two parts: the first part returns the card to the source, and the second part scales and rotates the card as it is "dealt":
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/accelerate_interpolator"
android:ordering="sequentially">
<set android:interpolator="#android:anim/accelerate_interpolator"
android:ordering="together">
<objectAnimator
android:propertyName="translationX"
android:duration="10"
android:valueFrom="0.0f"
android:valueTo="0.0f"/>
<objectAnimator
android:propertyName="translationY"
android:duration="10"
android:valueFrom="0.0f"
android:valueTo="0.0f"/>
<objectAnimator
android:propertyName="scaleX"
android:duration="10"
android:valueFrom="0.75f"
android:valueTo="0.75f"/>
<objectAnimator
android:propertyName="scaleY"
android:duration="10"
android:valueFrom="0.75f"
android:valueTo="0.75f"/>
</set>
<set android:ordering="together">
<objectAnimator
android:propertyName="rotation"
android:duration="300"
android:valueFrom="0.0f"
android:valueTo="360.0f"/>
<objectAnimator
android:propertyName="scaleX"
android:duration="300"
android:valueFrom="0.75f"
android:valueTo="0.5f"/>
<objectAnimator
android:propertyName="scaleY"
android:duration="300"
android:valueFrom="0.75f"
android:valueTo="0.5f"/>
</set>
Then in code I add the actual translation which depends on how many hands are arranged in a rough semi-circle. The complete sequence of animations is then added in an AnimatorSet and fired off at the end. Code-snippet;
fullScreenContent.addView(dealtCardView);
for (int iCard=0; iCard<numCards; iCard++) {
for (int iPlayer = 0; iPlayer < numPlayers; iPlayer++) {
dealtCardAnimator = dealtCardAnimator.clone();
dealtCardAnimator.setTarget(dealtCardView);
//offsets where the cards are dealt according to player
ObjectAnimator playerOffsetXAnimator = ObjectAnimator.ofFloat(dealtCardView, "TranslationX", mGame.getPlayer(iPlayer).getPlayerLayout().getTranslationX());
ObjectAnimator playerOffsetYAnimator = ObjectAnimator.ofFloat(dealtCardView, "TranslationY", mGame.getPlayer(iPlayer).getPlayerLayout().getTranslationY());
if (lastDealtCardAnimator == null) dealSet.play(dealtCardAnimator).with(playerOffsetXAnimator).with(playerOffsetYAnimator);
else dealSet.play(dealtCardAnimator).with(playerOffsetXAnimator).with(playerOffsetYAnimator).after(lastDealtCardAnimator);
//The card is returned to the home point with the first portion of deal_from_drawpile
lastDealtCardAnimator = dealtCardAnimator;
}//end for iPlayer
}//end for numCards
dealSet.start();
I can now tinker with the duration of each step in the XML to get the effect I want. Note that you have to embed the duration in each element , not in the set section (where it is ignored, and some arbitrary default seems to be used instead).
I'm trying to apply fade-in\out transition in Android.
Can you please tell me why I see the fading only for a moment and then it returns to original alpha?
I have added setFillAfter(true); to persist the image disappearance.
I have tried:
animation = android.view.animation.AnimationUtils
.loadAnimation(AppService.getAppContext(),
com.waze.R.anim.fade_out);
animation.setInterpolator(new AccelerateInterpolator());
animation.setFillAfter(true);
boxImage_left.startAnimation(animation);
with this:
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true">
<alpha android:fromAlpha="0.0"
android:toAlpha="1.0"
android:repeatCount="0"
android:duration="500"/>
</set>
and also:
AlphaAnimation alpha = new AlphaAnimation(0,1);
alpha.setDuration(600);
animation.setInterpolator(new AccelerateInterpolator());
animation.setFillAfter(true);
vImage_left.startAnimation(alpha);
Let your activity implements AnimationListener, and set visiblity after animation end.
public class YourActivity extends Activity implements AnimationListener
{
#override
onCreate(Bundle x)
{
...
// set animation listener
animFadein.setAnimationListener(this);
animFadeOut.setAnimationListener(this);
}
#Override
public void onAnimationEnd(Animation animation)
{
if (animation == animFadein)
{
view.setVisibility(View.VISIBLE);
}
if (animation == animFadeOut)
{
view.setVisibility(View.INVISIBLE);
}
}
}
And for animation speed modify animation duration and interpolator
Instead of AccelerateInterpolator you can use LinearInterpolator
I know it's too late but will help someone else.
I have prepared a two graphics for background of my button. I have one put in "android:background" in xml file and I have no idea how to make to change this image for another image for a while after click.
I tried to do this using OnHoverListener and OnClickListener but it doesn't work
Button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Button.setBackground(getResources().getDrawable(
R.drawable.img_hovered));
Button.setBackground(getResources().getDrawable(R.drawable.img));
}
});
if you want to make it clickable button, then make a selector xml in drawable folder and copy below code.
lets take its name as app_click.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="#drawable/yourimage_pressed" android:state_pressed="true"/>
<item android:drawable="#drawable/yourimage_unpressed"/>
</selector>
then use this selector for your button Selector.
like this
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/app_click" />
See if the following does what you are looking for. The code assumes that R.drawable.img is the original background. When Button is clicked, the background changes to R.drawable.img_hovered for 0.5 seconds. Then it reverts back to R.drawable.img:
Button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Button.setBackground(getResources().getDrawable(
R.drawable.img_hovered));
new Handler().postDelayed(new Runnable() {
public void run() {
Button.setBackground(getResources().getDrawable(R.drawable.img));
// Button Click Code Here
}
}, 500L); // 0.5 Seconds
}
});
I have a linearLayout that disappears when I hit a button, it comes back when I press the button again. But it does it so fast, it doesn't look nice.
I do this via:
disappearView.setVisibility(View.GONE);
I would like to add some animation... If I just set visibity to invisible the space where the layout was is still there. So I tried this:
if (disappearView.getVisibility() == View.VISIBLE){
Animation out = AnimationUtils.makeOutAnimation(this, true);
disappearView.startAnimation(out);
disappearView.setVisibility(View.INVISIBLE);
disappearView.setVisibility(View.GONE);
}
else {
Animation in = AnimationUtils.loadAnimation(this, android.R.anim.fade_in);
disappearView.startAnimation(in);
disappearView.setVisibility(View.VISIBLE);
}
This does the animation too fast and disappears. You can't see it at all. Do I need to use a thread to start gone after invisible is set...or a delay? Or is there a better way of doing all this?
I'm not sure exactly what you're trying to accomplish...do you want the LinearLayout to fade out over a little bit of time rather than instantly disappear? And then once it fades out be removed from the parent via View.GONE?
If so, you can use an AlphaAnimation for the fade out and then attach a listener like EvZ posted:
AlphaAnimation fadeOutAnimation = new AlphaAnimation(1, 0); // start alpha, end alpha
fadeOutAnimation.setDuration(1000); // time for animation in milliseconds
fadeOutAnimation.setFillAfter(true); // make the transformation persist
fadeOutAnimation.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationEnd(Animation animation) {
linearLayout.setVisibility(View.GONE);
}
#Override
public void onAnimationRepeat(Animation animation) { }
#Override
public void onAnimationStart(Animation animation) { }
});
linearLayout.setAnimation(fadeOutAnimation);
You can try to use onAnimationEnd : Animation.AnimationListener