Skip to content
This repository has been archived by the owner on Oct 2, 2023. It is now read-only.

Commit

Permalink
Implemented Edge only gesture grabs
Browse files Browse the repository at this point in the history
  • Loading branch information
r0adkll committed Aug 5, 2015
1 parent 44ad099 commit fa3edb7
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 43 deletions.
8 changes: 4 additions & 4 deletions example/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile project(':library')

compile 'com.android.support:appcompat-v7:22.2.0'
compile 'com.android.support:recyclerview-v7:22.2.0'
compile 'com.android.support:cardview-v7:22.2.0'
compile 'com.android.support:appcompat-v7:22.2.1'
compile 'com.android.support:recyclerview-v7:22.2.1'
compile 'com.android.support:cardview-v7:22.2.1'
compile 'com.jakewharton:butterknife:6.1.0'
compile 'com.squareup.picasso:picasso:2.5.2'
compile 'com.github.bumptech.glide:glide:3.6.0'
compile 'com.r0adkll:deadskunk:1.0.4-SNAPSHOT'
compile 'com.google.code.gson:gson:2.3.1'
}
14 changes: 7 additions & 7 deletions example/example.iml
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/dependency-cache" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/dex" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/dex-cache" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.android.support/appcompat-v7/22.2.0/jars" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.android.support/cardview-v7/22.2.0/jars" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.android.support/recyclerview-v7/22.2.0/jars" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.android.support/appcompat-v7/22.2.1/jars" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.android.support/cardview-v7/22.2.1/jars" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.android.support/recyclerview-v7/22.2.1/jars" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.r0adkll/deadskunk/1.0.4-SNAPSHOT/jars" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/incremental" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/jacoco" />
Expand All @@ -95,12 +95,12 @@
<orderEntry type="library" exported="" name="gson-2.3.1" level="project" />
<orderEntry type="library" exported="" name="timber-3.1.1-SNAPSHOT" level="project" />
<orderEntry type="library" exported="" name="deadskunk-1.0.4-SNAPSHOT" level="project" />
<orderEntry type="library" exported="" name="glide-3.6.0" level="project" />
<orderEntry type="library" exported="" name="support-v4-22.2.1" level="project" />
<orderEntry type="library" exported="" name="recyclerview-v7-22.2.0" level="project" />
<orderEntry type="library" exported="" name="cardview-v7-22.2.0" level="project" />
<orderEntry type="library" exported="" name="recyclerview-v7-22.2.1" level="project" />
<orderEntry type="library" exported="" name="cardview-v7-22.2.1" level="project" />
<orderEntry type="library" exported="" name="support-annotations-22.2.1" level="project" />
<orderEntry type="library" exported="" name="picasso-2.5.2" level="project" />
<orderEntry type="library" exported="" name="appcompat-v7-22.2.0" level="project" />
<orderEntry type="library" exported="" name="appcompat-v7-22.2.1" level="project" />
<orderEntry type="library" exported="" name="butterknife-6.1.0" level="project" />
<orderEntry type="module" module-name="library" exported="" />
</component>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
import android.view.MenuItem;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.r0adkll.deadskunk.utils.Utils;
import com.r0adkll.deadskunk.views.AspectRatioImageView;
import com.r0adkll.slidr.Slidr;
import com.r0adkll.slidr.example.model.AndroidOS;
import com.r0adkll.slidr.model.SlidrConfig;
import com.r0adkll.slidr.model.SlidrPosition;
import com.squareup.picasso.Picasso;

import butterknife.ButterKnife;
import butterknife.InjectView;
Expand All @@ -39,6 +39,8 @@ public class ViewerActivity extends AppCompatActivity {
TextView mVersion;
@InjectView(R.id.sdk)
TextView mSdk;
@InjectView(R.id.position)
TextView mPosition;

private AndroidOS mOS;

Expand All @@ -50,16 +52,17 @@ protected void onCreate(Bundle savedInstanceState) {

// Get the status bar colors to interpolate between
int primary = getResources().getColor(R.color.primaryDark);
int secondary = getResources().getColor(R.color.accent);
int secondary = getResources().getColor(R.color.transparent);

// Build the slidr config
int numPositions = SlidrPosition.values().length;
SlidrPosition position = SlidrPosition.values()[Utils.getRandom().nextInt(numPositions)];
mPosition.setText(position.name());

SlidrConfig config = new SlidrConfig.Builder()
.primaryColor(primary)
.secondaryColor(secondary)
.position(SlidrPosition.LEFT)
.position(position)
.velocityThreshold(2400)
.distanceThreshold(.25f)
.edge(true)
Expand All @@ -83,8 +86,9 @@ protected void onCreate(Bundle savedInstanceState) {
mSdk.setText(String.valueOf(mOS.sdk_int));

// Load header image
Picasso.with(this)
Glide.with(this)
.load(mOS.image_url)
.crossFade()
.into(mCover);
}

Expand Down
15 changes: 14 additions & 1 deletion example/src/main/res/layout/activity_viewer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,25 @@

</RelativeLayout>

<TextView
android:id="@+id/position"
android:layout_width="match_parent"
android:layout_height="48dp"
android:gravity="center_vertical"
android:layout_marginLeft="16dp"

android:fontFamily="sans-serif-medium"
tools:text="Left"
android:textSize="16sp"
android:textColor="@color/black55"

/>

<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
app:cardElevation="4dp"
app:cardCornerRadius="2dp"
Expand Down
6 changes: 5 additions & 1 deletion example/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

<style name="Theme.Slidr.Slidable" parent="Theme.AppCompat.NoActionBar">
<item name="colorPrimary">@color/primary</item>
<item name="colorPrimaryDark">@color/accent</item>
<item name="colorPrimaryDark">@color/primaryDark</item>
<item name="android:colorAccent">@color/accent</item>
<item name="android:textColorPrimary">@color/textPrimary</item>
<item name="android:textColorSecondary">@color/textSecondary</item>
Expand All @@ -21,6 +21,10 @@
<!-- These values must be set for this mechanism to work -->
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">@android:color/transparent</item>

<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">@android:color/transparent</item>

</style>

<style name="ActionBarThemeOverlay" parent="">
Expand Down
59 changes: 34 additions & 25 deletions library/src/main/java/com/r0adkll/slidr/widget/SliderPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,15 @@ public class SliderPanel extends FrameLayout {

private int mScreenWidth;
private int mScreenHeight;

private View mDimView;
private View mDecorView;
private ViewDragHelper mDragHelper;
private OnPanelSlideListener mListener;

private boolean mIsLocked = false;
private boolean mIsEdgeGrabbed = false;
private boolean mIsEdgeTouched = false;
private int mEdgePosition;

private SlidrConfig mConfig;

Expand Down Expand Up @@ -90,40 +93,39 @@ private void init(){
final float minVel = MIN_FLING_VELOCITY * density;

ViewDragHelper.Callback callback;
int position;
switch (mConfig.getPosition()){
case LEFT:
callback = mLeftCallback;
position = ViewDragHelper.EDGE_LEFT;
mEdgePosition = ViewDragHelper.EDGE_LEFT;
break;
case RIGHT:
callback = mRightCallback;
position = ViewDragHelper.EDGE_RIGHT;
mEdgePosition = ViewDragHelper.EDGE_RIGHT;
break;
case TOP:
callback = mTopCallback;
position = ViewDragHelper.EDGE_TOP;
mEdgePosition = ViewDragHelper.EDGE_TOP;
break;
case BOTTOM:
callback = mBottomCallback;
position = ViewDragHelper.EDGE_BOTTOM;
mEdgePosition = ViewDragHelper.EDGE_BOTTOM;
break;
case VERTICAL:
callback = mVerticalCallback;
position = ViewDragHelper.EDGE_TOP | ViewDragHelper.EDGE_BOTTOM;
mEdgePosition = ViewDragHelper.EDGE_TOP | ViewDragHelper.EDGE_BOTTOM;
break;
case HORIZONTAL:
callback = mHorizontalCallback;
position = ViewDragHelper.EDGE_LEFT | ViewDragHelper.EDGE_RIGHT;
mEdgePosition = ViewDragHelper.EDGE_LEFT | ViewDragHelper.EDGE_RIGHT;
break;
default:
callback = mLeftCallback;
position = ViewDragHelper.EDGE_LEFT;
mEdgePosition = ViewDragHelper.EDGE_LEFT;
}

mDragHelper = ViewDragHelper.create(this, mConfig.getSensitivity(), callback);
mDragHelper.setMinVelocity(minVel);
mDragHelper.setEdgeTrackingEnabled(position);
mDragHelper.setEdgeTrackingEnabled(mEdgePosition);

ViewGroupCompat.setMotionEventSplittingEnabled(this, false);

Expand Down Expand Up @@ -158,11 +160,13 @@ public void run() {
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
boolean interceptForDrag;
boolean interceptWithEdge = true;

if(mConfig.isEdgeOnly()){
interceptWithEdge = canDragFromEdge(ev);
mIsEdgeGrabbed = interceptWithEdge;
if(mIsLocked){
return false;
}

if(mConfig.isEdgeOnly()) {
mIsEdgeTouched = canDragFromEdge(ev);
}

// Fix for pull request #13 and issue #12
Expand All @@ -172,13 +176,15 @@ public boolean onInterceptTouchEvent(MotionEvent ev) {
interceptForDrag = false;
}

boolean value = interceptForDrag && interceptWithEdge && !mIsLocked;
Log.d("SP", "onInterceptTouchEvent() -> " + value);
return value;
return interceptForDrag && !mIsLocked;
}

@Override
public boolean onTouchEvent(MotionEvent event) {
if(mIsLocked){
return false;
}

try {
mDragHelper.processTouchEvent(event);
}catch (IllegalArgumentException e){
Expand Down Expand Up @@ -213,11 +219,11 @@ public void unlock(){
mIsLocked = false;
}

private boolean canDragFromEdge(MotionEvent ev){
private boolean canDragFromEdge(MotionEvent ev) {
float x = ev.getX();
float y = ev.getY();

switch (mConfig.getPosition()){
switch (mConfig.getPosition()) {
case LEFT:
return x < mConfig.getEdgeSize(getWidth());
case RIGHT:
Expand Down Expand Up @@ -248,7 +254,8 @@ private boolean canDragFromEdge(MotionEvent ev){

@Override
public boolean tryCaptureView(View child, int pointerId) {
return child.getId() == mDecorView.getId() && !mIsLocked && (mConfig.isEdgeOnly() ? mIsEdgeGrabbed : true);
boolean edgeCase = !mConfig.isEdgeOnly() || mDragHelper.isEdgeTouched(mEdgePosition, pointerId);
return child.getId() == mDecorView.getId() && edgeCase;
}

@Override
Expand Down Expand Up @@ -330,7 +337,8 @@ public void onViewDragStateChanged(int state) {
private ViewDragHelper.Callback mRightCallback = new ViewDragHelper.Callback() {
@Override
public boolean tryCaptureView(View child, int pointerId) {
return child.getId() == mDecorView.getId() && !mIsLocked && (mConfig.isEdgeOnly() ? mIsEdgeGrabbed : true);
boolean edgeCase = mConfig.isEdgeOnly() ? mDragHelper.isEdgeTouched(mEdgePosition, pointerId) : true;
return child.getId() == mDecorView.getId() && edgeCase;
}

@Override
Expand Down Expand Up @@ -411,7 +419,7 @@ public void onViewDragStateChanged(int state) {
private ViewDragHelper.Callback mTopCallback = new ViewDragHelper.Callback() {
@Override
public boolean tryCaptureView(View child, int pointerId) {
return child.getId() == mDecorView.getId() && !mIsLocked && (mConfig.isEdgeOnly() ? mIsEdgeGrabbed : true);
return child.getId() == mDecorView.getId() && (!mConfig.isEdgeOnly() || mIsEdgeTouched);
}

@Override
Expand Down Expand Up @@ -490,7 +498,7 @@ public void onViewDragStateChanged(int state) {
private ViewDragHelper.Callback mBottomCallback = new ViewDragHelper.Callback() {
@Override
public boolean tryCaptureView(View child, int pointerId) {
return child.getId() == mDecorView.getId() && !mIsLocked && (mConfig.isEdgeOnly() ? mIsEdgeGrabbed : true);
return child.getId() == mDecorView.getId() && (!mConfig.isEdgeOnly() || mIsEdgeTouched);
}

@Override
Expand Down Expand Up @@ -569,7 +577,7 @@ public void onViewDragStateChanged(int state) {
private ViewDragHelper.Callback mVerticalCallback = new ViewDragHelper.Callback() {
@Override
public boolean tryCaptureView(View child, int pointerId) {
return child.getId() == mDecorView.getId() && !mIsLocked && (mConfig.isEdgeOnly() ? mIsEdgeGrabbed : true);
return child.getId() == mDecorView.getId() && (!mConfig.isEdgeOnly() || mIsEdgeTouched);
}

@Override
Expand Down Expand Up @@ -663,7 +671,8 @@ public void onViewDragStateChanged(int state) {
private ViewDragHelper.Callback mHorizontalCallback = new ViewDragHelper.Callback() {
@Override
public boolean tryCaptureView(View child, int pointerId) {
return child.getId() == mDecorView.getId() && !mIsLocked && (mConfig.isEdgeOnly() ? mIsEdgeGrabbed : true);
boolean edgeCase = !mConfig.isEdgeOnly() || mDragHelper.isEdgeTouched(mEdgePosition, pointerId);
return child.getId() == mDecorView.getId() && edgeCase;
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion local.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
# Location of the SDK. This is only used by Gradle.
# For customization when using a Version Control System, please read the
# header note.
#Tue Aug 04 17:20:04 EDT 2015
#Wed Aug 05 10:05:59 EDT 2015
sdk.dir=/Users/drew.heavner/android-sdk

0 comments on commit fa3edb7

Please sign in to comment.