-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVerticalPopupView.java
More file actions
172 lines (154 loc) · 5.25 KB
/
VerticalPopupView.java
File metadata and controls
172 lines (154 loc) · 5.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package com.jocoo.verticalscrollview;
import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Rect;
import android.os.Build;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.view.animation.DecelerateInterpolator;
import android.widget.OverScroller;
public class VerticalPopupView extends ViewGroup {
private static final String TAG = VerticalPopupView.class.getName();
private static final int ANIMATED_SCROLL_GAP = 250;
private ViewGroup mContent;
private ViewGroup mPanel;
private boolean bMeasured = false;
private boolean isOpen = false;
private OverScroller mScroller;
private long mLastScroll;
private int mPanelHeight;
private int mContentHeight;
private int mContentWidth;
private int[] mTempPoint;
public VerticalPopupView(Context context) {
this(context, null);
}
public VerticalPopupView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
private void init() {
mScroller = new OverScroller(getContext(), new DecelerateInterpolator());
setBackgroundColor(Color.BLACK);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if (!bMeasured) {
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
mContentWidth = widthSize;
mContentHeight = heightSize;
setMeasuredDimension(mContentWidth, mContentHeight);
measureChildren(widthMeasureSpec, heightMeasureSpec);
mContent = (ViewGroup) getChildAt(0);
mPanel = (ViewGroup) getChildAt(1);
mPanelHeight = mPanel.getMeasuredHeight();
Log.d(TAG, mPanelHeight + "");
bMeasured = true;
} else {
setMeasuredDimension(mContentWidth, mContentHeight);
}
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
mContent.layout(0, 0, r, mContentHeight);
mPanel.layout(0, mContentHeight, r, mContentHeight + mPanelHeight);
scrollTo(0, 0);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (!isOpen) return false;
int x = (int) event.getX();
int y = (int) event.getY();
if (event.getAction() == MotionEvent.ACTION_DOWN) {
return true;
} else if (event.getAction() == MotionEvent.ACTION_UP
&& isTransformedPointInView(x, y, mContent)
&& !isTransformedPointInView(x, y, mPanel)) {
toggle(false);
return true;
}
return super.onTouchEvent(event);
}
private boolean isTransformedPointInView(int x, int y, View child) {
final int[] point = getTempPoint();
point[0] = x;
point[1] = y;
transformPointToViewLocal(child, point);
Rect rect = new Rect();
child.getLocalVisibleRect(rect);
return rect.contains(point[0], point[1]);
}
private void transformPointToViewLocal(View child, int[] point) {
point[0] += getScrollX() - child.getLeft();
point[1] += getScrollY() - child.getTop();
}
private int[] getTempPoint() {
if (mTempPoint == null) {
mTempPoint = new int[2];
}
return mTempPoint;
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public final void smoothScrollBy(int dx, int dy) {
if (getChildCount() == 0) {
// Nothing to do.
return;
}
long duration = AnimationUtils.currentAnimationTimeMillis() - mLastScroll;
int mScrollX = getScrollX();
int mScrollY = getScrollY();
if (duration > ANIMATED_SCROLL_GAP) {
int mPaddingBottom = getPaddingBottom();
int mPaddingTop = getPaddingTop();
final int height = getHeight() - mPaddingBottom - mPaddingTop;
final int bottom = getChildAt(0).getHeight() + mPanelHeight;
final int maxY = Math.max(0, bottom - height);
final int scrollY = mScrollY;
dy = Math.max(0, Math.min(scrollY + dy, maxY)) - scrollY;
mScroller.startScroll(mScrollX, scrollY, 0, dy, 300);
postInvalidate();
} else if (duration > 100) {
if (!mScroller.isFinished()) {
mScroller.abortAnimation();
}
mScroller.startScroll(dx, mScrollY, 0, dy, 100);
}
mLastScroll = AnimationUtils.currentAnimationTimeMillis();
}
public final void smoothScrollTo(int x, int y) {
int mScrollX = getScrollX();
int mScrollY = getScrollY();
smoothScrollBy(x - mScrollX, y - mScrollY);
}
@Override
public void computeScroll() {
if (mScroller.computeScrollOffset()) {
scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
postInvalidate();
}
}
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
super.onScrollChanged(l, t, oldl, oldt);
mContent.setTranslationY(t * 1.0f);
float scale = t * 1.0f / mPanelHeight;
mContent.setAlpha(1 - 0.2f * scale);
}
public void toggle(boolean open) {
if (!open) {
smoothScrollTo(0, 0);
} else {
smoothScrollTo(0, mPanelHeight);
}
isOpen = !isOpen;
}
public void toggle() {
toggle(!isOpen);
}
}