Animation In Changing Layoutparams In Linearlayout
Solution 1:
I think you just want to animate a view from 0 height to its final height, you can do this with a custom Animation:
publicclassShowAnimextendsAnimation {
int targetHeight;
View view;
publicShowAnim(View view, int targetHeight) {
this.view = view;
this.targetHeight = targetHeight;
}
@OverrideprotectedvoidapplyTransformation(float interpolatedTime, Transformation t) {
view.getLayoutParams().height = (int) (targetHeight * interpolatedTime);
view.requestLayout();
}
@Overridepublicvoidinitialize(int width, int height, int parentWidth,
int parentHeight) {
super.initialize(width, height, parentWidth, parentHeight);
}
@OverridepublicbooleanwillChangeBounds() {
returntrue;
}
}
And do this in your code to start the animation:
Animationani=newShowAnim(headerView, 100/* target layout height */);
ani.setDuration(2000/* animation time */);
headerView.startAnimation(ani);
Solution 2:
use animateLayoutChanges xml and enableTransitionType in java or kotlin
1. add animateLayoutChanges to root layout xml
<LinearLayoutandroid:id="@+id/mainLinearLayout"android:layout_width="match_parent"android:layout_height="wrap_content"android:animateLayoutChanges="true"android:orientation="vertical"><android.support.v7.widget.AppCompatEditTextandroid:id="@+id/editText"android:layout_width="match_parent"android:layout_height="0dp" /></LinearLayout>
2. in java
LayoutTransitionlayoutTransition= mainLinearLayout.layoutTransition;
layoutTransition.setDuration(5000); // Change duration
layoutTransition.enableTransitionType(LayoutTransition.CHANGING);
editText.layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT; // you can set number, example: 300
editText.requestLayout();
3.in kotlin
vallayoutTransition= mainLinearLayout.layoutTransition
layoutTransition.setDuration(5000) // Change duration
layoutTransition.enableTransitionType(LayoutTransition.CHANGING)
editText.layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT // you can set number, example: 300
editText.requestLayout()
Solution 3:
Since we have the layout transitions in android since JELLYBEAN we can use that instead of using the animation object.
The article below explains it in detail. https://proandroiddev.com/the-little-secret-of-android-animatelayoutchanges-e4caab2fddec
In short we would only need this code -
tView.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
tView.setLayoutParams(lp);
Here lp would be the layout params
RelativeLayout.LayoutParamslp=newRelativeLayout.LayoutParams
(RelativeLayout.LayoutParams.MATCH_PARENT, newHeight);
One more thing to add would be to add this line in the layout file, to the layout that would be doing the transition.
android:animateLayoutChanges="true"
Solution 4:
To animate change in Layout params of a Linear layout and its child you can use LayoutTransition.
Its important that you define and attach transition to Linear layout parent for eg: llRoot.setLayoutTransition(layoutTransition)
in below code snippet before c hanging LayoutParams of child.
Support for LayoutTransition is above Android JellyBean
privatevar AnimationDuration = 1100f@RequiresApi(Build.VERSION_CODES.JELLY_BEAN)privatefunfadeOutControls() {
var layoutTransition = LayoutTransition()
layoutTransition.setDuration(AnimationDuration.toLong()) // Change duration
layoutTransition.enableTransitionType(LayoutTransition.CHANGING)
layoutTransition.addTransitionListener(object : LayoutTransition.TransitionListener {
overridefunstartTransition(transition: LayoutTransition, container: ViewGroup, view: View, transitionType: Int) {
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)overridefunendTransition(transition: LayoutTransition, container: ViewGroup, view: View, transitionType: Int) {
//Change this line of code to below one
transition.disableTransitionType(LayoutTransition.CHANGING)
}
})
// set transition to Linear layout
llRoot.setLayoutTransition(layoutTransition)
// change Layout params of child now to animate Transitionval lp = LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT)
lp.weight = 10f
mediaRoot.setLayoutParams(lp)
leftControl.visibility = View.GONE
rightControl.visibility = View.GONE
}
Solution 5:
If you want to implement the slider animation for your layout then see this demo.
UPDATED
Also see http://www.inter-fuser.com/2009/07/android-transistions-slide-in-and-slide.html
Hope it will help you.
If not then let me know.
Thanks. Enjoy. :)
Post a Comment for "Animation In Changing Layoutparams In Linearlayout"