ImageView Visibility Issue in Relative Layout - android

I've seen all sorts of code that makes me think that mine should work, but for some reason it does not. I've got an ImageView that animates vertically down another image and I want the mobile imageview to disappear once the animation is complete but it does not. The 'scanbar' imageview is the one in question. it is set as invisible in the XML and is made visible on a button press. I need it to go away when the animation is finished.
public class scan extends Activity {
EditText Quote;
private static final Random rgenerator = new Random();
ImageView scanbar;
public void scanLine() {
// Displays the scanline animation over the wireframe image
ImageView wireframe;
scanbar = (ImageView)findViewById(R.id.scanbar);
wireframe = (ImageView) findViewById(R.id.wireframe);
scanbar.setVisibility(View.VISIBLE);
// Super ultra-secret code
}
Animation.AnimationListener scanListener = new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
scanbar.setVisibility(View.INVISIBLE);
setResults();
}
};
The setResults(); call works properly, so I know that section of code is being executed. Anyone know what I'm doing wrong?

Will give this old question an answer just in case someone stumbles upon it like me.
Check out stickupkid's answer here: Android, setVisbility to gone not working in RelativeLayout. Call clearAnimation() on what ever view is doing the animation before calling View.INVISIBLE

I can confirm that: Calling clearAnimation() on the view that is doing the animation before calling View.INVISIBLE, or GONE does the trick.

Related

Animatin constantly runs

I want run one animation and then animation one finished, second animation is started. I write this code but when i run this program, animation constantly runs! and not finished. Why? What is the problem??
public class Splash extends Activity{
Animation animation1;
Animation animation2;
Animation animation3;
ImageView image;
ImageButton circleProduct;
ImageButton circleIntroduce;
ImageButton circleMore;
ImageButton circleContact;
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
animation1=AnimationUtils.loadAnimation(this, R.anim.bounce);
circleProduct=(ImageButton)findViewById(R.id.btnCircleProduct);
circleIntroduce=(ImageButton)findViewById(R.id.btnCircleIntroduce);
circleMore=(ImageButton)findViewById(R.id.btnCircleMore);
circleContact=(ImageButton)findViewById(R.id.btnCircleContact);
image=(ImageView)findViewById(R.id.img);
image.startAnimation(animation1);
animation1.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
circleProduct.setVisibility(View.VISIBLE);
circleProduct.startAnimation(animation1);
circleContact.setVisibility(View.VISIBLE);
circleContact.startAnimation(animation1);
circleIntroduce.setVisibility(View.VISIBLE);
circleIntroduce.startAnimation(animation1);
circleMore.setVisibility(View.VISIBLE);
circleMore.startAnimation(animation1);
}
});
}
Thanks for help
You're running the same animation again on onAnimationEnd method.-
circleMore.startAnimation(animation1);
Therefore, every time animation1 finished, it will start again, unless you start a different animation with a different animationListener.
Because you are starting the same animation, animation1, in your listener, so you have created a loop.
Create another animation instance for the animations you have to launch in the onAnimationEnd method.
Your problem is that when a1 ends you start a1 again and not a2.
I would suggest you that you form an AnimationSet out of your thee animations, where you set the start offset of a2 to the duration of a1 and the start offset of a3 to the duration of a1 + the duration of a2.
Then you apply the whole animation set to the view and it will play one animation after another and end after the third.

How to jump and rotate an image in android?

I have an image in image view of my activity. There is button also in my activity. i want that when i press button image should be jump and rotate and will replace by another image. Actually i want to implement coin Toss application ?how can i achieve this . any help will be appreciated.
With ObjectAnimator and setting KeyFrame it can be achived, which was introduced in API 11.
You can use AnimationSet with animation listeners like:
AnimationSet aset= new AnimationSet();
aset.add(jumAnimation);
aset.add(flipAnimation1);
aset.add(flipAnimation2);
with flipAnimation1 rotate the view from 0 degrees to 90 degrees and change the image
then start the second animation and do the rest of the rotation.
flipAnimation.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animation animation) {
//change the image
//start flipAnimation2
}
});
});
imageView.startAnimation(aset);
Dont forget to set animation attributes like: duration,fillAfter.

Right way to animate children inside custom ViewGroup

I am building a custom ViewGroup that arranges a bunch of children in particular positions.
On a particular trigger (lets say an external button press), the children animate to new positions. The way I tried doing it was by starting a Timer and on each timer update, computing new positions and then calling requestLayout().
When I do this the animation is a little jittery. Is there a more efficient way of doing this?
Any ideas on what the right steps are to do a custom rearragement of child views?
You should implement an AnimationListener like this:
Animation anim = AnimationUtils.loadAnimation(R.android.anim.slide_in_left);
anim.setAnimationListener(listener);
view.startAnimation(anim);
private AnimationListener imageViewAnimationListener = new AnimationListener() //
{
#Override
public void onAnimationEnd(Animation arg0) //
{
}
#Override
public void onAnimationRepeat(Animation arg0) //
{
}
#Override
public void onAnimationStart(Animation arg0) //
{
}
};
Hope this helps!

android animation is not finished in onAnimationEnd

It seems that an android animation is not truly finished when the onAnimationEnd event is fired although animation.hasEnded is set to true.
I want my view to change it's background drawable on the end of it's ScaleAnimation which it does, but you can clearly see that it is changed some miliseconds before it finishes. The problem is, that it flickers because the new background appears (=is) scaled for a short time until the animation really finishes.
Is there a way to get either the real end of the animation or just prevent the new background from beeing scaled this short period of time?
Thank you!
//EDIT: I'm using an AnimationListener to get the following call:
#Override
public void onAnimationEnd(Animation animation)
{
View view = (MyView) ((ExtendedScaleAnimation) animation).getView();
view.clearAnimation();
view.requestLayout();
view.refreshBackground(); // <-- this is where the background gets changed
}
Here is the actual bug related to this issue http://code.google.com/p/android-misc-widgets/issues/detail?id=8
This basically states that the onAnimationEnd method doesn't really work well when an AnimationListener is attached to an Animation
The workaround is to listen for the animation events in the view to which you were applying the animation to
For example if initially you were attaching the animation listener to the animation like this
mAnimation.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationEnd(Animation arg0) {
//Functionality here
}
});
and then applying to the animation to a ImageView like this
mImageView.startAnimation(mAnimation);
To work around this issue, you must now create a custom ImageView
public class MyImageView extends ImageView {
and then override the onAnimationEnd method of the View class and provide all the functionality there
#Override
protected void onAnimationEnd() {
super.onAnimationEnd();
//Functionality here
}
This is the proper workaround for this issue, provide the functionality in the over-riden View -> onAnimationEnd method as opposed to the onAnimationEnd method of the AnimationListener attached to the Animation.
This works properly and there is no longer any flicker towards the end of the animation.
I was abe to resolve this by calling clearAnimation() on the view being animated inside onAnimationEnd, that took away the flicker
Its weird why would anyone have to do that, as onAnimationEnd callback should have been called only if the animation has already ended. But I guess the answer lies in the depth of Framework on how view/layout handles animation callback.
For now take it as a hack-free solution, that just works.
animation.setAnimationListener(new AnimationListener() {
public void onAnimationEnd(Animation anim) {
innerView.clearAnimation(); // to get rid of flicker at end of animation
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams
(innerBlockContainer.getWidth(), innerBlockContainer.getHeight());
/* Update lp margin, left/top to update layout after end of Translation */
ViewGroup parent_ofInnerView = (ViewGroup)innerView.getParent();
vp.updateViewLayout(innerBlockContainer, lp);
}
public void onAnimationRepeat(Animation arg0) {}
public void onAnimationStart(Animation arg0) {
}
});
innerView.startAnimation(animation);
I had same issue and solved it using
view.clearAnimation();
before
view.startAnimation(anim);
I had a similar problem and I used Soham's solution with custom view class.
It worked fine, but at the end, I've found a simpler solution that worked for me.
After calling the view.StartAnimation(animation), and before the next step in my program, I've added a short delay that will be long enough to let the animation finish, but short enough to be unnoticeable by the user:
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
nextStepInMyProgram();
}
}, 200);// delay in milliseconds (200)
For some reason the onAnimationStart works properly, and the onAnimationEnd doesnt. So heres how I originally did it and what I changed:
Attempt 1 (flicker):
a) Move image from 0px to 80px
b) In onAnimationEnd, set the image's location to 80px
Attempt 2 (no flicker):
a) In onAnimationStart, set the image's location to 80px
b) Move the image from -80px to 0px
Hope that made sense. Basically I flipped the way I did it
Try to use getAnimation() from your object:
public void onShowListBtnClick(View view)
{
rightPanel.startAnimation(AnimationUtils.loadAnimation(MainActivity.this, R.anim.slide_left));
rightPanel.getAnimation().setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animation animation) {
// write your code here
}
});
}
An easy fix is to add one line to AnimationListener.onAnimationEnd():
#Override
public void onAnimationEnd(Animation a) {
a.setAnimationListener(null);
…
}
annimation can be also stopped on screen rotation. in this case onAnimationEnd() is not being called. my workaround:
animation.setDuration(animationDuration);
animation.setAnimationListener(new AnimationListenerAdapter() {...});
view.startAnimation(animation);
handler.postDelayed(new Runnable() {
#Override
public void run() {
if(!animation.hasEnded()) {
// here you can handle this case
}
}
}, animationDuration + 100);
I had this issue because my Animation was not started in the main thread.
This resulted in a duration of 0.
However , the Animation did play correctly - it just called onAnimationEnd() immediately after execution.
If you are using repeat count as infinite, then onAnimationEnd would not get called. Check the documentation link
You can also use setUpdateListener, then check the current fraction of the animation progress and act accordingly.
Here's a Kotlin example for a fade-out animation which eventually makes the view gone from the layout:
view.animate()
.alpha(0f)
.setDuration(duration)
.setUpdateListener { animation ->
if (animation.animatedFraction > 0.99f) {
view.visibility = View.GONE
}
}
.start()
This worked for me:
#Override
public void onAnimationUpdate(ValueAnimator animation) {
if (Float.compare(animation.getAnimatedFraction(),1.0f)) {
// animation ended;
//do stuff post animation
}
}

TranslateAnimated ImageView not clickable after animation [Android]

I have 2 ImageViews that I translate from the top of the screen to the bottom. these views are infalted from xml and the animation is added from java code. The animation works perfectly, but the onClickListener I added in java code doesn't seem to work. I used fillAfter attribute of the animation to make the iamges stay at their arrival after the translation, but THESE images aren't clickable anymore... However, their position before translation remains clickable...
I can't see the logic of this. Could anyone give me some piece of advice about that?
This is because Animations affects only the drawing of widget. However, The real Location is not affected -It is still in the previous one-.
To overcome this problem, you need to update the layout parameters of the ImageView manually by installing an animation listener as follows:
Animation.setAnimationListener(new AnimationListener() {
public void onAnimationStart(Animation arg0) {
}
public void onAnimationRepeat(Animation arg0) {
//TODO Auto-generated method stub
}
public void onAnimationEnd(Animation arg0) {
android.widget.LinearLayout.LayoutParams params = new LayoutParams(
android.widget.LinearLayout.LayoutParams.FILL_PARENT,
android.widget.LinearLayout.LayoutParams.WRAP_CONTENT);
params.topMargin = addLocationButton.getTop()-100;
ImageView.setLayoutParams(params);
}
});

Categories

Resources