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
Related
I'm sure I'm missing something obvious but I'm unable to get my ViewFlipper to animate smoothly from one layout to the next.
My desire is to have the animation move at a constant speed. Not to accel or decel. The overall desired effect is to have the flipper animate in perpetuity with no visible stutter or hiccup, so that the two (or more) layouts just keep moving along, as though on a looped reel.
However, when I don't set an Interpolation it defaults to *some default built-in interpolation that slows down to 0 towards the end.
And when I *do set the Interpolation there's no number I seem to be able to pass in to give me the smooth animation result. 0 stops the animation outright and 1 does (seemingly) what the default does.
What am I overlooking?
here's my animation method that I am passing (as part of my custom Animation to the ViewFlipper:
public Animation inFromRightAnimation(int duration) {
Animation inFromRight = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, +1.0f, Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f
);
inFromRight.setDuration(duration);
AccelerateInterpolator ai = new AccelerateInterpolator();//tried 0 and 1.0f as params without success
inFromRight.setInterpolator(ai); //
return inFromRight;
}
Try using LinearInterpolator instead of AccelerateInterpolator. LinearInterpolator ensures that your views will move with a constant velocity without any acceleration or deceleration.
I am working on an application where i need to translate a view from one cordinate to another.
x1 = 191, y1 = 300
x2 = 50 , y2 = 150
I used the following code, but its behaving very strangly, I mean it's nor translating between those two points.
Code:
Animation transAnim = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, x,
Animation.RELATIVE_TO_PARENT, pawnInitLocation[0],
Animation.RELATIVE_TO_PARENT, y,
Animation.RELATIVE_TO_PARENT, pawnInitLocation[1]);
transAnim.setDuration(1000);
transAnim.setFillEnabled(true);
transAnim.setFillAfter(true);
v.startAnimation(transAnim);
I am new to animations in android and havent really achieved anything like this. Any help would be great.
Animation.RELATIVE_TO_PARENT specifies that the value you are providing is a float value to be multiplied by the width or height of the parent. 1.0 corresponds to 100% of the parent's corresponding dimension.
An example of this would be animating a view completely off the screen by specifying 1.0 for 100% creating an exit-right animation or -1.0 for -100% creating an exit-left animation.
TranslateAnimation
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"
I am rotating a rectangle area view and applying the transformation after in this manner
AnimationSet snowMov1 = new AnimationSet(true);
RotateAnimation rotate1 = new RotateAnimation(0,-90, Animation.RELATIVE_TO_SELF,0.0f , Animation.RELATIVE_TO_SELF,0.0f );
rotate1.setDuration(000);
snowMov1.addAnimation(rotate1);
TranslateAnimation trans1 = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 1.0f);
trans1.setDuration(000);
snowMov1.addAnimation(trans1);
snowMov1.setFillAfter(true); //this will apply the animation and keep the transformation
This however does not rotate the focus area of that view. The focus area remains the same.
Can someone please help me how the focus area can be rotated as well ??
Thank you.
Unfortunately, there is no way to do this automatically. Animations only update the drawing position of views, and not their actual position. Your best bet here would be to set a listener for the animation and to actually change the position in the onAnimationEnd method.
I'm trying to scale a View equivalent to the right and left from center of a View using ScaleAnimation . Whatever values I set for pivotX and PivotY it always scale in the same way (like right edge appears to be scaling keeping left edge constant). Below is the code I used to initialize the ScaleAnimation. Can anyone please let me know, if Im doing anything wrong?. Thanks.
final ScaleAnimation scaleAnim = new ScaleAnimation(1.0f, 2.0f, 1.0f, 1.0f,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
Initialing the anim solved the problem.
I just used the below to code to initialise it.
anim.initialize(/* animate view */child.getWidth(),
child.getHeight(),
/* parents view */ this.getWidth(),
this.getHeight());