Vous êtes sur la page 1sur 3

Le Slider

J'ai construit une classe qui drive de LinearLayout pour contenir toutes mes vues et qui s'appelle
Slider . De cette manire,
pour faire glisser le menu, je fais glisser toute l'activit et l'effet est plus saisissant. Mon Slider
possde plusieurs attributs :
boolean isOpen pour retenir l'tat de mon menu (ouvert ou ferm).
RelativeLayout toHide qui est le menu dissimuler ou afficher.
final static int SPEED afin de dfinir la vitesse dsire pour mon animation.
En gros, cette classe ne possde qu'une grosse mthode qui permet d'ouvrir ou de fermer le menu :
Animation.AnimationListener
Class Overview

An animation listener receives notifications from an animation. Notifications indicate animation


related events, such as the end or the repetition of the animation.
Le Listener est une classe susceptible d'couter s'il y a animation, Pour qu'une action soit excute
la suite d'un vnement animation, celle ci y sera autorise par l'instruction:
animation.setAnimationListener (instance_Listener)

Summary

Public Methods
onAnimationEnd(Animation animation)
abstract void
Notifies the end of the animation.
onAnimationRepeat(Animation animation)
abstract void
Notifies the repetition of the animation.
onAnimationStart(Animation animation)
abstract void
Notifies the start of the animation.

package com.example.jconana.notepad;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
/**
* Created by jconana on 06/05/16.
*/
public class Slider extends LinearLayout {
/* Est-ce que le menu est ouvert ? */
protected boolean isOpen = true;
/* Le menu dissimuler */
protected RelativeLayout toHide = null;
/* Vitesse dsire pour l'animation */
protected final static int SPEED = 300;
/* Listener pour l'animation de fermeture du menu, ici, on va dfinir une
classe closeListener qui sera charge d'couter si une animation s'est
produite, particulirement, on s'intresse excuter une action lorsque
l'animation se termine */
Animation.AnimationListener closeListener = new
Animation.AnimationListener() {
public void onAnimationEnd(Animation animation) {
//On dissimule le menu
toHide.setVisibility(View.GONE);
}
public void onAnimationRepeat(Animation animation) {
}
public void onAnimationStart(Animation animation) {
}
};
/* Listener pour l'animation d'ouverture du menu, , ici, on va dfinir une
classe openListener qui sera charge d'couter si une animation s'est produite,
particulirement, on s'intresse excuter une action lorsque l'animation
commence */
Animation.AnimationListener openListener = new Animation.AnimationListener()
{
public void onAnimationEnd(Animation animation) {
}
public void onAnimationRepeat(Animation animation) {
}
public void onAnimationStart(Animation animation) {
//On affiche le menu
toHide.setVisibility(View.VISIBLE);
}
};
/**
* Constructeur utilis pour l'initialisation en Java.
* @param context Le contexte de l'activit.
*/
public Slider(Context context) {
super(context);
}
/**
* Constructeur utilis pour l'initialisation en XML.

* @param context Le contexte de l'activit.


* @param attrs Les attributs dfinis dans le XML.
*/
/**
* Utilise pour ouvrir ou fermer le menu.
* @return true si le menu est dsormais ouvert.
*/
public Slider(Context context, AttributeSet attrs) {
super(context, attrs);
}
/**
* Utilise pour ouvrir ou fermer le menu.
* @return true si le menu est dsormais ouvert.
*/
public boolean toggle() {
//Animation de transition. An animation that controls the position of
an object
TranslateAnimation animation = null;
//On passe de ouvert ferm (ou vice versa)
isOpen = !isOpen;
//Si le menu est dj ouvert
if (isOpen)
{
//Animation de translation du bas vers le haut
animation = new TranslateAnimation(0.0f, 0.0f,
-toHide.getHeight(), 0.0f);
animation.setAnimationListener(openListener);
} else
{
//Sinon, animation de translation du haut vers le bas
animation = new TranslateAnimation(0.0f, 0.0f,
0.0f, -toHide.getHeight());
animation.setAnimationListener(closeListener);
}
//On dtermine la dure de l'animation
animation.setDuration(SPEED);
//On ajoute un effet d'acclration
animation.setInterpolator(new AccelerateInterpolator());
//Enfin, on lance l'animation
startAnimation(animation);
return isOpen;
}
public void setToHide(RelativeLayout toHide) {
this.toHide = toHide;
}
}

Vous aimerez peut-être aussi