Last active
December 25, 2015 08:39
-
-
Save jmartinesp/6948318 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.arasthel.wfspain.views; | |
import android.app.Activity; | |
import android.content.Context; | |
import android.graphics.Canvas; | |
import android.graphics.Color; | |
import android.graphics.Point; | |
import android.util.AttributeSet; | |
import android.util.DisplayMetrics; | |
import android.util.Log; | |
import android.view.Display; | |
import android.view.LayoutInflater; | |
import android.view.MotionEvent; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.ImageView; | |
import android.widget.LinearLayout; | |
import android.widget.RelativeLayout; | |
import com.arasthel.wfspain.R; | |
/** | |
* Created by Arasthel on 12/10/13. | |
*/ | |
public class CustomSlidingDrawer extends RelativeLayout { | |
private ImageView headerImgView; | |
private LinearLayout contentView; | |
private int marginTop = 0; | |
private int oldY = 0; | |
private float screenPercent = (float) 0.4; | |
private boolean touched = false; | |
public CustomSlidingDrawer(Context context) { | |
super(context); | |
createView(); | |
} | |
@Override | |
protected void onSizeChanged(int w, int h, int oldw, int oldh) { | |
super.onSizeChanged(w, h, oldw, oldh); | |
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); | |
marginTop = (int)(h*screenPercent); | |
params.setMargins(0, marginTop, 0, 0); | |
contentView.setLayoutParams(params); | |
headerImgView.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, (int)(h*screenPercent))); | |
} | |
private void createView() { | |
headerImgView = new ImageView(getContext()); | |
headerImgView.setScaleType(ImageView.ScaleType.CENTER_CROP); | |
headerImgView.setImageDrawable(getResources().getDrawable(R.drawable.intro_01)); | |
contentView = new LinearLayout(getContext()); | |
contentView.setBackgroundColor(Color.WHITE); | |
if(!isInEditMode()) { | |
contentView.setOnTouchListener(drawerTouchListener); | |
} | |
addView(headerImgView); | |
addView(contentView); | |
} | |
private OnTouchListener drawerTouchListener = new OnTouchListener() { | |
@Override | |
public boolean onTouch(View v, MotionEvent event) { | |
int newY = (int)event.getRawY(); | |
switch(event.getAction()) { | |
case MotionEvent.ACTION_DOWN: | |
if(newY > marginTop) { | |
touched = true; | |
} | |
break; | |
case MotionEvent.ACTION_UP: | |
touched = false; | |
oldY = 0; | |
break; | |
case MotionEvent.ACTION_CANCEL: | |
touched = false; | |
oldY = 0; | |
break; | |
case MotionEvent.ACTION_MOVE: | |
if(touched) { | |
if(newY != marginTop) { | |
if(oldY != 0) { | |
int deltaY = newY - oldY; | |
int newTop = marginTop + deltaY; | |
if(newTop > 0 && newTop < (int)(screenPercent*getHeight())) { | |
RelativeLayout.LayoutParams params = (LayoutParams) contentView.getLayoutParams(); | |
params.setMargins(0, newTop, 0, 0); | |
contentView.setLayoutParams(params); | |
invalidate(); | |
marginTop = newTop; | |
} else { | |
if(newTop <= 0) { | |
RelativeLayout.LayoutParams params = (LayoutParams) contentView.getLayoutParams(); | |
params.setMargins(0, 0, 0, 0); | |
contentView.setLayoutParams(params); | |
invalidate(); | |
marginTop = 0; | |
} | |
int drawerHeight = (int)(screenPercent*getHeight()); | |
if(newTop >= drawerHeight) { | |
RelativeLayout.LayoutParams params = (LayoutParams) contentView.getLayoutParams(); | |
params.setMargins(0, drawerHeight, 0, 0); | |
contentView.setLayoutParams(params); | |
invalidate(); | |
marginTop = drawerHeight; | |
} | |
} | |
} | |
oldY = newY; | |
} | |
} | |
} | |
return true; | |
} | |
}; | |
public CustomSlidingDrawer(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
createView(); | |
} | |
public CustomSlidingDrawer(Context context, AttributeSet attrs, int defStyle) { | |
super(context, attrs, defStyle); | |
createView(); | |
} | |
public void setImage(int drawableId) { | |
headerImgView.setImageDrawable(getResources().getDrawable(drawableId)); | |
} | |
public void setContentView(int layoutId) { | |
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); | |
View v = inflater.inflate(layoutId, contentView, false); | |
contentView.removeAllViews(); | |
contentView.addView(v); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment