Skip to content

Commit

Permalink
Merge branch 'master' into support
Browse files Browse the repository at this point in the history
  • Loading branch information
mbacuiz committed Dec 3, 2019
2 parents d48754a + abf10d8 commit f331d92
Show file tree
Hide file tree
Showing 9 changed files with 173 additions and 38 deletions.
4 changes: 4 additions & 0 deletions anylayer/src/main/java/per/goweii/anylayer/AnyLayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ public static DialogLayer dialog(Context context) {
return new DialogLayer(context);
}

public static PopupLayer popup(Context context) {
return new PopupLayer(context);
}

public static PopupLayer popup(View targetView) {
return new PopupLayer(targetView);
}
Expand Down
60 changes: 60 additions & 0 deletions anylayer/src/main/java/per/goweii/anylayer/ContainerLayout.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package per.goweii.anylayer;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.FrameLayout;

/**
* Create by cuizhen on {2019/12/3}
* <p>
* ▄████▄ █ ██ ██▓▒███████▒ ██░ ██ ▓█████ ███▄ █
* ▒██▀ ▀█ ██ ▓██▒▓██▒▒ ▒ ▒ ▄▀░▓██░ ██▒▓█ ▀ ██ ▀█ █
* ▒▓█ ▄ ▓██ ▒██░▒██▒░ ▒ ▄▀▒░ ▒██▀▀██░▒███ ▓██ ▀█ ██▒
* ▒▓▓▄ ▄██▒▓▓█ ░██░░██░ ▄▀▒ ░░▓█ ░██ ▒▓█ ▄▓██▒ ▐▌██▒
* ▒ ▓███▀ ░▒▒█████▓ ░██░▒███████▒░▓█▒░██▓░▒████▒██░ ▓██░
* ░ ░▒ ▒ ░░▒▓▒ ▒ ▒ ░▓ ░▒▒ ▓░▒░▒ ▒ ░░▒░▒░░ ▒░ ░ ▒░ ▒ ▒
* ░ ▒ ░░▒░ ░ ░ ▒ ░░░▒ ▒ ░ ▒ ▒ ░▒░ ░ ░ ░ ░ ░░ ░ ▒░
* ░ ░░░ ░ ░ ▒ ░░ ░ ░ ░ ░ ░ ░░ ░ ░ ░ ░ ░
* ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░
* ░ ░
*/
public class ContainerLayout extends FrameLayout {

private OnTouchedListener mOnTouchedListener = null;

public ContainerLayout(Context context) {
this(context, null);
}

public ContainerLayout(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}

public ContainerLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
if (mOnTouchedListener != null) {
mOnTouchedListener.onTouched();
}
break;
default:
break;
}
return super.dispatchTouchEvent(ev);
}

public void setOnTouchedListener(OnTouchedListener onTouchedListener) {
this.mOnTouchedListener = onTouchedListener;
}

public interface OnTouchedListener {
void onTouched();
}
}
3 changes: 3 additions & 0 deletions anylayer/src/main/java/per/goweii/anylayer/DecorLayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import android.content.ComponentCallbacks;
import android.content.Context;
import android.content.res.Configuration;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
Expand Down
24 changes: 21 additions & 3 deletions anylayer/src/main/java/per/goweii/anylayer/DialogLayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public Config getConfig() {

@Override
protected View onCreateChild(LayoutInflater inflater, ViewGroup parent) {
FrameLayout container = (FrameLayout) inflater.inflate(R.layout.anylayer_dialog_layer, parent, false);
ContainerLayout container = (ContainerLayout) inflater.inflate(R.layout.anylayer_dialog_layer, parent, false);
getViewHolder().setChild(container);
getViewHolder().setContent(onCreateContent(inflater, getViewHolder().getContentWrapper()));
getViewHolder().getContentWrapper().addView(getViewHolder().getContent());
Expand Down Expand Up @@ -177,6 +177,14 @@ public void onClick(View v) {
getViewHolder().getChild().setOnClickListener(null);
getViewHolder().getChild().setClickable(false);
}
getViewHolder().getChild().setOnTouchedListener(new ContainerLayout.OnTouchedListener() {
@Override
public void onTouched() {
if (getConfig().mOutsideTouchedListener != null) {
getConfig().mOutsideTouchedListener.outsideTouched();
}
}
});
fitContainerToActivityContent();
FrameLayout.LayoutParams contentWrapperParams = (FrameLayout.LayoutParams) getViewHolder().getContentWrapper().getLayoutParams();
contentWrapperParams.width = FrameLayout.LayoutParams.MATCH_PARENT;
Expand Down Expand Up @@ -587,6 +595,11 @@ public DialogLayer outsideInterceptTouchEvent(boolean intercept) {
return this;
}

public DialogLayer outsideTouched(OutsideTouchedListener listener) {
getConfig().mOutsideTouchedListener = listener;
return this;
}

public static class ViewHolder extends DecorLayer.ViewHolder {
private FrameLayout mActivityContent;
private ImageView mBackground;
Expand Down Expand Up @@ -616,8 +629,8 @@ public void setChild(View child) {
}

@Override
public FrameLayout getChild() {
return (FrameLayout) super.getChild();
public ContainerLayout getChild() {
return (ContainerLayout) super.getChild();
}

void setContent(View content) {
Expand All @@ -639,6 +652,7 @@ public ImageView getBackground() {

protected static class Config extends DecorLayer.Config {
protected boolean mOutsideInterceptTouchEvent = true;
protected OutsideTouchedListener mOutsideTouchedListener = null;

protected AnimatorCreator mBackgroundAnimatorCreator = null;
protected AnimatorCreator mContentAnimatorCreator = null;
Expand Down Expand Up @@ -667,6 +681,10 @@ protected static class Config extends DecorLayer.Config {
protected static class ListenerHolder extends DecorLayer.ListenerHolder {
}

public interface OutsideTouchedListener {
void outsideTouched();
}

public interface DragTransformer {
void onDragging(View content, View background, float f);
}
Expand Down
105 changes: 74 additions & 31 deletions anylayer/src/main/java/per/goweii/anylayer/PopupLayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.content.Context;
import android.content.res.Configuration;
import android.util.TypedValue;
import android.view.LayoutInflater;
Expand All @@ -21,6 +22,10 @@ public class PopupLayer extends DialogLayer {

private ViewTreeObserver.OnScrollChangedListener mOnScrollChangedListener;

public PopupLayer(Context context) {
super(Utils.requireNonNull(context, "context == null"));
}

public PopupLayer(View targetView) {
super(Utils.requireNonNull(targetView, "targetView == null").getContext());
getViewHolder().setTarget(targetView);
Expand Down Expand Up @@ -128,7 +133,7 @@ public void onConfigurationChanged(Configuration newConfig) {
Utils.getViewSize(getViewHolder().getBackground(), new Runnable() {
@Override
public void run() {
initLocation();
updateLocation();
}
});
}
Expand All @@ -155,42 +160,27 @@ protected void initContainer() {
Utils.getViewSize(getViewHolder().getChild(), new Runnable() {
@Override
public void run() {
initLocation();
updateLocation();
}
});
if (!getConfig().mOutsideInterceptTouchEvent) {
mOnScrollChangedListener = new ViewTreeObserver.OnScrollChangedListener() {
@Override
public void onScrollChanged() {
initLocation();
updateLocation();
}
};
getViewHolder().getParent().getViewTreeObserver().addOnScrollChangedListener(mOnScrollChangedListener);
}
}

private void initLocation() {
final int[] locationTarget = new int[2];
getViewHolder().getTarget().getLocationOnScreen(locationTarget);
final int[] locationRoot = new int[2];
getViewHolder().getDecor().getLocationOnScreen(locationRoot);
final int targetX = (locationTarget[0] - locationRoot[0]);
final int targetY = (locationTarget[1] - locationRoot[1]);
final int targetWidth = getViewHolder().getTarget().getWidth();
final int targetHeight = getViewHolder().getTarget().getHeight();
initContentWrapperLocation(targetX, targetY, targetWidth, targetHeight);
if (getConfig().mBackgroundAlign) {
initBackgroundLocation(targetX, targetY, targetWidth, targetHeight);
}
}

private void initContentWrapperLocation(int targetX, int targetY, int targetWidth, int targetHeight) {
final int[] lp = new int[2];
getViewHolder().getChild().getLocationOnScreen(lp);
int parentX = lp[0];
int parentY = lp[1];
int parentW = getViewHolder().getChild().getWidth();
int parentH = getViewHolder().getChild().getHeight();
int parentWidth = getViewHolder().getChild().getWidth();
int parentHeight = getViewHolder().getChild().getHeight();
int width = getViewHolder().getContentWrapper().getWidth();
int height = getViewHolder().getContentWrapper().getHeight();
FrameLayout.LayoutParams p = (FrameLayout.LayoutParams) getViewHolder().getContent().getLayoutParams();
Expand All @@ -203,7 +193,7 @@ private void initContentWrapperLocation(int targetX, int targetY, int targetWidt
case CENTER:
if (p.width == FrameLayout.LayoutParams.MATCH_PARENT) {
int l = targetX - parentX;
int r = parentX + parentW - (targetX + targetWidth);
int r = parentX + parentWidth - (targetX + targetWidth);
if (l < r) {
w = targetWidth + l * 2;
x = 0;
Expand All @@ -228,7 +218,7 @@ private void initContentWrapperLocation(int targetX, int targetY, int targetWidt
break;
case TO_RIGHT:
if (p.width == FrameLayout.LayoutParams.MATCH_PARENT) {
int r = parentX + parentW - (targetX + targetWidth);
int r = parentX + parentWidth - (targetX + targetWidth);
x = targetX + targetWidth;
w = r;
w -= getConfig().mOffsetX;
Expand All @@ -239,7 +229,7 @@ private void initContentWrapperLocation(int targetX, int targetY, int targetWidt
case ALIGN_LEFT:
if (p.width == FrameLayout.LayoutParams.MATCH_PARENT) {
int l = targetX - parentX;
w = parentW - l;
w = parentWidth - l;
x = targetX;
w -= getConfig().mOffsetX;
} else {
Expand Down Expand Up @@ -269,7 +259,7 @@ private void initContentWrapperLocation(int targetX, int targetY, int targetWidt
x = 0;
w -= getConfig().mOffsetX;
} else {
x = parentX + parentW - width;
x = parentX + parentWidth - width;
}
break;
default:
Expand All @@ -279,7 +269,7 @@ private void initContentWrapperLocation(int targetX, int targetY, int targetWidt
case CENTER:
if (p.height == FrameLayout.LayoutParams.MATCH_PARENT) {
int t = targetY - parentY;
int b = parentY + parentH - (targetY + targetHeight);
int b = parentY + parentHeight - (targetY + targetHeight);
if (t < b) {
h = targetHeight + t * 2;
y = 0;
Expand All @@ -304,7 +294,7 @@ private void initContentWrapperLocation(int targetX, int targetY, int targetWidt
break;
case BELOW:
if (p.height == FrameLayout.LayoutParams.MATCH_PARENT) {
int b = parentY + parentH - (targetY + targetHeight);
int b = parentY + parentHeight - (targetY + targetHeight);
y = targetY + targetHeight;
h = b;
h -= getConfig().mOffsetY;
Expand All @@ -315,7 +305,7 @@ private void initContentWrapperLocation(int targetX, int targetY, int targetWidt
case ALIGN_TOP:
if (p.width == FrameLayout.LayoutParams.MATCH_PARENT) {
int t = targetY - parentY;
h = parentH - t;
h = parentHeight - t;
y = targetY;
h -= getConfig().mOffsetY;
} else {
Expand Down Expand Up @@ -345,7 +335,7 @@ private void initContentWrapperLocation(int targetX, int targetY, int targetWidt
h -= getConfig().mOffsetY;
y = 0;
} else {
y = parentY + parentH - height;
y = parentY + parentHeight - height;
}
break;
default:
Expand All @@ -363,21 +353,36 @@ private void initContentWrapperLocation(int targetX, int targetY, int targetWidt
params.height = (int) h;
getViewHolder().getContentWrapper().setLayoutParams(params);
}
if (getConfig().mUpdateLocationInterceptor != null) {
float[] xy = new float[]{x, y};
getConfig().mUpdateLocationInterceptor.interceptor(
xy, params.width, params.height,
targetX, targetY, targetWidth, targetHeight,
parentX, parentY, parentWidth, parentHeight
);
x = xy[0];
y = xy[1];
}
if (getConfig().mOffsetX != 0) {
x += getConfig().mOffsetX;
}
if (getConfig().mOffsetY != 0) {
y += getConfig().mOffsetY;
}
if (getConfig().mInside) {
x = Utils.floatRange(x, 0, parentW - w);
y = Utils.floatRange(y, 0, parentH - h);
x = Utils.floatRange(x, 0, parentWidth - w);
y = Utils.floatRange(y, 0, parentHeight - h);
}
getViewHolder().getContentWrapper().setX(x);
getViewHolder().getContentWrapper().setY(y);
}

private void initBackgroundLocation(int targetX, int targetY, int targetWidth, int targetHeight) {
private void initBackgroundLocation() {
if (!getConfig().mBackgroundAlign) {
getViewHolder().getBackground().setX(0);
getViewHolder().getBackground().setY(0);
return;
}
FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) getViewHolder().getBackground().getLayoutParams();
int width = getViewHolder().getBackground().getWidth();
int height = getViewHolder().getBackground().getHeight();
Expand Down Expand Up @@ -464,6 +469,37 @@ protected void initContent() {
getViewHolder().getContent().setLayoutParams(contentParams);
}

public void updateLocation() {
final View target = getViewHolder().getTarget();
final int[] locationTarget = new int[]{0, 0};
if (target != null) {
target.getLocationOnScreen(locationTarget);
}
final int[] locationRoot = new int[2];
getViewHolder().getDecor().getLocationOnScreen(locationRoot);
final int targetX = (locationTarget[0] - locationRoot[0]);
final int targetY = (locationTarget[1] - locationRoot[1]);
int targetWidth = 0;
int targetHeight = 0;
if (target != null) {
targetWidth = target.getWidth();
targetHeight = target.getWidth();
}
initContentWrapperLocation(targetX, targetY, targetWidth, targetHeight);
initBackgroundLocation();
}

public PopupLayer updateLocationInterceptor(UpdateLocationInterceptor interceptor) {
getConfig().mUpdateLocationInterceptor = interceptor;
return this;
}

public PopupLayer targetView(View targetView) {
getViewHolder().setTarget(targetView);
updateLocation();
return this;
}

/**
* 是否裁剪contentView至包裹边界
*
Expand Down Expand Up @@ -638,6 +674,7 @@ public View getTarget() {
}

protected static class Config extends DialogLayer.Config {
protected UpdateLocationInterceptor mUpdateLocationInterceptor = null;
protected boolean mContentClip = true;
protected boolean mBackgroundAlign = true;
protected boolean mBackgroundOffset = true;
Expand All @@ -652,4 +689,10 @@ protected static class Config extends DialogLayer.Config {

protected static class ListenerHolder extends DialogLayer.ListenerHolder {
}

public interface UpdateLocationInterceptor {
void interceptor(float[] popupXY, int popupWidth, int popupHeight,
int targetX, int targetY, int targetWidth, int targetHeight,
int parentX, int parentY, int parentWidth, int parentHeight);
}
}
Loading

0 comments on commit f331d92

Please sign in to comment.