New layout sliding in from bottom pushing the entire layout upwards - android

I am trying to create a screen, on some operation it is required to slide a layout from bottom to the height to match it's length, with a translation animation kind of slide.
Have a look at the screen example:
As you can see the initial screen layout, the new view 5 slides in from bottom, pushing the entire layout upwards with an animation.
I have tried this: https://stackoverflow.com/a/19766034/1085742
Using the above link, the new view is visible with animation but screen is not sliding down to show the new view 5.
Any idea how to do this!!
Thanks in advance.

I also tested this. Apparently an animation with translateY or Y does not change the layout bounds. In other words, the other views do not move up. Neither when using an Animation nor an Animator.
One solution is to animate the margins, if the animated view is in a layout that supports margins. In Kotlin:
val collapse = ValueAnimator.ofInt(0, -someLayoutAndViews1.height)
collapse.addUpdateListener {
val params = someLayoutAndViews1.layoutParams as ViewGroup.MarginLayoutParams
params.topMargin = it.animatedValue as Int
someLayoutAndViews1.layoutParams = params
}
collapse.start()

Related

Views right and bottom of layout behave differently than views on left and top when moving

I'm creating a game app, and when I want to create intro page, I have 4 bitmaps that should "enter" the scene, one on each side (top, left, right, bottom). They are all programmatically added to scene.
Scene have simple layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="#+id/playground"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipChildren="false"
android:clipToPadding="false"
>
</RelativeLayout>
</RelativeLayout>
I have determining playgroud view size (in pixels) and adding 4 images. Left should be added at the left side of view and then moved to original position. Same for all other images, they are outside of view, and the moved inside a view.
Sample of adding left image:
RelativeView playground = view.findViewById(R.id.playground);
/* some playground size calculations..., after ViewTreeObserver */
int playAreaWidth = playground.getWidth();
int playAreaHeight = playground.getHeight();
ImageView leftBack = new ImageView(requireContext());
leftBack.setScaleType(ImageView.ScaleType.FIT_XY);
leftBack.setImageResource(R.drawable.back_left);
playground.addView(leftBack);
/* some image size calculations... gettnig variables maxLeftWidth */
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(maxLeftWidth, playAreaHeight);
params.leftMargin = -maxLeftWidth;
params.topMargin = 0;
leftBack.setLayoutParams(params);
and a simple animation, to move left image from outside a view to x coordinate 0
Animation a = new Animation() {
#Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
RelativeLayout.LayoutParams paramsMoving = new RelativeLayout.LayoutParams(maxLeftWidth, playAreaHeight);
paramsMoving.leftMargin = (int) (-maxLeftWidth + maxLeftWidth * interpolatedTime);
paramsMoving.topMargin = 0;
leftBack.setLayoutParams(paramsMoving);
}
};
a.setDuration(300);
leftBack.startAnimation(a);
and that works as expected. Same for top image moving down.
However, problem is with bottom and right images. Instead of moving from outside a view into a view, they just scale. Looks like right edge of a right image and bottom edge of a bottom image cannot be outside a view. x position of right image and y position of bottom image are changing correctly during animation, but images have not proper width (right image) and height (bottom image).
Is there any way to force it to be displayed in a proper size outside a view?
I was thinking about ConstraintLayout or MotionLayout (like Lalit Fauzdar said) but, I'm still not familiar with them and there will be a lot of static animation views, all dynamically added, depending how game goes on.
Solution I found is just to use FrameLayout instead of RelativeLayout (and FrameLayout.LayoutParams instead of RelativeLayout.LayoutParams) and all works like a charm.

Animate layout size change

I have an activity like below screen.
View 1 have width and height as match_parent respectively. View 2 have width match_parent and fixed (200 dp) height.
On a button click I change the visibility (from visible to gone) of View 2. As soon as I hide View 2, View 1 comes in full screen instantly or with a jerk. I want the View 1 to gradually expand to full screen with animation instead of instantly.
How can I animate this layout size change?
You can set android:animateLayoutChanges="true" on the parent layout. This works from API 11 and onward.
You need to use scale animation for view1 and fade out for view2 and then use Animatorset to run both at the same time. For example for a simple scale animation
ScaleAnimation anim = new ScaleAnimation(fXscale, toXscale, fYscale, tYscale, Animation.RELATIVE_TO_SELF, (float)0.5, Animation.RELATIVE_TO_SELF, (float)0.5);//fix to fit your needs.

How Click on Animated View or Object in Android?

I have an ImageButton with TranslateX Animation Right to Left like Merquee, So it animated right to left. Now when i click on that nothing happened. Actually click is not perform and click only perform on real position of imagebutton.
what to do any suggestion? greatly appriciate... Thanks
Use ObjectAnimator(For later version then Honeycomb) for Animatinfg Your Objects, You can use follwing code for references:
RelativeLayout layout = (RelativeLayout) findViewById(R.id.relativeLayout1);
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
ObjectAnimator mover = ObjectAnimator.ofFloat(layout, "translationX",
width, -width);
mover.setDuration(10000);
mover.setRepeatMode(Animation.INFINITE);
mover.setRepeatCount(Animation.INFINITE);
mover.start();
If you Are Using Api lower Then the HoneyComb(Like Gingerbread) then Use this Library: http://nineoldandroids.com/
It will Working As its Working in my devices.
As of now, the Animations which alter a View's matrix Only change the co-ordinates where the view is drawn, and not the actual location of View in Layout. So, its just a canvas transform when onDraw() of that View is being called.
So, you can setTranslationX(100) and view will be drawn a 100 pixels to right. But, the click area (getHitRect()) is still on the same place which was assigned to view on layout pass.
Or, you can actually place the view where it should be after animation, and run the animation in reverse.
If you want to actually alter the layout, you will have to alter that view's LayoutParams and change width/height/margin on it. Then you will have to requestLayout() on each frame of animation.
Example : This will animate left margin of a view inside a FrameLayout:
//---assuming animated view is a child of Framelayout---
FrameLayout parent;
View animatedChild;
ValueAnimator animator = new ValueAnimator();
animator.setFloatValues(0,parent.getWidth()); //--slide out to right--
animator.setDuration(1000);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
FrameLayout.LayoutParams params = animatedChild.getLayoutParams();
Float margin = (Float) valueAnimator.getAnimatedValue();
params.leftMargin = margin.intValue();
animatedChild.requestLayout();
}
});
animator.start();
I run into similar problems before when developing my android app. And I found when animation is running, it is actually making the image layer is flowing around. So you need to click on the original position of the button, which means you may need to click on a black space if the button has started moving.

move the screen content to the left outside the screen android

is there a way to move the whole screen content outside the screen bounds, with titlebar and all, on the left for example, i tried
content = ((LinearLayout) act.findViewById(android.R.id.content)
.getParent());
to get the screen content and add a margin to it
FrameLayout.LayoutParams pr = (android.widget.FrameLayout.LayoutParams) content
.getLayoutParams();
pr.rightMargin = xOffset;
content.setLayoutParams(pr);
but it doesnt work, the margin appears but the content is not moved just resized to fit the screen, anyone know how to do this the simple way?
you can set the margin in negative(-) values.
for example if your want the layout to move upper use this:
android:marginTop = "-50dip or more u can put"
Are you trying to flip between views? As in, moving that layout off the screen to show another one? If so you should look at using a widget called ViewFlipper which will do exactly as you need. It can also be animated etc.
http://developer.android.com/reference/android/widget/ViewFlipper.html

Android animate view comming into screen that pushes the current full screen view out

I need this for a project pre API11.
Im looking for a way to add a view in from right, that will cause the current full screen view to move out to left as to allow the new view to be visibe in the screen.
the new view is about 1/3 of the screen width so thats the distance the current fusll screen view should move out of the screen.
i have tired various solutions with TranslateAnimation(), although im happy with the animation, i'm having issues with all the clickable areas in the original view not being where they are supposed to be.
so i started playing with the layout to position it correctly with negative margin, but after a couple of seconds when a new layout request comes the view is popped back into place. also this solution had a choppy effect when moving the view on the animation end listener.
what would be the most simple solution that has good animation when the new view is entering or exiting and that puts the view in the correct place in terms of layout.
Since you're using API11, use the Animator API. All views have properties now with corresponding setters and getters. To shift a view to the left out of scope, get the width of the screen then do:
View viewToShiftOut = getOutView();
ObjectAnimator outAnim = ObjectAnimator.ofFloat(viewToShiftOut, "x", 0, -widthOfScreen);
outAnim.setDuration(1000);
outAnim.start();
This will shift the view and it's contents entirely out of the screen for a duration of 1 second (1000 miliseconds). To shift one in it's place do this:
View viewToShiftIn = getInView();
ObjectAnimator inAnim = ObjectAnimator.ofFloat(viewToShiftIn, "x", widthOfScreen, 0);
inAnim.setDuration(1000);
inAnim.start();
All the properties, clickable areas, etc. will follow the view so you'll be done when the animations finish. You can animate them together like so:
View viewToShiftOut = getOutView();
View viewToShiftIn = getInView();
ObjectAnimator outAnim = ObjectAnimator.ofFloat(viewToShiftOut, "x", 0, -widthOfScreen);
ObjectAnimator inAnim = ObjectAnimator.ofFloat(viewToShiftOut, "x", widthOfScreen, 0);
outAnim.setDuration(1000);
inAnim.setDuration(1000);
outAnim.start();
inAnim.start();
Blog post
EDIT: I noticed you wanted to only shift the view by a 1/3rd, so you can get the amount of pixels to shift each by widthOfScreen/3.
EDIT 2: Another advantage is you can use it with hardware acceleration without trouble which most 3.0+ tablets have.
Instead of using fillAfter and fillEnabled, as you're probably doing, you will need to programmatically alter your layout in the onAnimationEnd of an AnimationListener to match your desired final positioning.
To remove the flicker, call clearAnimation() on the View you are animating, also in onAnimationEnd.

Categories

Resources