Android, setVisibility/animation issue - java

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

Related

Alpha animation works, then does not work

My app is about users picking three buttons: Sad, Happy and Confuse. If they click the Sad button, the image changes into a sad emoji. If they click the Happy button, happy emoji is displayed. Simple.
But, when I run my app, and I click any of the buttons, the alpha animation works. If I then click another button, the alpha animation does not work anymore. The alpha animation only works once. The alpha animation is only for the images.
Here's my Java code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Happy_Btn = findViewById(R.id.Happy_Btn);
Sad_Btn = findViewById(R.id.Sad_Btn);
Confuse_Btn = findViewById(R.id.Confuse_Btn);
Feeling_txtView = findViewById(R.id.Feeling_txtView);
Face_Img = findViewById(R.id.Face_Img);
Animation animation = new AlphaAnimation(0f, 1.0f);
animation.setDuration(2000);
Feeling_txtView.setText("You are feeling happy!");
Happy_Btn.setEnabled(false);
Sad_Btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Sad_Btn.setEnabled(false);
Happy_Btn.setEnabled(true);
Confuse_Btn.setEnabled(true);
Feeling_txtView.setText("You are feeling sad...");
Face_Img.setAnimation(animation);
Face_Img.setImageResource(R.drawable.sad);
}
});
Happy_Btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Sad_Btn.setEnabled(true);
Happy_Btn.setEnabled(false);
Confuse_Btn.setEnabled(true);
Feeling_txtView.setText("You are feeling happy!");
Face_Img.setAnimation(animation);
Face_Img.setImageResource(R.drawable.happyface);
}
});
Confuse_Btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Sad_Btn.setEnabled(true);
Happy_Btn.setEnabled(true);
Confuse_Btn.setEnabled(false);
Feeling_txtView.setText("You are feeling confused?");
Face_Img.setAnimation(animation);
Face_Img.setImageResource(R.drawable.confuse);
}
});
}
(EDIT)
I made few changes to my code and then when I run it, my issue is solved! But I'm not sure if this is the right way to do it. What I did was I first cut these two lines:
Animation animation = new AlphaAnimation(0f, 1.0f);
animation.setDuration(2000);
and then paste them into each buttons (Sad, Happy and Confuse)
My app works the way I wanted it to be.
you should renew AlphaAnimation
you do it is the right

How can I interact with animations?

I have an animation that slides across the screen, while the animation is playing I want it to disapear if it has been clicked!
I have tried to use OnClickListener() and also OnClickMethods, both only work after the animation has finished playing!
TranslateAnimation animation = new TranslateAnimation(answer, answer, 0, height * -1); // xfrom , xto, y from,y to
animation.setDuration(5000);
Floater0.startAnimation(animation);
animation.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
if (AdPlayer0.isPlaying()) {
answer = rn.nextInt(width) + 1; //useless
TranslateAnimation animation0 = new TranslateAnimation(answer, answer, 0, height * -1); // xfrom , xto, y from,y to
animation0.setDuration(5000);
animation0.setFillEnabled(true);
Floater1.startAnimation(animation0);
Floater1.setVisibility(View.VISIBLE);
Floater0.startAnimation(animation);
}
}
});
I was reading the answer here and something interesting was mentioned:
Animations do NOT effect the 'physical' location of the views on screen. If you want it to actually move then you need to adjust the properties of the view.
I have now tested the above quote, and it seems to be true! I have also looked into how to adjust the position properties of the view after the animation has ended, however I want to change its position before an animation has ended(While its playing)!
Any suggestions for a work around? ideally I would want to change the physical position of the view while the animation is played!
A TranslationAnimation only moves the pixels on the screen, so an OnClickListener does not animate with it. Use ViewPropertyAnimator instead and set an OnClickListener then it should work.
The code would look something like this :
Floater1.animate().setDuration(5000).translationX(answer).translationY(height * -1);
Check out all the available methods in the docs.
Edit/Update :
.translationX(float toX)
means -> animate the view to this point on the x axis.
The animation always starts at the current position of the view, so you don't need a fromX. There are also other methods which you can use :
.translationXBy(float byX)
.translationYBy(float byY)
With these you can translate the view BY the float value, and not TO a certain point on the screen.

Animate button move and set new position in Android

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

ProgressBar duration and hasEnded methods

I'm working with a progress bar and an animation.
progressBar = (ProgressBar)findViewById(R.id.progressBar1);
Animation animation = new RotateAnimation(30, 180);
progressBar.setAnimation(animation);
further down in my method I
set the duration for this animation
progressBar.getAnimation().setDuration(3000);
so for 3 seconds, the animation will run.
if I want to hide the ProgressBar after the animation ends I have to do the following
if(progressBar.getAnimation().getDuration() == 3000){
progressBar.setVisibility(ProgressBar.GONE);
}
What I tried doing was
if(progressBar.getAnimation.hasEnded()){
progressBar.setVisibility(ProgressBar.GONE);
}
this does not work
Could somebody explain why the hasEnded method doesn't seem to return true where I think it should.
you can call set an Animation Listener on the animation object. The Listener calls tree methods, one of which is onAnimationEnd().
animation.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation anim) {
};
#Override
public void onAnimationRepeat(Animation anim) {
};
#Override
public void onAnimationEnd(Animation anim) {
// here your logic
};
});
I think problem is in below line...
if(progressBar.getAnimation.hasEnded()) {
it should be...
if(progressBar.getAnimation().hasEnded()) {
Edit::
That's because you're starting the animation and immediately checking if the animation has ended which is doubtful unless it simply doesn't do anything. What you want to do is implement an AnimationListener callback and set it to that animation.

TranslateAnimated ImageView Flicker

I have one big problem. I use this code to animate ImageView to horizontal move one ImageView from current X position to 0.
Here is the animation code
translate = new TranslateAnimation(0, translateX, 0, 0);
translate.setDuration(400);
translate.setAnimationListener(new AnimationListener() {
public void onAnimationEnd(Animation animation) {
txtTitle.setText(String.format("Translate: %d %d", translateX, lpView1.leftMargin));
lpView1.leftMargin = 0;
mainSwitchBtn.setLayoutParams(lpView1);
}
public void onAnimationRepeat(Animation animation) {
}
public void onAnimationStart(Animation animation) {
}
});
translate.reset();
translate.setFillAfter(false);
mainSwitchBtn.clearAnimation();
mainSwitchBtn.startAnimation(translate);
When animation is ended, I have moved ImageView to 0. At that I see flicker of ImageView. I do not know what is a problem. Can some one help me?
EDIT: Founded Problem
I have founded what is a problem in my code.
I have replaced this code
mainSwitchBtn.setLayoutParams(lpView1);
With this
mainSwitchBtn.layout(0, 0, 0, 0);
I do not know what is a difference between setting margins in layout params and set params to view object or use .layout function but now I have not flickering.
It seems like animation ends and your imageview returning to original non-animated image, maybe you should try to use HorizontalScrollView instead of animation

Categories