In my android app I have a view which is laid on top of another view. When I click a button this top view is animated and goes out of screen. The problem is that it is like the view is still "there" as it captures all touch events and they do not get passed to the view which is behind.
This is the code that animates the view away:
TranslateAnimation animation = new TranslateAnimation (0, 0, 0, height);
animation.setDuration(1000);
animation.setFillAfter(true);
topView.startAnimation(animation);
What can I do to solve this problem?
Create an animation Listener and remove the view from its parent when the animation is over.
Animation animation = new AlphaAnimation(0 ,0);
animation.setAnimationListener(new AnimationListener() {
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
public void onAnimationEnd(Animation animation) {
parentView.removeView(topView)
}
});
Related
Hi I am creating a game with three ImageViews that gradually move down and then when they reach a certain point the ImageViews return to the top of the screen and so the ImageViews move in a continuous cycle. I have tried using a translate animation to move the ImageViews around. However all the ImageViews remain stationary which means that I haven't set the the translate animation up correctly.
Here is the code that I have used (speedtwo is a double, and timetaken is a long that I set up)
double speedtwominus = speedtwo * -1;
final TranslateAnimation anim = new TranslateAnimation(0, 0, 0, (float) speedtwo);
anim.setDuration(timetaken);
final TranslateAnimation finish = new TranslateAnimation(0, 0, 0, (float) speedtwominus);
anim.setDuration(0);
anim.setAnimationListener(new TranslateAnimation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
numone.startAnimation(anim);
}
#Override
public void onAnimationEnd(Animation arg0) {
// TODO Auto-generated method stub
numone.startAnimation(finish);
}
#Override
public void onAnimationRepeat(Animation arg0) {
// TODO Auto-generated method stub
}
});
I had used TranslateAnimation and slide up and down a view.
However, I realize, even though after I slide down the view, and use View.GONE in its visibility, the view still able to receive touch event.
You can produce the same problem, by clicking on the button to make the orange color view disappear from the bottom of the screen. Then, when you click on the bottom of the screen, you will realize touch event of the custom view is still being triggered.
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int color = getResources().getColor(android.R.color.holo_orange_light);
// construct the RelativeLayout
final RelativeLayout customView = new RelativeLayout(this) {
#Override
public boolean onTouchEvent(MotionEvent event) {
this.setPressed(true);
Log.i("CHEOK", "OH NO! TOUCH!!!!");
return super.onTouchEvent(event);
}
};
customView.setBackgroundColor(color);
final FrameLayout frameLayout = (FrameLayout)this.findViewById(R.id.frameLayout);
frameLayout.addView(customView, new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, 100, Gravity.BOTTOM));
customView.setVisibility(View.GONE);
Button button = (Button)this.findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (customView.getVisibility() != View.VISIBLE) {
// Slide up!
TranslateAnimation anim=new TranslateAnimation(0,0,100,0);
anim.setFillAfter(true);
anim.setDuration(200);
Log.i("CHEOK", "VISIBLE!!!");
customView.setVisibility(View.VISIBLE);
customView.setAnimation(anim);
customView.setEnabled(true);
} else {
// Slide down!
TranslateAnimation anim=new TranslateAnimation(0,0,0,100);
anim.setFillAfter(true);
anim.setDuration(200);
// HELPME : Not sure why, after I hide the view by sliding it down,
// making it View.GONE and setEnabled(false), it still able to
// receive touch event.
Log.i("CHEOK", "GONE!!!");
customView.setVisibility(View.GONE);
customView.setAnimation(anim);
customView.setEnabled(false);
}
}
});
}
}
The complete source code to demonstrate this problem can be found here :
https://www.dropbox.com/s/1101dm885fn5hzq/animator_bug.zip
How can I make my custom view not to receive touch event, after I had slide it down?
It is not clear to me why you are using setAnimation() instead of startAnimation() as it does not appear that you have set a start time on your animations.
On another note, I have found that setting a view to GONE while it has an associated animation does not make it truly "GONE." Instead, you must first get rid of the animation using clearAnimation().
So, something like this instead:
public void onClick(View v) {
if (customView.getVisibility() != View.VISIBLE) {
// Slide up!
TranslateAnimation anim=new TranslateAnimation(0,0,100,0);
anim.setFillAfter(true);
anim.setDuration(200);
customView.setVisibility(View.VISIBLE);
customView.setEnabled(true);
customView.startAnimation(anim);
} else {
// Slide down!
TranslateAnimation anim=new TranslateAnimation(0,0,0,100);
anim.setFillAfter(true);
anim.setDuration(200);
anim.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationEnd(Animation animation) {
customView.clearAnimation();
customView.setVisibility(View.GONE);
customView.setEnabled(false);
}
#Override
public void onAnimationRepeat(Animation animation) {
// nothing to do
}
#Override
public void onAnimationStart(Animation animation) {
// nothing to do
}
}
}
}
I don't recall, but you may have to post() the contents of onAnimationEnd() instead of running the code immediately for it to take effect properly.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to use fade in and fade out effect with an image using Transition, android
http://www.dynamicdrive.com/dynamicindex14/fadeinslideshow.htm
I have seen this link. It shows images by using Fade in and Fade out effects. How could I do this programmatically?
create two Animation
Animation fadeOutAnimation = new AlphaAnimation(1.0f, 0.0f);
Animation fadeInAnimation = new AlphaAnimation(0.0f, 1.0f);
set duration
fadeOutAnimation.setDuration(1000);
fadeInAnimation.setDuration(1000);
set listener - when the fadeout animation finish replace the image
fadeOutAnimation.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) {
//here switch the images !
//and the begin the second animation FadeIn
}
});
ImageView.startAnimation(fadeOutAnimation);
I have 3 horizontal draggable views. For example, if I drop first on the second - the second and the third must move to the right.
The initial position:
The code:
case DragEvent.ACTION_DROP:
targetIndex = ((ViewInfo) targetView.getTag()).index;
int lastIndex = getChildCount() - 1;
droppedView = (View) event.getLocalState();
droppedIndex = ((ViewInfo) droppedView.getTag()).index;
if (droppedView == this) {
return false;
}
for (int i = targetIndex; i <= lastIndex; i++) {
if (getChildAt(i) != droppedView) {
moveChildToRight(i);
}
}
private void moveChildToRight(int index) {
....
TranslateAnimation anim = new TranslateAnimation(0, 200, 0, 0);
anim.setDuration(1000);
anim.setFillAfter(true);
anim.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationEnd(Animation animation) {
Log.d(TAG, "Animation ended");
requestLayout();
}
#Override
public void onAnimationRepeat(Animation arg0) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationStart(Animation arg0) {
// TODO Auto-generated method stub
}
});
child.startAnimation(anim);
}
OK, after drop the 2nd and the 3rd views move to the right:
After that I long-press to the empty space between 1st and 2nd+3rd views, and what do I see? Drag of the 2nd view is initiated! But it's in fact shown more righter, than it can be touched. The "visual" representation of the 2nd or 3rd views are not touchable after animation:
Why?
I realize this is over a year old, but I'll answer in case someone else finds the page.
This is how translate animation (view animation) works. It moves the object visually, but doesn't update the actually X/Y property. For example, if your views were buttons, then touching in the original area would show the click on the where the button visually is, but clicking on where you see the button would do nothing, as the actual button never moved.
I have animating the view from one place to another, after animation done I have written the code like this,
public void onAnimationEnd(Animation animation) {
imageView.setOnClickListener(this);
}
But I cant able to click that view. No response from that view.
You should move the view after the animation to the new position. You can use LayoutParams.
You should move the view after the animation to the new position. You can use LayoutParams. use the code like this..
transAnimation= new TranslateAnimation(0,
sourceX-destinationX,0,sourceY-destinationY); // right to left
transAnimation.setDuration(1000);
//transAnimation.setFillEnabled(true);
transAnimation.setFillAfter(false);
view.startAnimation(transAnimation);
transAnimation.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animation animation) {
Log.v("hari", "--------onAnimationEnd--------");
RelativeLayout.LayoutParams relativeNew
= new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
relativeNew.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
relativeNew.addRule(RelativeLayout.ALIGN_LEFT);
relativeNew.setMargins(125, 10, 10, 10);
view.setLayoutParams(relativeNew);
}