Animating view: set translationY to 0 while also scaling - android

I have a view that I want to animate- scale it to a bigger size and at the same time move to the very top of the screen, just below the notification bar. The problem is however, at least I think, that the translationY complies with initial view scale and moves it to the top of the screen, but as the view grows, it gets cut off at top.
This is the code I'm using
image.animate()
.alpha(1f)
.translationY(0)
.scaleX(6f)
.scaleY(6f)
.start()
Any simple solution to this? A way I could calculate the real value I should set translationY to (based on scale) in order to be positioned correctly? Any help appreciated!

Related

Move and resize a test with an animation in Android

I want to move and resize a TextView from the center of the screen to the upper left corner with the default 16dp margin. I used the code:
val pos = convertDpToPixel(16.0f, context)
testNameTextView
.animate()
.scaleX(0.5f)
.scaleY(0.5f)
.x(pos)
.y(pos)
It does not work because the final margin depends on the length of the text.
If I do not resize the view everything works fine.
I have also tried to do this:
testNameTextView
.animate()
.scaleX(0.5f)
.scaleY(0.5f)
.withEndAction {
testNameTextView
.animate()
.x(pos)
.y(pos)
}
but it did not make any difference. How can I resize and move the text?
I used:
testNameTextView
.animate()
.scaleX(SCALE)
.scaleY(SCALE)
.x(posTestName - (testNameTextView.width - testNameTextView.width * SCALE) / 2 )
.y(posTestName - (testNameTextView.height - testNameTextView.height * SCALE) / 2 )
So what happens is that the text is repositioned and then scaled keeping the center of the text fixed, so if you just do what I had done the left margin is incorrect because the text was shrunk.
You need to subtract the difference between half the width of the text before the scale and half the width of the text after the scaling.
PS
During the animation the translation and the resizing are done at the same time but the effect is the same as if the text was first translated and then scaled keeping the center of the text fixed

Android bottomsheet - How i can set onSlide offset to 1 when card state is half_expanded?

i have set my bottomsheet ot be half expanded using the folloiwng code:
bottomSheetBehavior?.isFitToContents = false
bottomSheetBehavior?.halfExpandedRatio = 0.6f
and it works fine. The issue is I have some content that changes alpha property as the card slides to halfway point. At the halfway expanded point, the alpha of the content should be 1. it works great without using isFitToContents = false, but when I do use it to half expand the card the slideOffset does not end at 1, for example, it ends at .019 because I just moved up a little bit to reach halfway expanded.
Therefore to my issue: How to make the half_expanded state slideOffSet end at 1 ?
another approach: if I could know the very last half expanded offset value then I can use that ahead of time as a scale factor. let's say that value was called halfExpandedOffSetEnd. then I could create a scale factor like this:
val multiplier = 1/halfExpandedOffSetEnd
I could then multiply that by all my offsets to get the desired effect. but how would i get the very last offset value for half expanded ahead of time?

Whats the anchor point on a view when using the translateX and translateY in Android

I have a RelativeLayout with a view inside. The view overtimes changes it's position so my question is I'm running into an issue where say the top and left coordinates are say max height and max width and the view is displayed partially on screen while the rest is cut off .
So my question is when translating the view is the view adjusting relative to the center pixel of the view or the bottom left top left bottom right or top right?
In the case of translateX and translateY it doesn't matter. No matter what anchor point you would use, the result would be the same. Scale and rotation however rely on the anchor point (or pivot) defined by setPivotX/PivotY.

How to achieve a "pan out" animation on Android

Let's say the following coordinates are relative to (0, 0) being the top left corner of the phone's screen with increasing positive x values going to the right and increasing positive y values going down as diagrammed here: http://t.cyol.com/cache/temp/img/2011/02/1000/119/img/img_1297675862_0.jpg
I'd like to do a "pan out" animation where there is a little box whose top left corner is at (x, y), and it has width w and height h where x, y, w, and h are greater than 0. Everywhere inside that box is some content. Everywhere outside that box is black.
Over 500 milliseconds, the box's top left corner should move to (0, 0) and its width and height will grow to fill the entire screen. That is, over half a second, the box pans out to fullscreen.
The content inside the box is a WebView.
How do I achieve this animation? I tried scaling, but that's not what I want to achieve, because the content inside the box shouldn't get squished. Translating only works if the box starts out in a corner.
In order to achieve this effect, the whole view must be relayouted at each frame of the animation. You'll need to change the layout params of the view and call requestLayout(). Check this link:
Android Animate Scale Image to fit screen width and height

How to position a view off-screen so that it can be Animated to move on-screen?

I have a RelativeLayout filling the screen and a couple of ImageView positioned on it using LayoutParams margins. These ImageView are animated in different ways, and in one case I want an image to "fly in" to the screen from the right.
Unfortunately, if I set leftMargin for that ImageView greater than the width of the screen, it does not appear (or appears cropped if it partially visible at the start of animation).
I tried setting width and height of RelativeLayout to be bigger than screen size - it works, but only partially: if the image is positioned completely off-screen, it does not work, and if the image is partially visible, it is not cropped, but that works only for right and bottom sides.
So, my question is this: how to position several ImageViews on and off the screen so that I can animate them using Animation?
In the end, I used a trick: I combined AnimationDrawable and view animation. I did the following (assuming that the animation has to run T milliseconds):
Position that ImageView on-screen.
Set as its background an AnimationDrawable with following frames:
empty Drawable: 1ms,
normal Drawable: Tms.
Change view's animation to this:
jump to off-screen position (translation with duration=1ms),
do normal animation.
This is probably a good place to start looking for those who would use Fixpoint's solution.
Android — How to position View off-screen?

Categories

Resources