Skip to content

Commit

Permalink
左右布局,跟随手指转动,松开手指判定后续转动,快速滑动时自动转
Browse files Browse the repository at this point in the history
  • Loading branch information
gumingwei committed Feb 29, 2016
1 parent a73bd80 commit 8192fd4
Show file tree
Hide file tree
Showing 9 changed files with 237 additions and 96 deletions.
2 changes: 1 addition & 1 deletion .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<activity android:name=".activitys.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

Expand Down
14 changes: 14 additions & 0 deletions app/src/main/java/com/well/swipe/SwipeApplication.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.well.swipe;

import android.app.Application;

/**
* Created by mingwei on 2/29/16.
*/
public class SwipeApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
}

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.well.swipe;
package com.well.swipe.activitys;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.well.swipe.R;

public class MainActivity extends AppCompatActivity {

@Override
Expand Down
94 changes: 50 additions & 44 deletions app/src/main/java/com/well/swipe/view/DragLayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.ViewConfiguration;
import android.widget.FrameLayout;

Expand All @@ -20,14 +21,6 @@ public class DragLayer extends FrameLayout {
* 旋转View
*/
private FanMum mFanMum;
/**
* fan的旋转角度
*/
private int mFanCurRotation = 0;
/**
* 按下时相对底部的角度
*/
private double mLastRotation;
/**
* 当前的旋转状态
*/
Expand All @@ -49,14 +42,6 @@ public class DragLayer extends FrameLayout {
*/
private static final int TOUCH_STATE_NEXT = 3;

/**
* 顺时针/逆时针
*/
private int mWhirling = ALONG;

private static final int ALONG = 0;

private static final int INVERSE = 1;

private float mDownMotionX;

Expand All @@ -70,6 +55,11 @@ public class DragLayer extends FrameLayout {
*/
private int mTouchSlop;

private VelocityTracker mVelocityTracker;

private int mMaximumVelocity, mMinmumVelocity;

private static final int ALLOW_AUTO = 3000;
/**
* 容器的宽高
*/
Expand All @@ -90,6 +80,8 @@ public DragLayer(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
ViewConfiguration mConfig = ViewConfiguration.get(context);
mTouchSlop = mConfig.getScaledTouchSlop();
mMaximumVelocity = ViewConfiguration.get(context).getScaledMaximumFlingVelocity();
mMinmumVelocity = ViewConfiguration.get(context).getScaledMinimumFlingVelocity();
}

@Override
Expand Down Expand Up @@ -122,8 +114,6 @@ protected void onLayout(boolean changed, int left, int top, int right, int botto
} else if (mFanMum.POSITION == FanMum.RIGHT) {
mFanMum.layout(offset, mHeight - fanSize, mWidth, mHeight);
}


}


Expand All @@ -133,6 +123,8 @@ public boolean onTouchEvent(MotionEvent event) {
if (getChildCount() <= 0) {
return super.onTouchEvent(event);
}

initVeloCityTracker(event);
final int action = event.getAction();

switch (action & MotionEvent.ACTION_MASK) {
Expand All @@ -144,9 +136,12 @@ public boolean onTouchEvent(MotionEvent event) {
mActivePointId = event.getPointerId(0);
if (mTouchState == TOUCH_STATE_WHIRLING) {
//正在滚动的时候

}
mLastRotation = Math.toDegrees(Math.atan((mLastMotionX) / ((mHeight - mLastMotionY))));
if (mFanMum.POSITION == FanMum.LEFT) {
mFanMum.downAngle(mLastMotionX, mHeight - mLastMotionY);
} else {
mFanMum.downAngle(mWidth - mLastMotionX, mHeight - mLastMotionY);
}
break;
case MotionEvent.ACTION_MOVE:

Expand All @@ -159,43 +154,54 @@ public boolean onTouchEvent(MotionEvent event) {
}

if (mTouchState == TOUCH_STATE_WHIRLING) {
double rotation = Math.toDegrees(Math.atan(newX / (mHeight - newY)));
double diffrotation = rotation - mLastRotation;
Log.i("Gmw", "diffrotation=" + diffrotation);
if (diffrotation > 0) {
mWhirling = ALONG;
if (mFanMum.POSITION == FanMum.LEFT) {
mFanMum.changeAngle(newX, mHeight - newY);
} else {
mWhirling = INVERSE;
mFanMum.changeAngle(mWidth - newX, mHeight - newY);
}
mFanMum.refresh(diffrotation);
}

break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:

if (mWhirling == ALONG) {
if (mFanMum.getRitation() % FanMum.UNIT_DEGREES > 0 && mFanMum.getRitation() %
FanMum.UNIT_DEGREES < FanMum.OFFSET_DEGREES) {
Log.i("Gmw", "顺时针_返回当前");
} else {
Log.i("Gmw", "顺时针_下一个");
mFanMum.upAngle();
mTouchState = TOUCH_STATE_REST;
mVelocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
float vy = mVelocityTracker.getYVelocity();
float vx = mVelocityTracker.getXVelocity();
if (mFanMum.POSITION == FanMum.LEFT) {
if (vy > ALLOW_AUTO || vx > ALLOW_AUTO) {
mFanMum.flingAlong();
} else if (vx < -ALLOW_AUTO || vy < -ALLOW_AUTO) {
mFanMum.flingInvrse();
}
} else {
if (mFanMum.getRitation() % FanMum.UNIT_DEGREES > (FanMum.UNIT_DEGREES - FanMum.OFFSET_DEGREES)) {
Log.i("Gmw", "逆时针_返回当前");
} else {
Log.i("Gmw", "逆时针_上一个");
if (vx > ALLOW_AUTO || vy < -ALLOW_AUTO) {
mFanMum.flingAlong();
} else if (vx < -ALLOW_AUTO || vy > ALLOW_AUTO) {
mFanMum.flingInvrse();
}
}
if (mFanMum.getRitation() % FanMum.UNIT_DEGREES > FanMum.OFFSET_DEGREES) {
Log.i("Gmw", "touch_ACTION_UP=" + mFanMum.getRitation() % FanMum.UNIT_DEGREES);
}
mTouchState = TOUCH_STATE_REST;

recyleVelocityTracker();
break;
case MotionEvent.ACTION_CANCEL:
recyleVelocityTracker();
break;
}

return true;
}

private void initVeloCityTracker(MotionEvent event) {
if (mVelocityTracker == null) {
mVelocityTracker = VelocityTracker.obtain();
}
mVelocityTracker.addMovement(event);
}

private void recyleVelocityTracker() {
if (mVelocityTracker != null) {
mVelocityTracker.recycle();
mVelocityTracker = null;
}
}
}
Loading

0 comments on commit 8192fd4

Please sign in to comment.