├── .gitignore
├── .idea
├── compiler.xml
├── copyright
│ └── profiles_settings.xml
├── dictionaries
│ └── gavin.xml
├── gradle.xml
├── kotlinc.xml
├── misc.xml
├── modules.xml
├── runConfigurations.xml
└── vcs.xml
├── README.md
├── app
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src
│ └── main
│ ├── AndroidManifest.xml
│ ├── java
│ └── com
│ │ └── gavin
│ │ └── com
│ │ └── popuptest
│ │ ├── MainActivity.java
│ │ └── TestPopupWindow.java
│ └── res
│ ├── layout
│ ├── activity_main.xml
│ └── popup_test.xml
│ ├── mipmap-hdpi
│ ├── ic_launcher.png
│ └── ic_launcher_round.png
│ ├── mipmap-mdpi
│ ├── ic_launcher.png
│ └── ic_launcher_round.png
│ ├── mipmap-xhdpi
│ ├── ic_launcher.png
│ └── ic_launcher_round.png
│ ├── mipmap-xxhdpi
│ ├── ic_launcher.png
│ └── ic_launcher_round.png
│ ├── mipmap-xxxhdpi
│ ├── ic_launcher.png
│ └── ic_launcher_round.png
│ └── values
│ ├── colors.xml
│ ├── strings.xml
│ └── styles.xml
├── build.gradle
├── gradle.properties
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── settings.gradle
└── smartpopupwindow
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src
└── main
├── AndroidManifest.xml
├── java
└── com
│ └── gavin
│ └── com
│ └── smartpopupwindow
│ ├── HorizontalPosition.java
│ ├── SmartPopupWindow.java
│ └── VerticalPosition.java
└── res
└── values
└── strings.xml
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /.idea/workspace.xml
5 | /.idea/libraries
6 | .DS_Store
7 | /build
8 | /captures
9 | .externalNativeBuild
10 |
--------------------------------------------------------------------------------
/.idea/compiler.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/.idea/copyright/profiles_settings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
--------------------------------------------------------------------------------
/.idea/dictionaries/gavin.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
--------------------------------------------------------------------------------
/.idea/gradle.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
18 |
19 |
--------------------------------------------------------------------------------
/.idea/kotlinc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
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 | Android > Lint > Correctness
107 |
108 |
109 | Android > Lint > Performance
110 |
111 |
112 |
113 |
114 | Android
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 | 1.8
141 |
142 |
143 |
144 |
145 |
146 |
147 |
148 |
149 |
150 |
151 |
152 |
153 |
154 |
155 |
156 |
157 |
158 |
159 |
160 |
161 |
162 |
163 |
--------------------------------------------------------------------------------
/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/.idea/runConfigurations.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # SmartPopupWindow
2 | 用法:
3 | ```java
4 | SmartPopupWindow popupWindow= SmartPopupWindow.Builder
5 | .build(Activity.this, view)
6 | .setAlpha(0.4f) //背景灰度 默认全透明
7 | .setOutsideTouchDismiss(false) //点击外部消失 默认true(消失)
8 | .createPopupWindow();
9 | popupWindow.showAtAnchorView(view, VerticalPosition.ABOVE, HorizontalPosition.CENTER);
10 | ```
11 | 水平方向参数HorizontalPosition:LEFT 、 RIGHT 、 ALIGN_LEFT 、 ALIGN_RIGHT、 CENTER
12 | 竖直方向参数VerticalPosition :ABOVE 、 BELOW、 ALIGN_TOP 、 ALIGN_BOTTOM、 CENTER
13 |
--------------------------------------------------------------------------------
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 25
5 | buildToolsVersion "25.0.2"
6 | defaultConfig {
7 | applicationId "com.gavin.com.popuptest"
8 | minSdkVersion 16
9 | targetSdkVersion 25
10 | versionCode 1
11 | versionName "1.0"
12 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
13 | }
14 | buildTypes {
15 | release {
16 | minifyEnabled false
17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
18 | }
19 | }
20 | }
21 |
22 | dependencies {
23 | compile fileTree(include: ['*.jar'], dir: 'libs')
24 | compile 'com.android.support:appcompat-v7:25.3.1'
25 | compile 'com.android.support.constraint:constraint-layout:1.0.2'
26 | compile project(':smartpopupwindow')
27 | }
28 |
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in /Users/gavin/Library/Android/sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
19 | # Uncomment this to preserve the line number information for
20 | # debugging stack traces.
21 | #-keepattributes SourceFile,LineNumberTable
22 |
23 | # If you keep the line number information, uncomment this to
24 | # hide the original source file name.
25 | #-renamesourcefileattribute SourceFile
26 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/app/src/main/java/com/gavin/com/popuptest/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.gavin.com.popuptest;
2 |
3 | import android.os.Bundle;
4 | import android.support.annotation.IdRes;
5 | import android.support.v4.widget.PopupWindowCompat;
6 | import android.support.v7.app.AppCompatActivity;
7 | import android.view.Gravity;
8 | import android.view.View;
9 | import android.view.ViewGroup;
10 | import android.widget.RadioGroup;
11 |
12 | import com.gavin.com.smartpopupwindow.HorizontalPosition;
13 | import com.gavin.com.smartpopupwindow.SmartPopupWindow;
14 | import com.gavin.com.smartpopupwindow.VerticalPosition;
15 |
16 | public class MainActivity extends AppCompatActivity {
17 |
18 | private TestPopupWindow mWindow;
19 |
20 | private View mButton;
21 | private View mPopupContentView;
22 |
23 | private int mGravity = Gravity.START;
24 | private int mOffsetX = 0;
25 | private int mOffsetY = 0;
26 |
27 | private boolean useSmartPopup = true;
28 |
29 | @Override
30 | protected void onCreate(Bundle savedInstanceState) {
31 | super.onCreate(savedInstanceState);
32 | setContentView(R.layout.activity_main);
33 | mWindow = new TestPopupWindow(this);
34 | mButton = findViewById(R.id.btn);
35 | mPopupContentView = getLayoutInflater().inflate(R.layout.popup_test, null);
36 | RadioGroup radioGroup = (RadioGroup) findViewById(R.id.rg);
37 |
38 |
39 | mWindow = new TestPopupWindow(this);
40 | View contentView = mWindow.getContentView();
41 | //需要先测量,PopupWindow还未弹出时,宽高为0
42 | contentView.measure(makeDropDownMeasureSpec(mWindow.getWidth()), makeDropDownMeasureSpec(mWindow.getHeight()));
43 | radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
44 |
45 | @Override
46 | public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
47 | switch (checkedId) {
48 | case R.id.below_left:
49 | mGravity = Gravity.START;
50 | mOffsetX = -mWindow.getContentView().getMeasuredWidth();
51 | mOffsetY = 0;
52 | //使用SmartPopup
53 | if (useSmartPopup)
54 | SmartPopupWindow.Builder
55 | .build(MainActivity.this, mPopupContentView)
56 | .createPopupWindow()
57 | .showAtAnchorView(mButton, VerticalPosition.BELOW, HorizontalPosition.LEFT);
58 | break;
59 | case R.id.below_align_left:
60 | mGravity = Gravity.START;
61 | mOffsetX = 0;
62 | mOffsetY = 0;
63 | //使用SmartPopup
64 | if (useSmartPopup)
65 | SmartPopupWindow.Builder
66 | .build(MainActivity.this, mPopupContentView)
67 | .createPopupWindow()
68 | .showAtAnchorView(mButton, VerticalPosition.BELOW, HorizontalPosition.ALIGN_LEFT);
69 |
70 | break;
71 | case R.id.below_right:
72 | mGravity = Gravity.END;
73 | mOffsetX = 0;
74 | mOffsetY = 0;
75 | //使用SmartPopup
76 | if (useSmartPopup)
77 | SmartPopupWindow.Builder
78 | .build(MainActivity.this, mPopupContentView)
79 | .createPopupWindow()
80 | .showAtAnchorView(mButton, VerticalPosition.BELOW, HorizontalPosition.RIGHT);
81 |
82 | break;
83 | case R.id.below_align_right:
84 | mGravity = Gravity.START;
85 | mOffsetX = mButton.getWidth() - mWindow.getContentView().getMeasuredWidth();
86 | mOffsetY = 0;
87 | //使用SmartPopup
88 | if (useSmartPopup)
89 | SmartPopupWindow.Builder
90 | .build(MainActivity.this, mPopupContentView)
91 | .createPopupWindow()
92 | .showAtAnchorView(mButton, VerticalPosition.BELOW, HorizontalPosition.ALIGN_RIGHT);
93 |
94 | break;
95 | case R.id.below_center:
96 | mGravity = Gravity.START;
97 | mOffsetX = Math.abs(mWindow.getContentView().getMeasuredWidth() - mButton.getWidth()) / 2;
98 | mOffsetY = 0;
99 | //使用SmartPopup
100 | if (useSmartPopup)
101 | SmartPopupWindow.Builder
102 | .build(MainActivity.this, mPopupContentView)
103 | .createPopupWindow()
104 | .showAtAnchorView(mButton, VerticalPosition.BELOW, HorizontalPosition.CENTER);
105 |
106 | break;
107 | case R.id.above_center:
108 | mGravity = Gravity.START;
109 | mOffsetX = Math.abs(mWindow.getContentView().getMeasuredWidth() - mButton.getWidth()) / 2;
110 | mOffsetY = -(mWindow.getContentView().getMeasuredHeight() + mButton.getHeight());
111 | //使用SmartPopup
112 | if (useSmartPopup)
113 | SmartPopupWindow.Builder
114 | .build(MainActivity.this, mPopupContentView)
115 | .createPopupWindow()
116 | .showAtAnchorView(mButton, VerticalPosition.ABOVE, HorizontalPosition.CENTER);
117 |
118 | break;
119 | case R.id.left_center:
120 | mGravity = Gravity.START;
121 | mOffsetX = -mWindow.getContentView().getMeasuredWidth();
122 | mOffsetY = -(mWindow.getContentView().getMeasuredHeight() + mButton.getHeight()) / 2;
123 | //使用SmartPopup
124 | if (useSmartPopup)
125 | SmartPopupWindow.Builder
126 | .build(MainActivity.this, mPopupContentView)
127 | .createPopupWindow()
128 | .showAtAnchorView(mButton, VerticalPosition.CENTER, HorizontalPosition.LEFT);
129 |
130 | break;
131 | case R.id.right_center:
132 | mGravity = Gravity.END;
133 | mOffsetX = 0;
134 | mOffsetY = -(mWindow.getContentView().getMeasuredHeight() + mButton.getHeight()) / 2;
135 | //使用SmartPopup
136 | if (useSmartPopup)
137 | SmartPopupWindow.Builder
138 | .build(MainActivity.this, mPopupContentView)
139 | .createPopupWindow()
140 | .showAtAnchorView(mButton, VerticalPosition.CENTER, HorizontalPosition.RIGHT);
141 |
142 | break;
143 | }
144 | }
145 | });
146 | mButton.setOnClickListener(new View.OnClickListener() {
147 | @Override
148 | public void onClick(View v) {
149 | showPopup();
150 | }
151 | });
152 | }
153 |
154 | @SuppressWarnings("ResourceType")
155 | private static int makeDropDownMeasureSpec(int measureSpec) {
156 | int mode;
157 | if (measureSpec == ViewGroup.LayoutParams.WRAP_CONTENT) {
158 | mode = View.MeasureSpec.UNSPECIFIED;
159 | } else {
160 | mode = View.MeasureSpec.EXACTLY;
161 | }
162 | return View.MeasureSpec.makeMeasureSpec(View.MeasureSpec.getSize(measureSpec), mode);
163 | }
164 |
165 | private void showPopup() {
166 | PopupWindowCompat.showAsDropDown(mWindow, mButton, mOffsetX, mOffsetY, mGravity);
167 | }
168 |
169 | }
170 |
--------------------------------------------------------------------------------
/app/src/main/java/com/gavin/com/popuptest/TestPopupWindow.java:
--------------------------------------------------------------------------------
1 | package com.gavin.com.popuptest;
2 |
3 | import android.content.Context;
4 | import android.graphics.Color;
5 | import android.graphics.drawable.ColorDrawable;
6 | import android.view.LayoutInflater;
7 | import android.view.View;
8 | import android.view.ViewGroup;
9 | import android.widget.PopupWindow;
10 |
11 | /**
12 | * Created by gavin
13 | * Created date 17/9/13
14 | */
15 |
16 | public class TestPopupWindow extends PopupWindow {
17 |
18 | public TestPopupWindow(Context context) {
19 | super(context);
20 | setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
21 | setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
22 | setOutsideTouchable(true);
23 | setFocusable(true);
24 | setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
25 | View contentView = LayoutInflater.from(context).inflate(R.layout.popup_test, null, false);
26 | setContentView(contentView);
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
9 |
10 |
14 |
15 |
21 |
22 |
28 |
29 |
34 |
35 |
40 |
41 |
46 |
47 |
52 |
53 |
58 |
59 |
64 |
65 |
66 |
67 |
75 |
76 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/popup_test.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
15 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Gavin-ZYX/SmartPopupWindow/3a099f88fcd995d5f3c86a2bcec424aad79af649/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Gavin-ZYX/SmartPopupWindow/3a099f88fcd995d5f3c86a2bcec424aad79af649/app/src/main/res/mipmap-hdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Gavin-ZYX/SmartPopupWindow/3a099f88fcd995d5f3c86a2bcec424aad79af649/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Gavin-ZYX/SmartPopupWindow/3a099f88fcd995d5f3c86a2bcec424aad79af649/app/src/main/res/mipmap-mdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Gavin-ZYX/SmartPopupWindow/3a099f88fcd995d5f3c86a2bcec424aad79af649/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Gavin-ZYX/SmartPopupWindow/3a099f88fcd995d5f3c86a2bcec424aad79af649/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Gavin-ZYX/SmartPopupWindow/3a099f88fcd995d5f3c86a2bcec424aad79af649/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Gavin-ZYX/SmartPopupWindow/3a099f88fcd995d5f3c86a2bcec424aad79af649/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Gavin-ZYX/SmartPopupWindow/3a099f88fcd995d5f3c86a2bcec424aad79af649/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Gavin-ZYX/SmartPopupWindow/3a099f88fcd995d5f3c86a2bcec424aad79af649/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #3F51B5
4 | #303F9F
5 | #FF4081
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | PopupTest
3 |
4 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/build.gradle:
--------------------------------------------------------------------------------
1 | // Top-level build file where you can add configuration options common to all sub-projects/modules.
2 |
3 | buildscript {
4 | repositories {
5 | jcenter()
6 | }
7 | dependencies {
8 | classpath 'com.android.tools.build:gradle:2.3.1'
9 |
10 | // NOTE: Do not place your application dependencies here; they belong
11 | // in the individual module build.gradle files
12 | }
13 | }
14 |
15 | allprojects {
16 | repositories {
17 | jcenter()
18 | }
19 | }
20 |
21 | task clean(type: Delete) {
22 | delete rootProject.buildDir
23 | }
24 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 |
3 | # IDE (e.g. Android Studio) users:
4 | # Gradle settings configured through the IDE *will override*
5 | # any settings specified in this file.
6 |
7 | # For more details on how to configure your build environment visit
8 | # http://www.gradle.org/docs/current/userguide/build_environment.html
9 |
10 | # Specifies the JVM arguments used for the daemon process.
11 | # The setting is particularly useful for tweaking memory settings.
12 | org.gradle.jvmargs=-Xmx1536m
13 |
14 | # When configured, Gradle will run in incubating parallel mode.
15 | # This option should only be used with decoupled projects. More details, visit
16 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
17 | # org.gradle.parallel=true
18 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Gavin-ZYX/SmartPopupWindow/3a099f88fcd995d5f3c86a2bcec424aad79af649/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Wed Sep 13 16:28:56 CST 2017
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip
7 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10 | DEFAULT_JVM_OPTS=""
11 |
12 | APP_NAME="Gradle"
13 | APP_BASE_NAME=`basename "$0"`
14 |
15 | # Use the maximum available, or set MAX_FD != -1 to use that value.
16 | MAX_FD="maximum"
17 |
18 | warn ( ) {
19 | echo "$*"
20 | }
21 |
22 | die ( ) {
23 | echo
24 | echo "$*"
25 | echo
26 | exit 1
27 | }
28 |
29 | # OS specific support (must be 'true' or 'false').
30 | cygwin=false
31 | msys=false
32 | darwin=false
33 | case "`uname`" in
34 | CYGWIN* )
35 | cygwin=true
36 | ;;
37 | Darwin* )
38 | darwin=true
39 | ;;
40 | MINGW* )
41 | msys=true
42 | ;;
43 | esac
44 |
45 | # Attempt to set APP_HOME
46 | # Resolve links: $0 may be a link
47 | PRG="$0"
48 | # Need this for relative symlinks.
49 | while [ -h "$PRG" ] ; do
50 | ls=`ls -ld "$PRG"`
51 | link=`expr "$ls" : '.*-> \(.*\)$'`
52 | if expr "$link" : '/.*' > /dev/null; then
53 | PRG="$link"
54 | else
55 | PRG=`dirname "$PRG"`"/$link"
56 | fi
57 | done
58 | SAVED="`pwd`"
59 | cd "`dirname \"$PRG\"`/" >/dev/null
60 | APP_HOME="`pwd -P`"
61 | cd "$SAVED" >/dev/null
62 |
63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
64 |
65 | # Determine the Java command to use to start the JVM.
66 | if [ -n "$JAVA_HOME" ] ; then
67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
68 | # IBM's JDK on AIX uses strange locations for the executables
69 | JAVACMD="$JAVA_HOME/jre/sh/java"
70 | else
71 | JAVACMD="$JAVA_HOME/bin/java"
72 | fi
73 | if [ ! -x "$JAVACMD" ] ; then
74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
75 |
76 | Please set the JAVA_HOME variable in your environment to match the
77 | location of your Java installation."
78 | fi
79 | else
80 | JAVACMD="java"
81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
82 |
83 | Please set the JAVA_HOME variable in your environment to match the
84 | location of your Java installation."
85 | fi
86 |
87 | # Increase the maximum file descriptors if we can.
88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
89 | MAX_FD_LIMIT=`ulimit -H -n`
90 | if [ $? -eq 0 ] ; then
91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
92 | MAX_FD="$MAX_FD_LIMIT"
93 | fi
94 | ulimit -n $MAX_FD
95 | if [ $? -ne 0 ] ; then
96 | warn "Could not set maximum file descriptor limit: $MAX_FD"
97 | fi
98 | else
99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
100 | fi
101 | fi
102 |
103 | # For Darwin, add options to specify how the application appears in the dock
104 | if $darwin; then
105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
106 | fi
107 |
108 | # For Cygwin, switch paths to Windows format before running java
109 | if $cygwin ; then
110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
112 | JAVACMD=`cygpath --unix "$JAVACMD"`
113 |
114 | # We build the pattern for arguments to be converted via cygpath
115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
116 | SEP=""
117 | for dir in $ROOTDIRSRAW ; do
118 | ROOTDIRS="$ROOTDIRS$SEP$dir"
119 | SEP="|"
120 | done
121 | OURCYGPATTERN="(^($ROOTDIRS))"
122 | # Add a user-defined pattern to the cygpath arguments
123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
125 | fi
126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
127 | i=0
128 | for arg in "$@" ; do
129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
131 |
132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
134 | else
135 | eval `echo args$i`="\"$arg\""
136 | fi
137 | i=$((i+1))
138 | done
139 | case $i in
140 | (0) set -- ;;
141 | (1) set -- "$args0" ;;
142 | (2) set -- "$args0" "$args1" ;;
143 | (3) set -- "$args0" "$args1" "$args2" ;;
144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
150 | esac
151 | fi
152 |
153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
154 | function splitJvmOpts() {
155 | JVM_OPTS=("$@")
156 | }
157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
159 |
160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
161 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12 | set DEFAULT_JVM_OPTS=
13 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windowz variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app', ':smartpopupwindow'
2 |
--------------------------------------------------------------------------------
/smartpopupwindow/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/smartpopupwindow/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 |
3 | android {
4 | compileSdkVersion 25
5 | buildToolsVersion "25.0.2"
6 |
7 | defaultConfig {
8 | minSdkVersion 16
9 | targetSdkVersion 25
10 | versionCode 1
11 | versionName "1.0"
12 |
13 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
14 |
15 | }
16 | buildTypes {
17 | release {
18 | minifyEnabled false
19 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
20 | }
21 | }
22 | }
23 |
24 | dependencies {
25 | compile fileTree(dir: 'libs', include: ['*.jar'])
26 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
27 | exclude group: 'com.android.support', module: 'support-annotations'
28 | })
29 | compile 'com.android.support:appcompat-v7:25.3.1'
30 | testCompile 'junit:junit:4.12'
31 | }
32 |
--------------------------------------------------------------------------------
/smartpopupwindow/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in /Users/gavin/Library/Android/sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
19 | # Uncomment this to preserve the line number information for
20 | # debugging stack traces.
21 | #-keepattributes SourceFile,LineNumberTable
22 |
23 | # If you keep the line number information, uncomment this to
24 | # hide the original source file name.
25 | #-renamesourcefileattribute SourceFile
26 |
--------------------------------------------------------------------------------
/smartpopupwindow/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
5 |
6 |
10 |
11 |
12 |
13 |
14 |
--------------------------------------------------------------------------------
/smartpopupwindow/src/main/java/com/gavin/com/smartpopupwindow/HorizontalPosition.java:
--------------------------------------------------------------------------------
1 | package com.gavin.com.smartpopupwindow;
2 |
3 | import android.support.annotation.IntDef;
4 |
5 | import java.lang.annotation.Retention;
6 | import java.lang.annotation.RetentionPolicy;
7 |
8 | /**
9 | * Created by gavin
10 | * Created date 17/9/13
11 | */
12 |
13 |
14 | @IntDef({
15 | HorizontalPosition.CENTER,
16 | HorizontalPosition.LEFT,
17 | HorizontalPosition.RIGHT,
18 | HorizontalPosition.ALIGN_LEFT,
19 | HorizontalPosition.ALIGN_RIGHT,
20 | })
21 | @Retention(RetentionPolicy.SOURCE)
22 | public @interface HorizontalPosition {
23 | int CENTER = 0;
24 | int LEFT = 1;
25 | int RIGHT = 2;
26 | int ALIGN_LEFT = 3;
27 | int ALIGN_RIGHT = 4;
28 | }
--------------------------------------------------------------------------------
/smartpopupwindow/src/main/java/com/gavin/com/smartpopupwindow/SmartPopupWindow.java:
--------------------------------------------------------------------------------
1 | package com.gavin.com.smartpopupwindow;
2 |
3 | import android.animation.ValueAnimator;
4 | import android.app.Activity;
5 | import android.content.Context;
6 | import android.graphics.Color;
7 | import android.graphics.drawable.ColorDrawable;
8 | import android.os.Build;
9 | import android.support.annotation.NonNull;
10 | import android.support.v4.widget.PopupWindowCompat;
11 | import android.util.AttributeSet;
12 | import android.view.Gravity;
13 | import android.view.KeyEvent;
14 | import android.view.MotionEvent;
15 | import android.view.View;
16 | import android.view.ViewGroup;
17 | import android.view.ViewTreeObserver;
18 | import android.view.Window;
19 | import android.view.WindowManager;
20 | import android.widget.PopupWindow;
21 |
22 | /**
23 | * 可以在任意位置显示的PopupWindow
24 | * 用法:
25 | SmartPopupWindow popupWindow= SmartPopupWindow.Builder
26 | .build(Activity.this, view)
27 | .setAlpha(0.4f) //背景灰度 默认全透明
28 | .setOutsideTouchDismiss(false) //点击外部消失 默认true(消失)
29 | .createPopupWindow();
30 | popupWindow.showAtAnchorView(view, VerticalPosition.ABOVE, HorizontalPosition.CENTER);
31 | */
32 | public class SmartPopupWindow extends PopupWindow {
33 |
34 | private int mWidth = ViewGroup.LayoutParams.WRAP_CONTENT;
35 | private int mHeight = ViewGroup.LayoutParams.WRAP_CONTENT;
36 | private float mAlpha = 1f; //背景灰度 0-1 1表示全透明
37 | private Context mContext;
38 | private View mContentView;
39 | private boolean isTouchOutsideDismiss = true; //点击外部消失
40 | private int mAnimationStyle = -1;
41 |
42 | //下面的几个变量只是位置处理外部点击事件(6.0以上)
43 | //是否只是获取宽高
44 | //getViewTreeObserver监听时
45 | private boolean isOnlyGetWH = true;
46 | private View mAnchorView;
47 | @VerticalPosition
48 | private int mVerticalGravity = VerticalPosition.BELOW;
49 | @HorizontalPosition
50 | private int mHorizontalGravity = HorizontalPosition.LEFT;
51 | private int mOffsetX;
52 | private int mOffsetY;
53 |
54 | public SmartPopupWindow(Context context) {
55 | this(context, null);
56 | }
57 |
58 | public SmartPopupWindow(Context context, AttributeSet attrs) {
59 | this(context, attrs, 0);
60 | }
61 |
62 | public SmartPopupWindow(Context context, AttributeSet attrs, int defStyleAttr) {
63 | super(context, attrs, defStyleAttr);
64 | mContext = context;
65 | }
66 |
67 | public void init() {
68 | setContentView(mContentView);
69 | setHeight(mHeight);
70 | setWidth(mWidth);
71 | touchOutsideDismiss(isTouchOutsideDismiss);
72 | if (mAnimationStyle != -1) {
73 | setAnimationStyle(mAnimationStyle);
74 | }
75 | }
76 |
77 | private void touchOutsideDismiss(boolean touchOutsideDismiss) {
78 | if (!touchOutsideDismiss) {
79 | setFocusable(true);
80 | setOutsideTouchable(false);
81 | setBackgroundDrawable(null);
82 |
83 | getContentView().setFocusable(true);
84 | getContentView().setFocusableInTouchMode(true);
85 | getContentView().setOnKeyListener(new View.OnKeyListener() {
86 | @Override
87 | public boolean onKey(View v, int keyCode, KeyEvent event) {
88 | if (keyCode == KeyEvent.KEYCODE_BACK) {
89 | dismiss();
90 | return true;
91 | }
92 | return false;
93 | }
94 | });
95 | //在Android 6.0以上 ,只能通过拦截事件来解决
96 | setTouchInterceptor(new View.OnTouchListener() {
97 | @Override
98 | public boolean onTouch(View v, MotionEvent event) {
99 |
100 | final int x = (int) event.getX();
101 | final int y = (int) event.getY();
102 |
103 | if ((event.getAction() == MotionEvent.ACTION_DOWN)
104 | && ((x < 0) || (x >= mWidth) || (y < 0) || (y >= mHeight))) {
105 | //outside
106 | return true;
107 | } else if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
108 | //outside
109 | return true;
110 | }
111 | return false;
112 | }
113 | });
114 | } else {
115 | setFocusable(true);
116 | setOutsideTouchable(true);
117 | setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
118 | }
119 | }
120 |
121 | @Override
122 | public void showAtLocation(View parent, int gravity, int x, int y) {
123 | isOnlyGetWH = true;
124 | mAnchorView = parent;
125 | mOffsetX = x;
126 | mOffsetY = y;
127 | addGlobalLayoutListener(getContentView());
128 | super.showAtLocation(parent, gravity, x, y);
129 | }
130 |
131 | public void showAtAnchorView(@NonNull View anchorView, @VerticalPosition int verticalPos, @HorizontalPosition int horizontalPos) {
132 | showAtAnchorView(anchorView, verticalPos, horizontalPos, true);
133 | }
134 |
135 | public void showAtAnchorView(@NonNull View anchorView, @VerticalPosition int verticalPos, @HorizontalPosition int horizontalPos, boolean fitInScreen) {
136 | showAtAnchorView(anchorView, verticalPos, horizontalPos, 0, 0, fitInScreen);
137 | }
138 | public void showAtAnchorView(@NonNull View anchorView, @VerticalPosition int verticalPos, @HorizontalPosition int horizontalPos, int x, int y) {
139 | showAtAnchorView(anchorView, verticalPos, horizontalPos, x, y, true);
140 | }
141 |
142 | public void showAtAnchorView(@NonNull View anchorView, @VerticalPosition int verticalPos, @HorizontalPosition int horizontalPos, int x, int y, boolean fitInScreen) {
143 | isOnlyGetWH = false;
144 | mAnchorView = anchorView;
145 | mOffsetX = x;
146 | mOffsetY = y;
147 | mVerticalGravity = verticalPos;
148 | mHorizontalGravity = horizontalPos;
149 | showBackgroundAnimator();
150 | final View contentView = getContentView();
151 | addGlobalLayoutListener(contentView);
152 | setClippingEnabled(fitInScreen);
153 | contentView.measure(makeDropDownMeasureSpec(getWidth()), makeDropDownMeasureSpec(getHeight()));
154 | final int measuredW = contentView.getMeasuredWidth();
155 | final int measuredH = contentView.getMeasuredHeight();
156 | if (!fitInScreen) {
157 | final int[] anchorLocation = new int[2];
158 | anchorView.getLocationInWindow(anchorLocation);
159 | x += anchorLocation[0];
160 | y += anchorLocation[1] + anchorView.getHeight();
161 | }
162 | y = calculateY(anchorView, verticalPos, measuredH, y);
163 | x = calculateX(anchorView, horizontalPos, measuredW, x);
164 | if (fitInScreen) {
165 | PopupWindowCompat.showAsDropDown(this, anchorView, x, y, Gravity.NO_GRAVITY);
166 | } else {
167 | showAtLocation(anchorView, Gravity.NO_GRAVITY, x, y);
168 | }
169 | }
170 |
171 | /**
172 | * 根据垂直gravity计算y偏移
173 | */
174 | private int calculateY(View anchor, int verticalGravity, int measuredH, int y) {
175 | switch (verticalGravity) {
176 | case VerticalPosition.ABOVE:
177 | y -= measuredH + anchor.getHeight();
178 | break;
179 | case VerticalPosition.ALIGN_BOTTOM:
180 | y -= measuredH;
181 | break;
182 | case VerticalPosition.CENTER:
183 | y -= anchor.getHeight() / 2 + measuredH / 2;
184 | break;
185 | case VerticalPosition.ALIGN_TOP:
186 | y -= anchor.getHeight();
187 | break;
188 | case VerticalPosition.BELOW:
189 | // Default position.
190 | break;
191 | }
192 |
193 | return y;
194 | }
195 |
196 | /**
197 | * 根据水平gravity计算x偏移
198 | */
199 | private int calculateX(View anchor, int horizontalGravity, int measuredW, int x) {
200 | switch (horizontalGravity) {
201 | case HorizontalPosition.LEFT:
202 | x -= measuredW;
203 | break;
204 | case HorizontalPosition.ALIGN_RIGHT:
205 | x -= measuredW - anchor.getWidth();
206 | break;
207 | case HorizontalPosition.CENTER:
208 | x += anchor.getWidth() / 2 - measuredW / 2;
209 | break;
210 | case HorizontalPosition.ALIGN_LEFT:
211 | // Default position.
212 | break;
213 | case HorizontalPosition.RIGHT:
214 | x += anchor.getWidth();
215 | break;
216 | }
217 |
218 | return x;
219 | }
220 |
221 | @SuppressWarnings("ResourceType")
222 | private static int makeDropDownMeasureSpec(int measureSpec) {
223 | return View.MeasureSpec.makeMeasureSpec(View.MeasureSpec.getSize(measureSpec), getDropDownMeasureSpecMode(measureSpec));
224 | }
225 |
226 | private static int getDropDownMeasureSpecMode(int measureSpec) {
227 | switch (measureSpec) {
228 | case ViewGroup.LayoutParams.WRAP_CONTENT:
229 | return View.MeasureSpec.UNSPECIFIED;
230 | default:
231 | return View.MeasureSpec.EXACTLY;
232 | }
233 | }
234 |
235 | //监听器,用于PopupWindow弹出时获取准确的宽高
236 | private final ViewTreeObserver.OnGlobalLayoutListener mOnGlobalLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {
237 | @Override
238 | public void onGlobalLayout() {
239 | mWidth = getContentView().getWidth();
240 | mHeight = getContentView().getHeight();
241 | //只获取宽高时,不执行更新操作
242 | if (isOnlyGetWH) {
243 | removeGlobalLayoutListener();
244 | return;
245 | }
246 | updateLocation(mWidth, mHeight, mAnchorView, mVerticalGravity, mHorizontalGravity, mOffsetX, mOffsetY);
247 | removeGlobalLayoutListener();
248 | }
249 | };
250 |
251 | private void updateLocation(int width, int height, @NonNull View anchor,
252 | @VerticalPosition final int verticalGravity,
253 | @HorizontalPosition int horizontalGravity,
254 | int x, int y) {
255 | x = calculateX(anchor, horizontalGravity, width, x);
256 | y = calculateY(anchor, verticalGravity, height, y);
257 | update(anchor, x, y, width, height);
258 | }
259 |
260 | private void removeGlobalLayoutListener() {
261 | if (getContentView() != null) {
262 | if (Build.VERSION.SDK_INT >= 16) {
263 | getContentView().getViewTreeObserver().removeOnGlobalLayoutListener(mOnGlobalLayoutListener);
264 | } else {
265 | getContentView().getViewTreeObserver().removeGlobalOnLayoutListener(mOnGlobalLayoutListener);
266 | }
267 | }
268 | }
269 |
270 | private void addGlobalLayoutListener(View contentView) {
271 | contentView.getViewTreeObserver().addOnGlobalLayoutListener(mOnGlobalLayoutListener);
272 | }
273 |
274 | @Override
275 | public void dismiss() {
276 | super.dismiss();
277 | dismissBackgroundAnimator();
278 | removeGlobalLayoutListener();
279 | }
280 |
281 | /**
282 | * 窗口显示,窗口背景透明度渐变动画
283 | */
284 | private void showBackgroundAnimator() {
285 | if (mAlpha >= 1f) return;
286 | ValueAnimator animator = ValueAnimator.ofFloat(1.0f, mAlpha);
287 | animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
288 |
289 | @Override
290 | public void onAnimationUpdate(ValueAnimator animation) {
291 | float alpha = (float) animation.getAnimatedValue();
292 | setWindowBackgroundAlpha(alpha);
293 | }
294 | });
295 | animator.setDuration(360);
296 | animator.start();
297 | }
298 |
299 | /**
300 | * 窗口隐藏,窗口背景透明度渐变动画
301 | */
302 | private void dismissBackgroundAnimator() {
303 | if (mAlpha >= 1f) return;
304 | ValueAnimator animator = ValueAnimator.ofFloat(mAlpha, 1.0f);
305 | animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
306 |
307 | @Override
308 | public void onAnimationUpdate(ValueAnimator animation) {
309 | float alpha = (float) animation.getAnimatedValue();
310 | setWindowBackgroundAlpha(alpha);
311 | }
312 | });
313 | animator.setDuration(360);
314 | animator.start();
315 | }
316 |
317 | /**
318 | * 控制窗口背景的不透明度
319 | */
320 | private void setWindowBackgroundAlpha(float alpha) {
321 | if (mContext == null) return;
322 | if (mContext instanceof Activity) {
323 | Window window = ((Activity) mContext).getWindow();
324 | WindowManager.LayoutParams layoutParams = window.getAttributes();
325 | layoutParams.alpha = alpha;
326 | window.setAttributes(layoutParams);
327 | }
328 | }
329 |
330 | public static class Builder {
331 | private SmartPopupWindow mWindow;
332 |
333 | private Builder(Activity activity, View view) {
334 | mWindow = new SmartPopupWindow(activity);
335 | mWindow.mContext = activity;
336 | mWindow.mContentView = view;
337 | }
338 |
339 | public static Builder build(Activity activity, View view) {
340 | return new Builder(activity, view);
341 | }
342 |
343 | public Builder setSize(int width, int height) {
344 | mWindow.mWidth = width;
345 | mWindow.mHeight = height;
346 | return this;
347 | }
348 |
349 | public Builder setAnimationStyle(int animationStyle) {
350 | mWindow.mAnimationStyle = animationStyle;
351 | return this;
352 | }
353 |
354 | public Builder setAlpha(float alpha) {
355 | mWindow.mAlpha = alpha;
356 | return this;
357 | }
358 |
359 | public Builder setOutsideTouchDismiss(boolean dismiss) {
360 | mWindow.isTouchOutsideDismiss = dismiss;
361 | return this;
362 | }
363 |
364 | /**
365 | * 创建PopupWindow
366 | * @return
367 | */
368 | public SmartPopupWindow createPopupWindow() {
369 | mWindow.init();
370 | return mWindow;
371 | }
372 | }
373 | }
374 |
375 |
--------------------------------------------------------------------------------
/smartpopupwindow/src/main/java/com/gavin/com/smartpopupwindow/VerticalPosition.java:
--------------------------------------------------------------------------------
1 | package com.gavin.com.smartpopupwindow;
2 |
3 | import android.support.annotation.IntDef;
4 |
5 | import java.lang.annotation.Retention;
6 | import java.lang.annotation.RetentionPolicy;
7 |
8 | /**
9 | * Created by gavin
10 | * Created date 17/9/13
11 | */
12 |
13 | @IntDef({
14 | VerticalPosition.CENTER,
15 | VerticalPosition.ABOVE,
16 | VerticalPosition.BELOW,
17 | VerticalPosition.ALIGN_TOP,
18 | VerticalPosition.ALIGN_BOTTOM,
19 | })
20 | @Retention(RetentionPolicy.SOURCE)
21 | public @interface VerticalPosition {
22 | int CENTER = 0;
23 | int ABOVE = 1;
24 | int BELOW = 2;
25 | int ALIGN_TOP = 3;
26 | int ALIGN_BOTTOM = 4;
27 | }
28 |
--------------------------------------------------------------------------------
/smartpopupwindow/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | SmartPopupWindow
3 |
4 |
--------------------------------------------------------------------------------