diff --git a/app/src/main/java/com/zyyoona7/easypopup/easypop/EasyPopActivity.java b/app/src/main/java/com/zyyoona7/easypopup/easypop/EasyPopActivity.java index d6f5fb9..01ec7bb 100644 --- a/app/src/main/java/com/zyyoona7/easypopup/easypop/EasyPopActivity.java +++ b/app/src/main/java/com/zyyoona7/easypopup/easypop/EasyPopActivity.java @@ -1,8 +1,10 @@ package com.zyyoona7.easypopup.easypop; import android.graphics.Color; +import android.support.v7.widget.AppCompatTextView; import android.util.Log; import android.view.Gravity; +import android.view.MotionEvent; import android.view.View; import android.widget.Button; import android.widget.LinearLayout; @@ -10,11 +12,13 @@ import android.widget.TextView; import com.blankj.utilcode.util.KeyboardUtils; +import com.blankj.utilcode.util.LogUtils; import com.blankj.utilcode.util.SizeUtils; import com.blankj.utilcode.util.ToastUtils; import com.zyyoona7.easypopup.R; import com.zyyoona7.easypopup.base.BaseActivity; import com.zyyoona7.easypopup.views.TitleBar; +import com.zyyoona7.easypopup.views.TriangleDrawable; import com.zyyoona7.lib.EasyPopup; import com.zyyoona7.lib.XGravity; import com.zyyoona7.lib.YGravity; @@ -22,7 +26,6 @@ public class EasyPopActivity extends BaseActivity implements View.OnClickListener { private static final String TAG = "EasyPopActivity"; - // TODO: 2018/5/9 重做Demo 带箭头的背景,微信长按跟随手指位置的弹窗 private TitleBar mTitleBar; private Button mCircleBtn; @@ -33,6 +36,7 @@ public class EasyPopActivity extends BaseActivity implements View.OnClickListene private Button mGiftBtn; private Button mCmmtBtn; private Button mComplexBtn; + private AppCompatTextView mEverywhereTv; private LinearLayout mComplexBgDimView; @@ -46,6 +50,9 @@ public class EasyPopActivity extends BaseActivity implements View.OnClickListene private CmmtPopup mCmmtPopup; private ComplexPopup mComplexPopup; + private EverywherePopup mEverywherePopup; + private float mLastX; + private float mLastY; @Override protected int setLayoutId() { @@ -70,6 +77,7 @@ protected void initViews() { mCmmtBtn = findViewById(R.id.btn_pop_cmmt); mComplexBtn = findViewById(R.id.btn_complex); mComplexBgDimView = findViewById(R.id.ll_complex_bg_dim); + mEverywhereTv = findViewById(R.id.tv_pop_everywhere); initQQPop(); initWeiboPop(); initCirclePop(); @@ -79,6 +87,28 @@ protected void initViews() { initGiftPop(); initCmmtPop(); initComplexPop(); + + mEverywherePopup=EverywherePopup.create(this) + .apply(); + + mEverywhereTv.setOnTouchListener(new View.OnTouchListener() { + @Override + public boolean onTouch(View v, MotionEvent event) { + if (event.getAction() == MotionEvent.ACTION_DOWN) { + mLastX = event.getRawX(); + mLastY = event.getRawY(); + LogUtils.i("onTouch x=" + mLastX + ",y=" + mLastY); + } + return false; + } + }); + mEverywhereTv.setOnLongClickListener(new View.OnLongClickListener() { + @Override + public boolean onLongClick(View v) { + mEverywherePopup.showEverywhere(v,(int)mLastX,(int)mLastY); + return true; + } + }); } @Override @@ -114,7 +144,14 @@ private void initQQPop() { mQQPop = EasyPopup.create() .setContext(this) .setContentView(R.layout.layout_right_pop) - .setAnimationStyle(R.style.RightTopPopAnim) + .setAnimationStyle(R.style.RightTop2PopAnim) + .setOnViewListener(new EasyPopup.OnViewListener() { + @Override + public void initViews(View view) { + View arrowView = view.findViewById(R.id.v_arrow); + arrowView.setBackground(new TriangleDrawable(TriangleDrawable.TOP, Color.parseColor("#88FF88"))); + } + }) .setFocusAndOutsideEnable(true) // .setBackgroundDimEnable(true) // .setDimValue(0.5f) @@ -125,19 +162,29 @@ private void initQQPop() { } private void showQQPop(View view) { - mQQPop.showAtAnchorView(view, YGravity.BELOW, XGravity.LEFT, SizeUtils.dp2px(30), 0); + int offsetX = SizeUtils.dp2px(20) - view.getWidth() / 2; + int offsetY = (mTitleBar.getHeight() - view.getHeight()) / 2; + mQQPop.showAtAnchorView(view, YGravity.BELOW, XGravity.ALIGN_RIGHT, offsetX, offsetY); } private void initWeiboPop() { mWeiboPop = EasyPopup.create() .setContentView(this, R.layout.layout_center_pop) .setAnimationStyle(R.style.TopPopAnim) + .setOnViewListener(new EasyPopup.OnViewListener() { + @Override + public void initViews(View view) { + View arrowView = view.findViewById(R.id.v_arrow_weibo); + arrowView.setBackground(new TriangleDrawable(TriangleDrawable.TOP, Color.WHITE)); + } + }) .setFocusAndOutsideEnable(true) .apply(); } private void showWeiboPop(View view) { - mWeiboPop.showAtAnchorView(view, YGravity.BELOW, XGravity.CENTER, 0, 0); + int offsetY = (mTitleBar.getHeight() - view.getHeight()) / 2; + mWeiboPop.showAtAnchorView(view, YGravity.BELOW, XGravity.CENTER, 0, offsetY); } private void initCirclePop() { diff --git a/app/src/main/java/com/zyyoona7/easypopup/easypop/EverywherePopup.java b/app/src/main/java/com/zyyoona7/easypopup/easypop/EverywherePopup.java new file mode 100644 index 0000000..2b1de75 --- /dev/null +++ b/app/src/main/java/com/zyyoona7/easypopup/easypop/EverywherePopup.java @@ -0,0 +1,71 @@ +package com.zyyoona7.easypopup.easypop; + +import android.content.Context; +import android.view.Gravity; +import android.view.View; + +import com.blankj.utilcode.util.ScreenUtils; +import com.zyyoona7.easypopup.R; +import com.zyyoona7.lib.BasePopup; + +public class EverywherePopup extends BasePopup { + + public static EverywherePopup create(Context context) { + return new EverywherePopup(context); + } + + private EverywherePopup(Context context) { + setContext(context); + } + + @Override + protected void initAttributes() { + setContentView(R.layout.layout_everywhere_pop) + .setAnimationStyle(R.style.LeftTopPopAnim); + } + + @Override + protected void initViews(View view) { + +// setOnRealWHAlreadyListener(new OnRealWHAlreadyListener() { +// @Override +// public void onRealWHAlready(BasePopup basePopup, int popWidth, int popHeight, int anchorW, int anchorH) { +// +// } +// }); + } + + /** + * 自适应触摸点 弹出 + * @param parent + * @param touchX + * @param touchY + * @return + */ + public EverywherePopup showEverywhere(View parent,int touchX, int touchY) { +// if (isRealWHAlready()) { + int screenHeight = ScreenUtils.getScreenHeight(); + int screenWidth = ScreenUtils.getScreenWidth(); + int offsetX=touchX; + int offsetY=touchY; + if (touchXscreenWidth && touchY+getHeight()>screenHeight){ + //右下弹出动画 + getPopupWindow().setAnimationStyle(R.style.RightBottomPopAnim); + offsetX=(touchX-getWidth()); + offsetY=touchY-getHeight(); + }else if (touchX+getWidth()>screenWidth){ + getPopupWindow().setAnimationStyle(R.style.RightTopPopAnim); + offsetX=(touchX-getWidth()); + }else { + getPopupWindow().setAnimationStyle(R.style.LeftTopPopAnim); + } + + showAtLocation(parent, Gravity.NO_GRAVITY,offsetX,offsetY); +// } + return this; + } +} diff --git a/app/src/main/java/com/zyyoona7/easypopup/views/TriangleDrawable.java b/app/src/main/java/com/zyyoona7/easypopup/views/TriangleDrawable.java new file mode 100644 index 0000000..58f15a7 --- /dev/null +++ b/app/src/main/java/com/zyyoona7/easypopup/views/TriangleDrawable.java @@ -0,0 +1,97 @@ +package com.zyyoona7.easypopup.views; + +import android.graphics.Canvas; +import android.graphics.Color; +import android.graphics.ColorFilter; +import android.graphics.Paint; +import android.graphics.Path; +import android.graphics.PixelFormat; +import android.graphics.Rect; +import android.graphics.drawable.Drawable; +import android.support.annotation.IntDef; +import android.support.annotation.IntRange; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; + +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; + +/** + * Created by jiang on 2017/5/19. + */ + +public class TriangleDrawable extends Drawable { + public static final int TOP = 12; + public static final int BOTTOM = 13; + public static final int LEFT = 14; + public static final int RIGHT = 15; + + private int bgColor = Color.WHITE; + @ARROWDIRECTION + private int arrowDirection; + + public TriangleDrawable(@ARROWDIRECTION int arrowDirection, int bgColor) { + this.arrowDirection = arrowDirection; + this.bgColor = bgColor; + } + + @Override + public void draw(@NonNull Canvas canvas) { + + Paint paint = new Paint(); + paint.setAntiAlias(true); + paint.setColor(bgColor); + paint.setStyle(Paint.Style.FILL); + Path path = createPath(); + canvas.drawPath(path, paint); + } + + private Path createPath() { + Rect bound = getBounds(); + Path path = new Path(); + if (arrowDirection == TOP) { + path.moveTo(bound.right / 2, 0); + path.lineTo(0, bound.bottom); + path.lineTo(bound.right, bound.bottom); + path.close(); + } else if (arrowDirection == BOTTOM) { + path.moveTo(bound.right / 2, bound.bottom); + path.lineTo(0, 0); + path.lineTo(bound.right, 0); + path.close(); + + } else if (arrowDirection == LEFT) { + path.moveTo(0, bound.bottom / 2); + path.lineTo(bound.right, 0); + path.lineTo(bound.right, bound.bottom); + path.close(); + } else { + path.moveTo(bound.right, bound.bottom / 2); + path.lineTo(0, 0); + path.lineTo(0, bound.bottom); + path.close(); + } + return path; + + } + + @Override + public void setAlpha(@IntRange(from = 0, to = 255) int alpha) { + + } + + @Override + public void setColorFilter(@Nullable ColorFilter colorFilter) { + + } + + @Override + public int getOpacity() { + return PixelFormat.TRANSPARENT; + } + + @IntDef({TOP, BOTTOM, LEFT, RIGHT}) + @Retention(RetentionPolicy.SOURCE) + public @interface ARROWDIRECTION { + } +} \ No newline at end of file diff --git a/app/src/main/res/anim/right_top_in_2.xml b/app/src/main/res/anim/right_top_in_2.xml new file mode 100644 index 0000000..f527b75 --- /dev/null +++ b/app/src/main/res/anim/right_top_in_2.xml @@ -0,0 +1,21 @@ + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/anim/right_top_out_2.xml b/app/src/main/res/anim/right_top_out_2.xml new file mode 100644 index 0000000..5385f9b --- /dev/null +++ b/app/src/main/res/anim/right_top_out_2.xml @@ -0,0 +1,21 @@ + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/drawable/shape_white_5_corner.xml b/app/src/main/res/drawable/shape_white_5_corner.xml new file mode 100644 index 0000000..0b36c8b --- /dev/null +++ b/app/src/main/res/drawable/shape_white_5_corner.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/activity_easy_pop.xml b/app/src/main/res/layout/activity_easy_pop.xml index 26b4cdb..7e1dd24 100644 --- a/app/src/main/res/layout/activity_easy_pop.xml +++ b/app/src/main/res/layout/activity_easy_pop.xml @@ -1,7 +1,6 @@ - + android:fillViewport="true"> -