提交 603f233a 编写于 作者: D dongzhong1990

增加视频播放悬浮窗

上级 5da5fdb1
......@@ -25,4 +25,6 @@ dependencies {
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.github.dongzhong:VideoPlayer:v1.2'
}
......@@ -3,6 +3,7 @@
package="dongzhong.testforfloatingwindow">
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
......@@ -20,6 +21,7 @@
</activity>
<service android:name=".FloatingButtonService"></service>
<service android:name=".FloatingImageDisplayService"></service>
<service android:name=".FloatingVideoService"></service>
</application>
</manifest>
\ No newline at end of file
package dongzhong.testforfloatingwindow;
import android.app.Service;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.Build;
import android.os.IBinder;
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 dongzhong.videoplayer.VideoPlayer;
/**
* Created by admin on 2018/5/30.
*/
public class FloatingVideoService extends Service {
public static boolean isStarted = false;
private WindowManager windowManager;
private WindowManager.LayoutParams layoutParams;
private View displayView;
@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 = 800;
layoutParams.height = 450;
layoutParams.x = 300;
layoutParams.y = 300;
}
@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.video_display, null);
VideoPlayer videoPlayer = displayView.findViewById(R.id.videoplayer_display);
videoPlayer.preset("https://raw.githubusercontent.com/dongzhong/ImageAndVideoStore/master/Bruno%20Mars%20-%20Treasure.mp4",
"Treasure - Bruno Mars", true);
windowManager.addView(displayView, layoutParams);
displayView.setOnTouchListener(new FloatingOnTouchListener());
}
}
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;
}
}
}
......@@ -32,6 +32,13 @@ public class MainActivity extends AppCompatActivity {
Toast.makeText(this, "授权成功", Toast.LENGTH_SHORT).show();
startService(new Intent(MainActivity.this, FloatingImageDisplayService.class));
}
} else if (requestCode == 2) {
if (!Settings.canDrawOverlays(this)) {
Toast.makeText(this, "授权失败", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "授权成功", Toast.LENGTH_SHORT).show();
startService(new Intent(MainActivity.this, FloatingVideoService.class));
}
}
}
......@@ -58,4 +65,16 @@ public class MainActivity extends AppCompatActivity {
startService(new Intent(MainActivity.this, FloatingImageDisplayService.class));
}
}
public void startFloatingVideoService(View view) {
if (FloatingVideoService.isStarted) {
return;
}
if (!Settings.canDrawOverlays(this)) {
Toast.makeText(this, "当前无权限,请授权", Toast.LENGTH_SHORT);
startActivityForResult(new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + getPackageName())), 2);
} else {
startService(new Intent(MainActivity.this, FloatingVideoService.class));
}
}
}
......@@ -17,4 +17,10 @@
android:text="start floating image display"
android:onClick="startFloatingImageDisplayService"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="start floating video player"
android:onClick="startFloatingVideoService"/>
</LinearLayout>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<dongzhong.videoplayer.VideoPlayer
android:id="@+id/videoplayer_display"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</FrameLayout>
\ No newline at end of file
......@@ -19,6 +19,7 @@ allprojects {
repositories {
google()
jcenter()
maven { url 'https://jitpack.io' }
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册