Skip to content

Commit

Permalink
优化demo
Browse files Browse the repository at this point in the history
  • Loading branch information
zyyoona7 committed May 11, 2018
1 parent c910e0a commit f25f269
Show file tree
Hide file tree
Showing 12 changed files with 507 additions and 139 deletions.
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
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;
import android.widget.PopupWindow;
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;

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;
Expand All @@ -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;

Expand All @@ -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() {
Expand All @@ -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();
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<EverywherePopup> {

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 (touchX<getWidth() && screenHeight-touchY<getHeight()){
//左下弹出动画
getPopupWindow().setAnimationStyle(R.style.LeftBottomPopAnim);
offsetY=touchY-getHeight();
}else if (touchX+getWidth()>screenWidth && 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;
}
}
Original file line number Diff line number Diff line change
@@ -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 {
}
}
21 changes: 21 additions & 0 deletions app/src/main/res/anim/right_top_in_2.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

<scale
android:duration="@android:integer/config_shortAnimTime"
android:fromXScale="0"
android:fromYScale="0"
android:interpolator="@android:anim/accelerate_interpolator"
android:pivotX="80%"
android:pivotY="0"
android:toXScale="100%"
android:toYScale="100%"
/>

<alpha
android:duration="@android:integer/config_shortAnimTime"
android:fromAlpha="0"
android:interpolator="@android:anim/accelerate_interpolator"
android:toAlpha="1"
/>
</set>
21 changes: 21 additions & 0 deletions app/src/main/res/anim/right_top_out_2.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

<scale
android:duration="@android:integer/config_shortAnimTime"
android:fromXScale="100%"
android:fromYScale="100%"
android:interpolator="@android:anim/accelerate_interpolator"
android:pivotX="80%"
android:pivotY="0"
android:toXScale="0"
android:toYScale="0"
/>

<alpha
android:duration="@android:integer/config_shortAnimTime"
android:fromAlpha="1"
android:interpolator="@android:anim/accelerate_interpolator"
android:toAlpha="0"
/>
</set>
6 changes: 6 additions & 0 deletions app/src/main/res/drawable/shape_white_5_corner.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">

<solid android:color="#FFFFFF"/>
<corners android:radius="5dp"/>
</shape>
Loading

0 comments on commit f25f269

Please sign in to comment.