Afficher et masquer la vue avec animation glissade/descente

j'ai
LinearLayout

, que je veux montrer ou cacher en utilisant
Animation

, Qui pousse la mise en page haut ou bas chaque fois que je change de visibilité.

J'ai vu plusieurs échantillons là-bas, mais aucun d'entre eux me convient.

J'ai créé deux fichiers xml Pour l'animation, mais je ne sais pas comment les exécuter quand je change de visibilité
LinearLayout

.
Invité:

Alice

Confirmation de:

Avec une nouvelle animation API, qui a été introduit dans Android 3.0 /Honeycomb/, Très facile à créer de telles animations.

Glisser a
View

Jusqu'à la distance:


view.animate//.translationY/distance/;


Plus tard, vous pouvez déplacer
View

retour à sa position d'origine comme suit:


view.animate//.translationY/0/;


Vous pouvez également facilement combiner plusieurs animations. La prochaine animation va glisser
View

bas à sa hauteur et disparaître en même temps:


// Prepare the View for the animation
view.setVisibility/View.VISIBLE/;
view.setAlpha/0.0f/;

// Start the animation
view.animate//
.translationY/view.getHeight///
.alpha/1.0f/
.setListener/null/;


Alors vous pouvez enlever
View

Retour et déplacez-le à sa position d'origine. Nous avons également installé
AnimatorListener

, Afin que nous puissions établir une visibilité
View

Back B.
GONE

, Dès que l'animation est terminée:


view.animate//
.translationY/0/
.alpha/0.0f/
.setListener/new AnimatorListenerAdapter// {
@Override
public void onAnimationEnd/Animator animation/ {
super.onAnimationEnd/animation/;
view.setVisibility/View.GONE/;
}
}/;

Camille

Confirmation de:

J'ai eu des problèmes de compréhension et d'application de la réponse acceptée. J'avais besoin d'un peu plus de contexte. Maintenant, quand j'ai compris cela, voici un exemple complet:


MainActivity.java


public class MainActivity extends AppCompatActivity {

Button myButton;
View myView;
boolean isUp;

@Override
protected void onCreate/Bundle savedInstanceState/ {
super.onCreate/savedInstanceState/;
setContentView/R.layout.activity_main/;

myView = findViewById/R.id.my_view/;
myButton = findViewById/R.id.my_button/;

// initialize as invisible /could also do in xml/
myView.setVisibility/View.INVISIBLE/;
myButton.setText/"Slide up"/;
isUp = false;
}

// slide the view from below itself to the current position
public void slideUp/View view/{
view.setVisibility/View.VISIBLE/;
TranslateAnimation animate = new TranslateAnimation/
0, // fromXDelta
0, // toXDelta
view.getHeight//, // fromYDelta
0/; // toYDelta
animate.setDuration/500/;
animate.setFillAfter/true/;
view.startAnimation/animate/;
}

// slide the view from its current position to below itself
public void slideDown/View view/{
TranslateAnimation animate = new TranslateAnimation/
0, // fromXDelta
0, // toXDelta
0, // fromYDelta
view.getHeight///; // toYDelta
animate.setDuration/500/;
animate.setFillAfter/true/;
view.startAnimation/animate/;
}

public void onSlideViewButtonClick/View view/ {
if /isUp/ {
slideDown/myView/;
myButton.setText/"Slide up"/;
} else {
slideUp/myView/;
myButton.setText/"Slide down"/;
}
isUp = !isUp;
}
}


activity_mail.xml


xml version="1.0" encoding="utf-8"?
<relativelayout android:layout_height="match_parent" android:layout_width="match_parent" tools:context="com.example.slideview.MainActivity" xmlns:android="[url=http://schemas.android.com/apk/res/android"]http://schemas.android.com/apk/res/android"[/url] xmlns:tools="[url=http://schemas.android.com/tools">]http://schemas.android.com/tools">[/url]
<button android:id="@+id/my_button" android:layout_centerhorizontal="true" android:layout_height="wrap_content" android:layout_margintop="100dp" android:layout_width="150dp" android:onclick="onSlideViewButtonClick"></button>
<linearlayout android:background="#a6e1aa" android:id="@+id/my_view" android:layout_alignparentbottom="true" android:layout_height="200dp" android:layout_width="match_parent" android:orientation="vertical">
</linearlayout>
</relativelayout>


Entrées

remercier
https://colinyeoh.wordpress.co ... ible/
Pour le fait qu'elle m'a dirigé la bonne direction. C'était plus utile que d'autres réponses sur cette page.

Si vous souhaitez commencer par la vue à l'écran, vous ne l'initiez pas comme
INVISIBLE

.

Depuis que nous l'avons animé complètement à l'extérieur de l'écran, il n'est pas nécessaire de le retourner à
INVISIBLE

. Si vous n'agissez pas complètement à l'extérieur de l'écran, vous pouvez ajouter une animation alpha et établir une visibilité en utilisant
AnimatorListenerAdapter

.

https://developer.android.com/ ... .html

Giselle

Confirmation de:

Maintenant, l'animation des changements de visibilité doit être faite à travers
Transition API

, qui est disponible dans le forfait de support /androidx/. Juste appeler la méthode
https://developer.android.com/ ... Group,%20androidx.transition.Transition/
Avec la transition
https://i.stack.imgur.com/8zkQT.gif
, Puis changez la visibilité de la présentation.

https://i.stack.imgur.com/8zkQT.gif

import androidx.transition.Slide;
import androidx.transition.Transition;
import androidx.transition.TransitionManager;

private void toggle// {
View redLayout = findViewById/R.id.redLayout/;
ViewGroup parent = findViewById/R.id.parent/;

Transition transition = new Slide/Gravity.BOTTOM/;
transition.setDuration/600/;
transition.addTarget/R.id.redLayout/;

TransitionManager.beginDelayedTransition/parent, transition/;
redLayout.setVisibility/show ? View.VISIBLE : View.GONE/;
}


activity_main.xml


xml version="1.0" encoding="utf-8"?
<relativelayout android:id="@+id/parent" android:layout_height="match_parent" android:layout_width="match_parent" android:orientation="vertical" xmlns:android="[url=http://schemas.android.com/apk/res/android">]http://schemas.android.com/apk ... gt%3B[/url]
<button android:id="@+id/btn" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="play"></button>
<linearlayout android:background="#5f00" android:id="@+id/redLayout" android:layout_alignparentbottom="true" android:layout_height="400dp" android:layout_width="match_parent"></linearlayout>
</relativelayout>


Vérifier

En utilisant d'autres exemples de transition par défaut et transitions utilisateur.

Babette

Confirmation de:

La solution la plus simple: installer
android:animateLayoutChanges="true"

Sur un conteneur contenant vos points de vue.

Pour le placer dans un contexte, si vous avez une mise en page, comme indiqué ci-dessous, toutes les modifications de la visibilité des espèces dans ce conteneur seront automatiquement animées.


<linearlayout android:animatelayoutchanges="true" android:id="@+id/container" android:layout_height="match_parent" android:layout_width="match_parent">
<views_which_change_visibility>
</views_which_change_visibility></linearlayout>


Vous pouvez trouver des informations plus détaillées à ce sujet sur la page.
https://developer.android.com/ ... .html

Constantine

Confirmation de:

Kotlin

Basé

« s

, Voici un moyen élégant utilisant une expansion de la forme. :


fun View.slideUp/duration: Int = 500/ {
visibility = View.VISIBLE
val animate = TranslateAnimation/0f, 0f, this.height.toFloat//, 0f/
animate.duration = duration.toLong//
animate.fillAfter = true
this.startAnimation/animate/
}

fun View.slideDown/duration: Int = 500/ {
visibility = View.VISIBLE
val animate = TranslateAnimation/0f, 0f, 0f, this.height.toFloat///
animate.duration = duration.toLong//
animate.fillAfter = true
this.startAnimation/animate/
}


Et puis, où que vous souhaitiez l'utiliser, vous avez juste besoin de
myView.slideUp//

ou
myView.slideDown//

Edmond

Confirmation de:

Vous pouvez exécuter le bon
Animation

, Quand visibilité
LinearLayout

Changements en créant une nouvelle sous-classe
LinearLayout

et trop surdidé
setVisibility//

, Pour commencer
Animations

. Considérez quelque chose comme ça:


public class SimpleViewAnimator extends LinearLayout
{
private Animation inAnimation;
private Animation outAnimation;

public SimpleViewAnimator/Context context/
{
super/context/;
}

public void setInAnimation/Animation inAnimation/
{
this.inAnimation = inAnimation;
}

public void setOutAnimation/Animation outAnimation/
{
this.outAnimation = outAnimation;
}

@Override
public void setVisibility/int visibility/
{
if /getVisibility// != visibility/
{
if /visibility == VISIBLE/
{
if /inAnimation != null/ startAnimation/inAnimation/;
}
else if //visibility == INVISIBLE/ || /visibility == GONE//
{
if /outAnimation != null/ startAnimation/outAnimation/;
}
}

super.setVisibility/visibility/;
}
}

Gabriel

Confirmation de:

if /filter_section.getVisibility// == View.GONE/ {
filter_section.animate//
.translationY/filter_section.getHeight///.alpha/1.0f/
.setListener/new AnimatorListenerAdapter// {
@Override
public void onAnimationStart/Animator animation/ {
super.onAnimationStart/animation/;
filter_section.setVisibility/View.VISIBLE/;
filter_section.setAlpha/0.0f/;
}
}/;
} else {
filter_section.animate//
.translationY/0/.alpha/0.0f/
.setListener/new AnimatorListenerAdapter// {
@Override
public void onAnimationEnd/Animator animation/ {
super.onAnimationEnd/animation/;
filter_section.setVisibility/View.GONE/;
}
}/;
}

Emile

Confirmation de:

Vous pouvez glisser de haut en bas dans n'importe quel type ou en phase de mise en page à l'aide du code. bellow Dans l'application android


boolean isClicked=false;
LinearLayout mLayoutTab = /LinearLayout/findViewById/R.id.linearlayout/;

if/isClicked/{
isClicked = false;
mLayoutTab.animate//
.translationYBy/120/
.translationY/0/
.setDuration/getResources//.getInteger/android.R.integer.config_mediumAnimTime//;

}else{
isClicked = true;
mLayoutTab.animate//
.translationYBy/0/
.translationY/120/
.setDuration/getResources//.getInteger/android.R.integer.config_mediumAnimTime//;
}

Edmond

Confirmation de:

Utilisez cette classe:


public class ExpandCollapseExtention {

public static void expand/View view/ {
view.setVisibility/View.VISIBLE/;

final int widthSpec = View.MeasureSpec.makeMeasureSpec/0, View.MeasureSpec.UNSPECIFIED/;
final int heightSpec = View.MeasureSpec.makeMeasureSpec/0, View.MeasureSpec.UNSPECIFIED/;
view.measure/widthSpec, heightSpec/;

ValueAnimator mAnimator = slideAnimator/view, 0, view.getMeasuredHeight///;
mAnimator.start//;
}


public static void collapse/final View view/ {
int finalHeight = view.getHeight//;

ValueAnimator mAnimator = slideAnimator/view, finalHeight, 0/;

mAnimator.addListener/new Animator.AnimatorListener// {

@Override
public void onAnimationEnd/Animator animator/ {
view.setVisibility/View.GONE/;
}


@Override
public void onAnimationStart/Animator animation/ {

}


@Override
public void onAnimationCancel/Animator animation/ {

}


@Override
public void onAnimationRepeat/Animator animation/ {

}
}/;
mAnimator.start//;
}


private static ValueAnimator slideAnimator/final View v, int start, int end/ {

ValueAnimator animator = ValueAnimator.ofInt/start, end/;

animator.addUpdateListener/new ValueAnimator.AnimatorUpdateListener// {

@Override
public void onAnimationUpdate/ValueAnimator valueAnimator/ {

int value = /Integer/ valueAnimator.getAnimatedValue//;
ViewGroup.LayoutParams layoutParams = v.getLayoutParams//;
layoutParams.height = value;
v.setLayoutParams/layoutParams/;
}
}/;
return animator;
}
}

Gabriel

Confirmation de:

Utilisant ObjectAnimator


private fun slideDown/view: View/ {
val height = view.height
ObjectAnimator.ofFloat/view, "translationY", 0.toFloat//, height.toFloat///.apply {
duration = 1000
start//
}
}

private fun slideUp/view: View/ {
val height = view.height
ObjectAnimator.ofFloat/view, "translationY", height.toFloat//,0.toFloat///.apply {
duration = 1000
start//
}
}

Giselle

Confirmation de:

J'ai eu une armoire angulaire où ma taille était encore
zero

so...


import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.view.View;

public final class AnimationUtils {

public static void slideDown/final View view/ {
view.animate//
.translationY/view.getHeight///
.alpha/0.f/
.setListener/new AnimatorListenerAdapter// {
@Override
public void onAnimationEnd/Animator animation/ {
// superfluous restoration
view.setVisibility/View.GONE/;
view.setAlpha/1.f/;
view.setTranslationY/0.f/;
}
}/;
}

public static void slideUp/final View view/ {
view.setVisibility/View.VISIBLE/;
view.setAlpha/0.f/;

if /view.getHeight// > 0/ {
slideUpNow/view/;
} else {
// wait till height is measured
view.post/new Runnable// {
@Override
public void run// {
slideUpNow/view/;
}
}/;
}
}

private static void slideUpNow/final View view/ {
view.setTranslationY/view.getHeight///;
view.animate//
.translationY/0/
.alpha/1.f/
.setListener/new AnimatorListenerAdapter// {
@Override
public void onAnimationEnd/Animator animation/ {
view.setVisibility/View.VISIBLE/;
view.setAlpha/1.f/;
}
}/;
}

}

Camille

Confirmation de:

Voici ma décision. Obtenez juste un lien à votre vue et appelez cette méthode:


public static void animateViewFromBottomToTop/final View view/{

view.getViewTreeObserver//.addOnGlobalLayoutListener/new ViewTreeObserver.OnGlobalLayoutListener// {

@Override
public void onGlobalLayout// {

view.getViewTreeObserver//.removeOnGlobalLayoutListener/this/;

final int TRANSLATION_Y = view.getHeight//;
view.setTranslationY/TRANSLATION_Y/;
view.setVisibility/View.GONE/;
view.animate//
.translationYBy/-TRANSLATION_Y/
.setDuration/500/
.setStartDelay/200/
.setListener/new AnimatorListenerAdapter// {

@Override
public void onAnimationStart/final Animator animation/ {

view.setVisibility/View.VISIBLE/;
}
}/
.start//;
}
}/;
}


Pas besoin de faire autre chose =/

Francis

Confirmation de:

Répondre Suragcha B. Kotlin an. Cela a fonctionné pour moi.


class MainActivity : AppCompatActivity// {

var isUp: Boolean = false

override fun onCreate/savedInstanceState: Bundle?/ {
super.onCreate/savedInstanceState/
setContentView/R.layout.activity_main/

var myView: View = findViewById/R.id.my_view/
var myButton: Button = findViewById/R.id.my_button/

//Initialize as invisible
myView.visibility = View.INVISIBLE
myButton.setText/"Slide up"/

isUp = false

}


fun View.slideUp/duration: Int = 500/{
visibility = View.VISIBLE
val animate = TranslateAnimation/0f, 0f, this.height.toFloat//, 0f/
animate.duration = duration.toLong//
animate.fillAfter = true
this.startAnimation/animate/
}

fun View.slideDown/duration: Int = 500/ {
visibility = View.VISIBLE
val animate = TranslateAnimation/0f, 0f, 0f, this.height.toFloat///
animate.duration = duration.toLong//
animate.fillAfter = true
this.startAnimation/animate/
}

fun onSlideViewButtonClick/view: View/{
if/isUp/{
my_view.slideDown//
my_button.setText/"Slide Up"/

}
else{
my_view.slideUp//
my_button.setText/"Slide Down"/
}
isUp = !isUp
}


}

Daniel

Confirmation de:

Vous pouvez utiliser des trois lignes de code simples pour montrer l'animation ...


//getting the hiding view by animation

mbinding.butn.setOnClickListener {

val SlideOutLeft = AnimationUtils.loadAnimation/this, R.anim.slide_out_left/
simplelayout.visibility = View.INVISIBLE
simplelayout.startAnimation/SlideOutLeft/


val SlideInRight = AnimationUtils.loadAnimation/applicationContext, R.anim.slide_in_right/
animation1.visibility = View.VISIBLE
animation1.startAnimation/SlideInRight/

}
//again unhide the view animation
mbinding.buttn.setOnClickListener {


val SlideInLeft=AnimationUtils.loadAnimation/this,R.anim.slide_in_left/
//set the layout
simplelayout.visibility=View.VISIBLE
simplelayout.startAnimation/SlideInLeft/

val SlideOutRight=AnimationUtils.loadAnimation/this,R.anim.slide_out_right/
animation1.visibility=View.INVISIBLE
animation1.startAnimation/SlideOutRight/

}

Hippolite

Confirmation de:

Avec des extensions Kotlin Tu peux l'utiliser:


enum class SlideDirection{
UP,
DOWN,
LEFT,
RIGHT
}

enum class SlideType{
SHOW,
HIDE
}

fun View.slideAnimation/direction: SlideDirection, type: SlideType, duration: Long = 250/{
val fromX: Float
val toX: Float
val fromY: Float
val toY: Float
val array = IntArray/2/
getLocationInWindow/array/
if//type == SlideType.HIDE && /direction == SlideDirection.RIGHT || direction == SlideDirection.DOWN// ||
/type == SlideType.SHOW && /direction == SlideDirection.LEFT || direction == SlideDirection.UP// /{
val displayMetrics = DisplayMetrics//
val windowManager = context.getSystemService/Context.WINDOW_SERVICE/ as WindowManager
windowManager.defaultDisplay.getMetrics/displayMetrics/
val deviceWidth = displayMetrics.widthPixels
val deviceHeight = displayMetrics.heightPixels
array[0] = deviceWidth
array[1] = deviceHeight
}
when /direction/ {
SlideDirection.UP -> {
fromX = 0f
toX = 0f
fromY = if/type == SlideType.HIDE/ 0f else /array[1] + height/.toFloat//
toY = if/type == SlideType.HIDE/ -1f * /array[1] + height/ else 0f
}
SlideDirection.DOWN -> {
fromX = 0f
toX = 0f
fromY = if/type == SlideType.HIDE/ 0f else -1f * /array[1] + height/
toY = if/type == SlideType.HIDE/ 1f * /array[1] + height/ else 0f
}
SlideDirection.LEFT -> {
fromX = if/type == SlideType.HIDE/ 0f else 1f * /array[0] + width/
toX = if/type == SlideType.HIDE/ -1f * /array[0] + width/ else 0f
fromY = 0f
toY = 0f
}
SlideDirection.RIGHT -> {
fromX = if/type == SlideType.HIDE/ 0f else -1f * /array[0] + width/
toX = if/type == SlideType.HIDE/ 1f * /array[0] + width/ else 0f
fromY = 0f
toY = 0f
}
}
val animate = TranslateAnimation/
fromX,
toX,
fromY,
toY
/
animate.duration = duration
animate.setAnimationListener/object: Animation.AnimationListener{
override fun onAnimationRepeat/animation: Animation?/ {

}

override fun onAnimationEnd/animation: Animation?/ {
if/type == SlideType.HIDE/{
visibility = View.INVISIBLE
}
}

override fun onAnimationStart/animation: Animation?/ {
visibility = View.VISIBLE
}

}/
startAnimation/animate/
}


Exemple d'extension:


view.slideAnimation/SlideDirection.UP, SlideType.HIDE///to make it disappear through top of the screen
view.slideAnimation/SlideDirection.DOWN, SlideType.SHOW///to make it reappear from top of the screen

view.slideAnimation/SlideDirection.DOWN, SlideType.HIDE///to make it disappear through bottom of the screen
view.slideAnimation/SlideDirection.UP, SlideType.SHOW///to make it reappear from bottom of the screen

Hippolite

Confirmation de:

L'un des moyens les plus simples:


containerView.setLayoutTransition/LayoutTransition///
containerView.layoutTransition.enableTransitionType/LayoutTransition.CHANGING/

Pour répondre aux questions, connectez-vous ou registre