Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. /**
  2.  * @author vivek
  3.  */
  4. //=========================================================
  5. public class CategoriesSlideView extends LinearLayout implements View.OnTouchListener{
  6.  
  7.     private final static int Offset = 30, MaxAlpha = 180;
  8.     private float startX = 0;
  9.     private int dwidth = 0;
  10.     private View overlay;
  11.    
  12.     //=================================
  13.     public CategoriesSlideView(Context context, AttributeSet attrs) {
  14.         super(context, attrs);
  15.     }
  16.  
  17.     //=================================
  18.     @Override
  19.     public void onAttachedToWindow(){
  20.         super.onAttachedToWindow();
  21.         if(android.os.Build.VERSION.SDK_INT>=android.os.Build.VERSION_CODES.LOLLIPOP)
  22.             setElevation(20f);
  23.         setOnTouchListener(this);
  24.         animate().alpha(0).setDuration(0).start();
  25.         startX = getX();
  26.         postDelayed(new Runnable(){public void run(){
  27.            
  28.             WindowManager mgr = (WindowManager)getContext().getSystemService(Context.WINDOW_SERVICE);
  29.             DisplayMetrics dm = new DisplayMetrics();
  30.             mgr.getDefaultDisplay().getMetrics(dm);
  31.             dwidth = dm.widthPixels;
  32.            
  33.             View parent = (View)getParent();
  34.             ValueAnimator anim = ObjectAnimator.ofFloat(CategoriesSlideView.this, "translationX", 0, parent.getX()+parent.getWidth()-Offset);
  35.             anim.setDuration(0);
  36.             anim.start();
  37.         }}, 400);
  38.     }
  39.    
  40.     //=================================
  41.     private void openIt(){
  42.         animate().alpha(1).setDuration(0).start();
  43.         ValueAnimator anim = ObjectAnimator.ofFloat(CategoriesSlideView.this, "translationX", getX(), dwidth-getWidth());
  44.         anim.setDuration(200);
  45.         anim.start();
  46.     }
  47.    
  48.     //=================================
  49.     public void openDirect(){
  50.         openIt();
  51.         addOverlay();
  52.         overlay.getBackground().setAlpha(MaxAlpha);
  53.     }
  54.    
  55.     //=================================
  56.     public void closeDirect(){
  57.         closeIt();
  58.     }  
  59.    
  60.     //=================================
  61.     private void closeIt(){
  62.         ValueAnimator anim = ObjectAnimator.ofFloat(CategoriesSlideView.this, "translationX", getX(), dwidth-Offset);
  63.         anim.setDuration(200);
  64.         anim.start();
  65.         animate().alpha(0).setDuration(300).start();
  66.         overlay.setVisibility(View.GONE);
  67.     }
  68.    
  69.     //=================================
  70.     private void addOverlay(){
  71.         if(overlay!=null && overlay.getVisibility()==View.VISIBLE) return;
  72.         ViewGroup parent = (ViewGroup)getParent();
  73.         overlay = new View(getContext());
  74.         overlay.setBackgroundColor(Color.BLACK);
  75.         overlay.getBackground().setAlpha(10);
  76.         parent.addView(overlay);
  77.         overlay.setOnClickListener(new View.OnClickListener() {
  78.             public void onClick(View v) {
  79.                 closeIt();
  80.             }
  81.         });
  82.         bringToFront();
  83.     }
  84.    
  85.     //=================================
  86.     @Override
  87.     public boolean onInterceptTouchEvent(MotionEvent event){   
  88.         return false;
  89.     }
  90.    
  91.     //=================================
  92.     @Override
  93.     public boolean onTouch(View v, MotionEvent event) {
  94.         switch(event.getAction()){
  95.             case MotionEvent.ACTION_DOWN:
  96.                 addOverlay();
  97.                 animate().alpha(1).start();
  98.                 break;
  99.             case MotionEvent.ACTION_UP:
  100.                 if((dwidth-event.getRawX())>((getWidth()*2)/3)){
  101.                     openIt();
  102.                 }else{
  103.                     closeIt();
  104.                 }              
  105.                 break;
  106.             default:
  107.                 if((dwidth-event.getRawX())>(startX+getWidth()))
  108.                     return true;               
  109.                 v.setX(event.getRawX());
  110.                 overlay.getBackground().setAlpha((int)((1-(event.getRawX()/dwidth))*MaxAlpha));
  111.                 break;
  112.         }  
  113.         return true;
  114.     }
  115. }