Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
james正哥儿
testforfloatingwindow
提交
e153b798
T
testforfloatingwindow
项目概览
james正哥儿
/
testforfloatingwindow
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
T
testforfloatingwindow
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
e153b798
编写于
5月 30, 2018
作者:
董
董重
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
动画播放悬浮窗口
上级
5e4633f3
变更
10
隐藏空白更改
内联
并排
Showing
10 changed file
with
145 addition
and
11 deletion
+145
-11
app/src/main/java/dongzhong/testforfloatingwindow/FloatingButtonService.java
...ongzhong/testforfloatingwindow/FloatingButtonService.java
+6
-7
app/src/main/java/dongzhong/testforfloatingwindow/FloatingImageDisplayService.java
...ng/testforfloatingwindow/FloatingImageDisplayService.java
+116
-0
app/src/main/java/dongzhong/testforfloatingwindow/MainActivity.java
...in/java/dongzhong/testforfloatingwindow/MainActivity.java
+8
-2
app/src/main/res/drawable/image_01.jpg
app/src/main/res/drawable/image_01.jpg
+0
-0
app/src/main/res/drawable/image_02.jpg
app/src/main/res/drawable/image_02.jpg
+0
-0
app/src/main/res/drawable/image_03.jpg
app/src/main/res/drawable/image_03.jpg
+0
-0
app/src/main/res/drawable/image_04.jpg
app/src/main/res/drawable/image_04.jpg
+0
-0
app/src/main/res/drawable/image_05.jpg
app/src/main/res/drawable/image_05.jpg
+0
-0
app/src/main/res/layout/activity_main.xml
app/src/main/res/layout/activity_main.xml
+2
-2
app/src/main/res/layout/image_display.xml
app/src/main/res/layout/image_display.xml
+13
-0
未找到文件。
app/src/main/java/dongzhong/testforfloatingwindow/FloatingButtonService.java
浏览文件 @
e153b798
...
...
@@ -8,7 +8,6 @@ import android.os.Build;
import
android.os.IBinder
;
import
android.provider.Settings
;
import
android.support.annotation.Nullable
;
import
android.util.Log
;
import
android.view.Gravity
;
import
android.view.MotionEvent
;
import
android.view.View
;
...
...
@@ -20,6 +19,8 @@ import android.widget.Button;
*/
public
class
FloatingButtonService
extends
Service
{
public
static
boolean
isStarted
=
false
;
private
WindowManager
windowManager
;
private
WindowManager
.
LayoutParams
layoutParams
;
...
...
@@ -28,6 +29,7 @@ public class FloatingButtonService extends Service {
@Override
public
void
onCreate
()
{
super
.
onCreate
();
isStarted
=
true
;
windowManager
=
(
WindowManager
)
getSystemService
(
WINDOW_SERVICE
);
layoutParams
=
new
WindowManager
.
LayoutParams
();
if
(
Build
.
VERSION
.
SDK_INT
>
Build
.
VERSION_CODES
.
O
)
{
...
...
@@ -38,7 +40,7 @@ public class FloatingButtonService extends Service {
layoutParams
.
format
=
PixelFormat
.
RGBA_8888
;
layoutParams
.
gravity
=
Gravity
.
LEFT
|
Gravity
.
TOP
;
layoutParams
.
flags
=
WindowManager
.
LayoutParams
.
FLAG_NOT_TOUCH_MODAL
|
WindowManager
.
LayoutParams
.
FLAG_NOT_FOCUSABLE
;
layoutParams
.
width
=
1
00
;
layoutParams
.
width
=
5
00
;
layoutParams
.
height
=
100
;
layoutParams
.
x
=
300
;
layoutParams
.
y
=
300
;
...
...
@@ -57,12 +59,10 @@ public class FloatingButtonService extends Service {
}
private
void
showFloatingWindow
()
{
button
=
new
Button
(
getApplicationContext
());
if
(
Settings
.
canDrawOverlays
(
this
))
{
button
=
new
Button
(
getApplicationContext
());
button
.
setText
(
"Floating Window"
);
button
.
setBackgroundColor
(
Color
.
BLUE
);
button
.
setWidth
(
100
);
button
.
setHeight
(
100
);
windowManager
.
addView
(
button
,
layoutParams
);
button
.
setOnTouchListener
(
new
FloatingOnTouchListener
());
...
...
@@ -85,12 +85,11 @@ public class FloatingButtonService extends Service {
int
nowY
=
(
int
)
event
.
getRawY
();
int
movedX
=
nowX
-
x
;
int
movedY
=
nowY
-
y
;
Log
.
d
(
"悬浮窗"
,
"movedX = "
+
movedX
+
", movedY ="
+
movedY
);
x
=
nowX
;
y
=
nowY
;
layoutParams
.
x
=
layoutParams
.
x
+
movedX
;
layoutParams
.
y
=
layoutParams
.
y
+
movedY
;
windowManager
.
updateViewLayout
(
button
,
layoutParams
);
windowManager
.
updateViewLayout
(
view
,
layoutParams
);
break
;
default
:
break
;
...
...
app/src/main/java/dongzhong/testforfloatingwindow/FloatingImageDisplayService.java
浏览文件 @
e153b798
...
...
@@ -2,21 +2,137 @@ package dongzhong.testforfloatingwindow;
import
android.app.Service
;
import
android.content.Intent
;
import
android.graphics.PixelFormat
;
import
android.os.Build
;
import
android.os.Handler
;
import
android.os.IBinder
;
import
android.os.Message
;
import
android.provider.Settings
;
import
android.support.annotation.Nullable
;
import
android.view.Gravity
;
import
android.view.LayoutInflater
;
import
android.view.MotionEvent
;
import
android.view.View
;
import
android.view.WindowManager
;
import
android.widget.ImageView
;
/**
* Created by dongzhong on 2018/5/30.
*/
public
class
FloatingImageDisplayService
extends
Service
{
public
static
boolean
isStarted
=
false
;
private
WindowManager
windowManager
;
private
WindowManager
.
LayoutParams
layoutParams
;
private
View
displayView
;
private
int
[]
images
;
private
int
imageIndex
=
0
;
private
Handler
changeImageHandler
;
@Override
public
void
onCreate
()
{
super
.
onCreate
();
isStarted
=
true
;
windowManager
=
(
WindowManager
)
getSystemService
(
WINDOW_SERVICE
);
layoutParams
=
new
WindowManager
.
LayoutParams
();
if
(
Build
.
VERSION
.
SDK_INT
>
Build
.
VERSION_CODES
.
O
)
{
layoutParams
.
type
=
WindowManager
.
LayoutParams
.
TYPE_APPLICATION_OVERLAY
;
}
else
{
layoutParams
.
type
=
WindowManager
.
LayoutParams
.
TYPE_PHONE
;
}
layoutParams
.
format
=
PixelFormat
.
RGBA_8888
;
layoutParams
.
gravity
=
Gravity
.
LEFT
|
Gravity
.
TOP
;
layoutParams
.
flags
=
WindowManager
.
LayoutParams
.
FLAG_NOT_TOUCH_MODAL
|
WindowManager
.
LayoutParams
.
FLAG_NOT_FOCUSABLE
;
layoutParams
.
width
=
500
;
layoutParams
.
height
=
500
;
layoutParams
.
x
=
300
;
layoutParams
.
y
=
300
;
images
=
new
int
[]
{
R
.
drawable
.
image_01
,
R
.
drawable
.
image_02
,
R
.
drawable
.
image_03
,
R
.
drawable
.
image_04
,
R
.
drawable
.
image_05
,
};
changeImageHandler
=
new
Handler
(
this
.
getMainLooper
(),
changeImageCallback
);
}
@Nullable
@Override
public
IBinder
onBind
(
Intent
intent
)
{
return
null
;
}
@Override
public
int
onStartCommand
(
Intent
intent
,
int
flags
,
int
startId
)
{
showFloatingWindow
();
return
super
.
onStartCommand
(
intent
,
flags
,
startId
);
}
private
void
showFloatingWindow
()
{
if
(
Settings
.
canDrawOverlays
(
this
))
{
LayoutInflater
layoutInflater
=
LayoutInflater
.
from
(
this
);
displayView
=
layoutInflater
.
inflate
(
R
.
layout
.
image_display
,
null
);
displayView
.
setOnTouchListener
(
new
FloatingOnTouchListener
());
ImageView
imageView
=
displayView
.
findViewById
(
R
.
id
.
image_display_imageview
);
imageView
.
setImageResource
(
images
[
imageIndex
]);
windowManager
.
addView
(
displayView
,
layoutParams
);
changeImageHandler
.
sendEmptyMessageDelayed
(
0
,
2000
);
}
}
private
Handler
.
Callback
changeImageCallback
=
new
Handler
.
Callback
()
{
@Override
public
boolean
handleMessage
(
Message
msg
)
{
if
(
msg
.
what
==
0
)
{
imageIndex
++;
if
(
imageIndex
>=
5
)
{
imageIndex
=
0
;
}
if
(
displayView
!=
null
)
{
((
ImageView
)
displayView
.
findViewById
(
R
.
id
.
image_display_imageview
)).
setImageResource
(
images
[
imageIndex
]);
}
windowManager
.
updateViewLayout
(
displayView
,
layoutParams
);
changeImageHandler
.
sendEmptyMessageDelayed
(
0
,
2000
);
}
return
false
;
}
};
private
class
FloatingOnTouchListener
implements
View
.
OnTouchListener
{
private
int
x
;
private
int
y
;
@Override
public
boolean
onTouch
(
View
view
,
MotionEvent
event
)
{
switch
(
event
.
getAction
())
{
case
MotionEvent
.
ACTION_DOWN
:
x
=
(
int
)
event
.
getRawX
();
y
=
(
int
)
event
.
getRawY
();
break
;
case
MotionEvent
.
ACTION_MOVE
:
int
nowX
=
(
int
)
event
.
getRawX
();
int
nowY
=
(
int
)
event
.
getRawY
();
int
movedX
=
nowX
-
x
;
int
movedY
=
nowY
-
y
;
x
=
nowX
;
y
=
nowY
;
layoutParams
.
x
=
layoutParams
.
x
+
movedX
;
layoutParams
.
y
=
layoutParams
.
y
+
movedY
;
windowManager
.
updateViewLayout
(
view
,
layoutParams
);
break
;
default
:
break
;
}
return
false
;
}
}
}
app/src/main/java/dongzhong/testforfloatingwindow/MainActivity.java
浏览文件 @
e153b798
...
...
@@ -29,6 +29,9 @@ public class MainActivity extends AppCompatActivity {
}
public
void
startFloatingButtonService
(
View
view
)
{
if
(
FloatingButtonService
.
isStarted
)
{
return
;
}
if
(!
Settings
.
canDrawOverlays
(
this
))
{
Toast
.
makeText
(
this
,
"当前无权限,请授权"
,
Toast
.
LENGTH_SHORT
);
startActivityForResult
(
new
Intent
(
Settings
.
ACTION_MANAGE_OVERLAY_PERMISSION
,
Uri
.
parse
(
"package:"
+
getPackageName
())),
1111
);
...
...
@@ -37,12 +40,15 @@ public class MainActivity extends AppCompatActivity {
}
}
public
void
startFloatingLayoutService
(
View
view
)
{
public
void
startFloatingImageDisplayService
(
View
view
)
{
if
(
FloatingImageDisplayService
.
isStarted
)
{
return
;
}
if
(!
Settings
.
canDrawOverlays
(
this
))
{
Toast
.
makeText
(
this
,
"当前无权限,请授权"
,
Toast
.
LENGTH_SHORT
);
startActivityForResult
(
new
Intent
(
Settings
.
ACTION_MANAGE_OVERLAY_PERMISSION
,
Uri
.
parse
(
"package:"
+
getPackageName
())),
1111
);
}
else
{
startService
(
new
Intent
(
MainActivity
.
this
,
Floating
Button
Service
.
class
));
startService
(
new
Intent
(
MainActivity
.
this
,
Floating
ImageDisplay
Service
.
class
));
}
}
}
app/src/main/res/drawable/image_01.jpg
0 → 100644
浏览文件 @
e153b798
67.5 KB
app/src/main/res/drawable/image_02.jpg
0 → 100644
浏览文件 @
e153b798
214.4 KB
app/src/main/res/drawable/image_03.jpg
0 → 100644
浏览文件 @
e153b798
138.8 KB
app/src/main/res/drawable/image_04.jpg
0 → 100644
浏览文件 @
e153b798
292.5 KB
app/src/main/res/drawable/image_05.jpg
0 → 100644
浏览文件 @
e153b798
100.2 KB
app/src/main/res/layout/activity_main.xml
浏览文件 @
e153b798
...
...
@@ -14,7 +14,7 @@
<Button
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:text=
"start floating
layout
"
android:onClick=
"startFloating
Layout
Service"
/>
android:text=
"start floating
image display
"
android:onClick=
"startFloating
ImageDisplay
Service"
/>
</LinearLayout>
\ No newline at end of file
app/src/main/res/layout/image_display.xml
0 → 100644
浏览文件 @
e153b798
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android=
"http://schemas.android.com/apk/res/android"
android:layout_width=
"match_parent"
android:layout_height=
"match_parent"
android:orientation=
"vertical"
>
<ImageView
android:id=
"@+id/image_display_imageview"
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
/>
</LinearLayout>
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录