Android: use animation outside the view area - android

I have an xml with 12 image. for example 3*4 table, where each cell contains an image.
In the OnCreate I write animation every image:
AnimationSet set = new AnimationSet(true);
Animation animation = new TranslateAnimation(
Animation.ABSOLUTE, -300.0f, Animation.START_ON_FIRST_FRAME, 0.0f,
Animation.ABSOLUTE, -800.0f, Animation.START_ON_FIRST_FRAME, 0.0f);
animation.setFillBefore(true);
animation.setFillAfter(true);
animation.setFillEnabled(true);
animation.setDuration(5000);
I want to start the animation from the top left of the screen and to end the animation to real position. But the animation is only use the image or cell region. Is there any solution to the animation, use the whole screen?

If they're all under the same parent view, you can set the parent's clipChildren attribute to false, which would allow the animation to extend beyond its own constraints (as long as it still remains within the boundaries of its parent):
android:clipChildren="false"

Related

TranslateAnimation from middle to Top Left without cutting off view

I have a parent container called mContainer and a child view called mChildImageView. The ImageView is positioned in the middle of the container and I am trying to translate the ImageView to the top left without cutting off the ImageView.
What I would like is to translate the top most point of the ImageView to the top left part of mContainer, this way it would make the ImageView cut off.
This is the following code I am using, as you can see I am using Absolute coordinates, I would like to make it more dynamic how can I accomplish this?
TranslateAnimation a = new TranslateAnimation(
Animation.ABSOLUTE, // Specifies X-Type interpretation, ABSOLUTE, RELATIVE_TO_SELF, RELATIVE_TO_PARENT
0.0f, // Change in x coordinate at start of animation, can be absolute or percentage if RELATIVE
Animation.ABSOLUTE,
-30, // Change in x coordinate at end of animation, can be absolute or percentage if RELATIVE
Animation.ABSOLUTE, // Specifies Y-Type interpretation, ABSOLUTE, RELATIVE_TO_SELF, RELATIVE_TO_PARENT
0.0f, // Change in y coordinate at start of animation, can be absolute or percentage if RELATIVE
Animation.ABSOLUTE,
30 // Change in y coordinate at end of animation, can be absolute or percentage if RELATIVE
);
mChildImageView.setAnimation(a);
mChildImageView.animate();
If you want to change TranslationX or/and TranslationY parameters of a view with relation to its parent, I highly recommend you to use Animators instead of Animations as they have some drawbacks (for example: Android - Animation Issue).
Possible variant:
mChildImageView.animate().translationX(newTranslationX).translationY(newTranslationY).setDuration(duration).start();
I figured it out so to get it to move to the top left we can do the following
TranslateAnimation a = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT,
0.0f,
Animation.RELATIVE_TO_PARENT,
-20.0f, // Translate to left - This is arbitrary enter your own value
Animation.RELATIVE_TO_PARENT,
0.0f,
Animation.RELATIVE_TO_PARENT,
-15.0f // Translate up - This is arbitrary enter your own value
);
mChildImageView.setAnimation(a);
mChildImageView.animate();
RELATIVE_TO_PARENT uses percentages so 0.0f to 1.0f

animate background image in horizontal scrollview item click

I need to animate a background view either left to right or right to left on in a horizontal scroll view item click. So it may indicate the selected item to user.
I am currently taking the absolute screen positions of each view on screen and translating them to there x and y positions on item click.
TranslateAnimation animation = new TranslateAnimation(xFrom, xTo, yFrom, yTo);
animation.setDuration(1000);
animation.setRepeatCount(0);
animation.setRepeatMode(2);
animation.setFillAfter(true);
mScroller.startAnimation(animation);
But i am failed to achieve the scenario?
So, I have achieve the scenario with ObjectAnimator.
ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(mSliderMember,"translationX", xFrom, xTo);
objectAnimator.setDuration(1000); // animation duration
objectAnimator.start();

How to refresh layout after animation in android?

I have a layout which moves on a specific user action. The layout moves a bit up, but buttons are clickable where they were before animation. How do I update the layout's children onAnimationEnd?
Here I could not find the answer, but I understood the problems is in how the Android Layout system is build.
I tried:
popupLayout.requestLayout();
And:
popupLayout.invalidate();
Neither worked. Any other Ideas on how to update the contained buttons?
Update
Here is how I do the animation:
Animation slide = null;
slide = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
0.0f, Animation.RELATIVE_TO_SELF, -0.3f);
slide.setDuration(300);
slide.setFillAfter(true);
slide.setFillEnabled(true);
popupLayout.startAnimation(slide);
// and then the AnimationListener
Update
I tried to set LayoutParams and it does not work neither.
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
dialogLayout.getWidth(), dialogLayout.getHeight());
// lp.setMargins(0, 0, 0, 0);
dialogLayout.setLayoutParams(lp);
Inside onAnimationEnd() you have to update your layout parameters of the view which you are animating, in your case it's LinearLayout. You have to update view in respect of the parent. If your animated view parent is e.g. RelativeLayout you can call this:
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) animatedView.getLayoutParams();
//TODO modify params
animatedView.setLayoutParams(params);
animatedView.requestLayout();
I think this should work. You can also try calling requestLayout() on the parent.
There is also tutorial on YT about View animations. Chet Haase talks about handling animations including TranslateAnimation. I don't have time to watch but maybe you will find there your answer.
Update
If you manage successfully set layout params after animation, don't forget to remove line:
slide.setFillAfter(true);
Remember that animation modify ONLY drawing coordinates relative to the view position. In other words, it creates an offset to original position which has to be reset after animation ends. Method setFillAfter(true) skip reseting this offset.
setContentView(rootLayout) will do the job... not working

Android Image View Animation leaving behind trail

I am animating an ImageView from the center of the screen to the top-left using an Animation Set.I am Translating and Scaling the View.
Heres the code snippet.
AnimationSet tempAnimation=new AnimationSet(true);
TranslateAnimation anim = new TranslateAnimation( 0,xPos, 0, yPos );
anim.setFillAfter( true );
anim.setFillEnabled(true);
ScaleAnimation scaleanimation = new ScaleAnimation(1, (float)0.22, 1, (float)0.22, Animation.RELATIVE_TO_SELF, (float)0.5, Animation.RELATIVE_TO_SELF, (float)0.5);
scaleanimation.setDuration(1000);
scaleanimation.setFillEnabled(true);
scaleanimation.setFillAfter(true);
tempAnimation.addAnimation(scaleanimation);
tempAnimation.addAnimation(anim);
tempAnimation.setDuration(1000);
// This takes care that the ImageViews stay in their final positions after animating .
tempAnimation.setFillEnabled(true);
tempAnimation.setFillAfter(true);
mimageView.startAnimation(tempAnimation);
So,the problem is that it is leaving behind a trail /previous positions for just a brief moment.But it doesnt look good or smooth.I have noticed that if I use only 1 animation it is fine but using the animation set causes the trail.Are there any tips to avoid this?
Cheers
Found the answer eventually at https://code.google.com/p/android/issues/detail?id=22151.
Scale and Translation Animations have a problem on Android 2.3 .The simplest way to fix this is to add a small transparent padding around the image.In the code shown above,Just add
mimageView.setPadding(1,1,1,1);
Works like a charm!

How to rotate a drawable by ObjectAnimator?

Alphaing a drawable work well like this:
if(mAlphaAnimation == null){
mAlphaAnimation = ObjectAnimator.ofFloat(this, "alpha", 0.0f,1.0f).setDuration(TARGET_ANIM_ALPHA_DURATION);
mAlphaAnimation.setInterpolator(new AccelerateDecelerateInterpolator());
mAlphaAnimation.setStartDelay(TARGET_ANIM_ALPHA_DELAY_BASE*power);
mAlphaAnimation.setRepeatCount(ValueAnimator.INFINITE);
mAlphaAnimation.setRepeatMode(ValueAnimator.REVERSE);
mAlphaAnimation.addUpdateListener(this);
}
But if I want rotate a drawable like below , it don's work.
private void createRotateAnim(float fromDegress,float toDegress,int duration){
if(mRotateAnimation == null){
mRotateAnimation = ObjectAnimator.ofFloat(this, "rotation",fromDegress,toDegress).setDuration(duration);
mRotateAnimation.setStartDelay(100);
mRotateAnimation.setInterpolator(new AccelerateInterpolator());
mRotateAnimation.addUpdateListener(this);
}
}
Anyone can help me to fix this issue, or these is any other way to create a rotation drawable animation .
I am sorry to my poor English.
Try with ObjectAnimator instead.
ImageView imageview = (ImageView)findViewById(R.id.image);
ObjectAnimator imageViewObjectAnimator = ObjectAnimator.ofFloat(imageview ,
"rotation", 0f, 360f);
imageViewObjectAnimator.setDuration(1000); // miliseconds
imageViewObjectAnimator.start();
EDIT
Since this question draw some attention let me to explain why to use ObjectAnimator instead of other Transition animators
The thing about using ObjectAnimator is that it's moving both the visible and the clickable area of the item, if you use another animation method, for example Transition Animation or some other Animators, and let's say if you want to move the Button from the bottom left of the screen to the top left, it will only move the visible area but not the Button itself, the clickable area will still be on the previous position, in this case the clickable area will still be on the bottom left instead of the top left where you moved the button.
If you do the same with ObjectAnimator, both the visible area, and the clickable area will move the the desired location.
Try this simple Rotation Animation applied to a image.
ImageView imageview = (ImageView)findViewById(R.id.myimage);
RotateAnimation rotate = new RotateAnimation(180, 360, Animation.RELATIVE_TO_SELF,
0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rotate.setDuration(500);
imageview.startAnimation(rotate);
This answer is just for a sake of question, it is correct that Clickable area will be different than View's current position. Please check this question for making clickable area correct. Button is not clickable after TranslateAnimation

Categories

Resources