Whenever I use TranslateAnimation to move an object to a new location, for some reason, the touch target of that object remains in the old position.
How do I change this behaviour?
eg.
public static void hideViewUp (View v, int duration) {
AnimationSet animSet = new AnimationSet(true);
animSet.setFillAfter(true);
animSet.setDuration(duration);
AlphaAnimation alp = new AlphaAnimation(1.0f, 0);
TranslateAnimation translate = new TranslateAnimation(Animation.RELATIVE_TO_SELF,
Animation.RELATIVE_TO_SELF,
Animation.RELATIVE_TO_SELF,
-(v.getTop() + v.getHeight()));
animSet.addAnimation(translate);
animSet.addAnimation(alp);
v.startAnimation(animSet);
}
There's no need to post any sample code - the issue is indeed pretty well known and general.
TranslateAnimation and all other old types of animation change only the draw transformation matrix. These animations shouldn't be used to move items permamently.
To truly move a view to a new position, use ObjectAnimator.
Here's an example: http://www.android-app-market.com/animations-in-android-translate-animation-alpha-animation-object-animation.html
Related
I am trying to have a Listview fade out/in under certain circumstances. I'm running into issues when running the animation, the Listview just becomes invisible and no animation is performed.
I've tried running the fade_out animation:
Animation out = AnimationUtils.loadAnimation(mainActivity, R.anim.fade_out);
out.setInterpolator(new LinearInterpolator());
myListView.startAnimation(out);
As well as an AlphaAnimation
AlphaAnimation animation1 = new AlphaAnimation(1f, .5f);
animation1.setDuration(2000);
animation1.setFillAfter(false);
myListView.startAnimation(animation1)
and finally simply setting the alpha
myListView.setAlpha(.5f)
The best I can tell is that alpha transparencies are not supported with a list view and only values of 0 and 1 are available as alpha. I can't find any documentation confirming or denying either way.
Is there some kind of known issue with ListViews? Am I doing something wrong? Or is this some kind of limited functionality on ListViews?
Just try it's.If you want to do visible :
public void viseb_show(){
AnimationSet set = new AnimationSet(true);
Animation animation = new AlphaAnimation(0.0f, 1.0f);
animation.setDuration(500);
set.addAnimation(animation);
anim.startAnimation(animation);
anim.setVisibility(View.VISIBLE);
}
or if non visible
public void viseb_gone(){
AnimationSet set = new AnimationSet(true);
Animation animation = new AlphaAnimation(1.0f, 0.0f);
animation.setDuration(3500);
set.addAnimation(animation);
anim.startAnimation(animation);
anim.setVisibility(View.GONE);
}
anim it's you object who mast be visible.
I figured out my issue. emigrantdd sent me in almost the right direction, how ever there were a few problems I needed to overcome.
First I had to alter emigrantdd's code to:
public void viseb_gone()
{
AnimationSet set = new AnimationSet(false);
Animation fadeout= new AlphaAnimation(1.0f, 0.0f);
fadeOut.setInterpolator(new LinearInterpolator());
fadeOut.setDuration(3500);
set.addAnimation(animation);
myListView.startAnimation(fadeOut);
myListView.setVisibility(View.GONE);
}
As there was some problem with the interpolator.
The other issue I had was that the ListView I was testing on was still in a ScrollView as a part of an old design, once it was taken out it worked as was intended with the changes above.
I'd like to move my view from first layout to another layout, I use TranslateAnimation but it move my view to wrong place.
How I can get view's coordinates and move my view to them?
code:
private void moveViewToNextView(View view,View v2) {
int originalPos[] = new int[2];
v2.getLocationOnScreen(originalPos);
TranslateAnimation anim = new TranslateAnimation(0, originalPos[0], 0, originalPos[1] );
anim.setDuration(1000);
anim.setFillAfter(true);
view.startAnimation(anim);
}
First of all use Object animator instead of TranslateAnimation , Object animator is only thing that moves object , translateanimation doesnt change its position on screen.
ObjectAnimator transAnimation= ObjectAnimator.ofFloat(view, propertyName, fromX, toX);
transAnimation.setDuration(3000);
transAnimation.start();
I have an AnimationSet with inside a ScaleAnimation and a TranslateAnimation like this:
TranslateAnimation:
TranslateAnimation goTopFromRight =
new TranslateAnimation(0, -(right.getLeft()-top.getLeft()),
0,-(right.getTop()-top.getTop()));
ScaleAnimation:
ScaleAnimation = setSizeForTop = new ScaleAnimation(1, 2, 1, 2);
and AnimationSet:
bringToLeftFromTopAnimationSet = new AnimationSet(true);
bringToTopFromRightAnimationSet.addAnimation(goTopFromRight);
bringToTopFromRightAnimationSet.addAnimation(setSizeForTop);
The problem is that when i try to use only the ScaleAnimation, my item goes to the position i want, but whe I am using the ScaleAnimation with the TranslateAnimation in the AnimationSet, my item translates more than i need, as if ScaleAnimation introduces some supplementary movements abd I don't know how to delete them.
Thank you for your help.
The correct solution is to change order of animations. Scale must go first:
bringToTopFromRightAnimationSet.addAnimation(setSizeForTop);
bringToTopFromRightAnimationSet.addAnimation(goTopFromRight);
To avoid the ScaleAnimation movement when using the ScaleAnimation and TranslateAnimation, you have to multiply the TranslateAnimation parametres with those from ScaleAnimation like this:
TranslateAnimation goRightFromTop;
ScaleAnimation sizeNotUp;
goRightFromTop = new TranslateAnimation(0,(-( invisibleCenterImageView.getLeft() - imageViewRight.getLeft() ))*(1/0.6667f), 0, (-( invisibleCenterImageView.getTop() - imageViewRight.getTop()))*(1/0.6667f));
sizeNotUp = new ScaleAnimation(1,0.6667f,1,0.6667f,Animation.RELATIVE_TO_SELF, (float)0.5, Animation.RELATIVE_TO_SELF, (float)0.5);
And then, surprise you deleted the ScaleAnimation movement.
I remember having weird problems with nested animations as well.
Have you tried setting the pivot point manually? See public ScaleAnimation (float fromX, float toX, float fromY, float toY, float pivotX, float pivotY) from http://developer.android.com/reference/android/view/animation/ScaleAnimation.html for it.
It could work.
My solution is to add a container outside your view, and apply the scale animation to your view while applying translate animation to the container.
Then you can make more complex animation even via XML~
I couldn't find examples of how to initialize the animation object.
example Animation ticketAnim;
well new Animation(); is not a valid object it seems so I can't just do Animation ticketAnim = new Animation(); but I would like to. I take the suggested initialization route that the IDE offers which is Animation ticketAnim = null;
of course, accessing this will result in a null pointer exception
what is the right way to do this?
When declaring a new animation, you need to use the constructor of an animation type. Here's some sample code for one of the animation controllers I use in my code:
private void addDeleteDropAnimation() {
AnimationSet set = new AnimationSet(true);
Animation animation = new AlphaAnimation(0.0f, 1.0f);
animation.setDuration(150);
set.addAnimation(animation);
animation = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0.0f,Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, -1.0f,Animation.RELATIVE_TO_SELF, 0.0f
);
animation.setDuration(300);
set.addAnimation(animation);
controllerDel = new LayoutAnimationController(set, 0.5f);
vw_delLinearLayout.setLayoutAnimation(controllerDel);
}
The Animation class itself is just an abstraction. To use an animation, implement one of Animation's direct know subclasses (also specified in the link to the Animation API).
These include:
AlphaAnimation
TranslateAnimation
RotateAnimation
ScaleAnimation
If you want, you can also create your own custom animation by extending the Animation class. A good example of creating a custom animation can be found here.
I applyed a TranslateAnimation to an EditText with the FillAfter=true in order to keep its position at the end of the animation.
The animation works properly but the problem is that I can't enter to the edittext anymore.
I think it's due to the fact that the animation affects only the rendering without modifying the actual view coordinates.
Is there any possibility to maintain the final coordinates with the edittext normally working?
Thanks,
Daniele
Unfotunately the animation only renders the raw pixels of the animated element, but not its "android-intern" position. The best solution (that i am able to come up with) is to use an AnimationListener and set the position of the animated element correctly after the animation has finished. Here is my code to slideDown a searchWrapper:
public void toggleSearchWrapper() {
AnimationSet set = new AnimationSet(true);
// slideDown Animation
Animation animation = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 1.0f
);
animation.setDuration(300);
animation.setFillEnabled(false);
animation.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(final Animation anim) {
};
#Override
public void onAnimationRepeat(final Animation anim) {
};
#Override
public void onAnimationEnd(final Animation anim) {
// clear animation to prevent flicker
searchWrapper.clearAnimation();
// set new "real" position of wrapper
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.BELOW, R.id.branchFinderIncludeHeader);
searchWrapper.setLayoutParams(lp);
}
});
set.addAnimation(animation);
// set and start animation
searchWrapper.startAnimation(animation);
}