Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.23 KB | None | 0 0
  1. package com.sixney.app.view;
  2.  
  3. import android.animation.AnimatorSet;
  4. import android.animation.IntEvaluator;
  5. import android.animation.ValueAnimator;
  6. import android.annotation.SuppressLint;
  7. import android.content.Context;
  8. import android.content.res.TypedArray;
  9. import android.util.AttributeSet;
  10. import android.view.LayoutInflater;
  11. import android.view.View;
  12. import android.view.ViewGroup;
  13. import android.view.ViewTreeObserver;
  14. import android.view.animation.AccelerateInterpolator;
  15. import android.widget.LinearLayout;
  16. import android.widget.TextView;
  17.  
  18. import com.sixney.app.R;
  19.  
  20. /**
  21.  * Created by florentchampigny on 29/04/2014.
  22.  */
  23. public class DoubleProgress extends LinearLayout {
  24.  
  25.     private static final String TAG = "DoubleProgress";
  26.  
  27.     View mView;
  28.  
  29.     String stringHaut = "Haut";
  30.     String stringBas = "Bas";
  31.     float max;
  32.     int temps;
  33.     int attente;
  34.     int textFromAlpha = 40;
  35.     int textToAlpha = 100;
  36.  
  37.     View barreHaut, barreBas;
  38.     View progressBarHaut, progressBarBas;
  39.     TextView valeurHaut, valeurBas;
  40.     TextView texteHaut, texteBas;
  41.  
  42.     float vHaut = 0;
  43.     float vBas = 0;
  44.  
  45.     public DoubleProgress(Context context) {
  46.         this(context, null);
  47.     }
  48.  
  49.     public DoubleProgress(Context context, AttributeSet attrs) {
  50.         super(context, attrs);
  51.  
  52.         TypedArray styledAttrs = context.obtainStyledAttributes(attrs, R.styleable.DoubleProgress);
  53.         try {
  54.             stringHaut = styledAttrs.getString(R.styleable.DoubleProgress_texteHaut);
  55.             stringBas = styledAttrs.getString(R.styleable.DoubleProgress_texteBas);
  56.             max = styledAttrs.getFloat(R.styleable.DoubleProgress_max, 5);
  57.             temps = styledAttrs.getInt(R.styleable.DoubleProgress_temps, 1000);
  58.             attente = styledAttrs.getInt(R.styleable.DoubleProgress_attente, 0);
  59.             textFromAlpha = styledAttrs.getInt(R.styleable.DoubleProgress_attente, 0);
  60.             textToAlpha = styledAttrs.getInt(R.styleable.DoubleProgress_attente, 0);
  61.         } catch (Exception e) {
  62.             e.printStackTrace();
  63.         } finally {
  64.             styledAttrs.recycle();
  65.         }
  66.  
  67.         LayoutInflater inflater = (LayoutInflater) context
  68.                 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  69.  
  70.         mView = inflater.inflate(R.layout.double_progress, this, true); //ajoute la view
  71.  
  72.         charger();
  73.         remplir();
  74.     }
  75.  
  76.     @Override
  77.     protected void onAttachedToWindow() {
  78.         super.onAttachedToWindow();
  79.     }
  80.  
  81.     @Override
  82.     protected void onDetachedFromWindow() {
  83.         super.onDetachedFromWindow();
  84.     }
  85.  
  86.     private void charger() {
  87.         barreHaut = mView.findViewById(R.id.barreHaut);
  88.         barreBas = mView.findViewById(R.id.barreBas);
  89.  
  90.         progressBarHaut = barreHaut.findViewById(R.id.progressBar);
  91.         progressBarBas = barreBas.findViewById(R.id.progressBar);
  92.  
  93.         valeurHaut = (TextView) barreHaut.findViewById(R.id.valeur);
  94.         valeurBas = (TextView) barreBas.findViewById(R.id.valeur);
  95.  
  96.         texteHaut = (TextView) barreHaut.findViewById(R.id.text);
  97.         texteBas = (TextView) barreBas.findViewById(R.id.text);
  98.     }
  99.  
  100.     private void remplir() {
  101.         texteHaut.setText(stringHaut);
  102.         texteBas.setText(stringBas);
  103.     }
  104.  
  105.     public void setValeurHaut(float f) {
  106.         vHaut = f;
  107.     }
  108.  
  109.     public void setValeurBas(float f) {
  110.         vBas = f;
  111.     }
  112.  
  113.     public void animer() {
  114.  
  115.         /**
  116.          * Attente de la largeur de la vue barreHaut
  117.          */
  118.         barreHaut.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
  119.             @SuppressLint("NewApi")
  120.             @SuppressWarnings("deprecation")
  121.             @Override
  122.             public void onGlobalLayout() {
  123.                 //now we can retrieve the width and height
  124.                 int width = barreHaut.getWidth();
  125.  
  126.                 animer(width);
  127.  
  128.                 if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN)
  129.                     barreHaut.getViewTreeObserver().removeOnGlobalLayoutListener(this);
  130.                 else
  131.                     barreHaut.getViewTreeObserver().removeGlobalOnLayoutListener(this);
  132.             }
  133.         });
  134.     }
  135.  
  136.     private void animer(int width) {
  137.  
  138.         valeurHaut.setText("");
  139.         valeurBas.setText("");
  140.  
  141.         ViewGroup.LayoutParams params = progressBarHaut.getLayoutParams();
  142.         params.width = 0;
  143.         progressBarHaut.setLayoutParams(params);
  144.  
  145.         ViewGroup.LayoutParams params2 = progressBarBas.getLayoutParams();
  146.         params2.width = 0;
  147.         progressBarBas.setLayoutParams(params2);
  148.  
  149.         float largeurMax = barreHaut.getWidth();
  150.         float pourcentHaut = vHaut / max;
  151.         float pourcentBas = vBas / max;
  152.  
  153.         int largeurHaut = (int) (largeurMax * pourcentHaut);
  154.         int largeurtBas = (int) (largeurMax * pourcentBas);
  155.  
  156.         final AnimatorSet anim = new AnimatorSet();
  157.         anim.setInterpolator(new AccelerateInterpolator());
  158.         anim.setDuration((long) temps);
  159.         anim.playTogether(
  160.                 ValueAnimator.ofObject(new WidthEvaluator(progressBarHaut, valeurHaut, vHaut), 0, largeurHaut),
  161.                 ValueAnimator.ofObject(new WidthEvaluator(progressBarBas, valeurBas, vBas), 0, largeurtBas)
  162.         );
  163.  
  164.         this.postDelayed(new Runnable() {
  165.             @Override
  166.             public void run() {
  167.                 anim.start();
  168.             }
  169.         }, attente);
  170.  
  171.     }
  172.  
  173.     private class WidthEvaluator extends IntEvaluator {
  174.  
  175.         private View v;
  176.         private TextView tv;
  177.         private float valeur;
  178.  
  179.         public WidthEvaluator(View v, TextView tv, float valeur) {
  180.             this.v = v;
  181.             this.tv = tv;
  182.             this.valeur = valeur;
  183.         }
  184.  
  185.  
  186.         @Override
  187.         public Integer evaluate(float fraction, Integer startValue, Integer endValue) {
  188.             int num = super.evaluate(fraction, startValue, endValue);
  189.  
  190.             tv.setText(String.format("%.1f", fraction * valeur).replace(",", "."));
  191.  
  192.             ViewGroup.LayoutParams params = v.getLayoutParams();
  193.             params.width = num;
  194.             v.setLayoutParams(params);
  195.  
  196.             return num;
  197.         }
  198.     }
  199.  
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement