我知道浮动窗口是服务...但是有办法将活动/片段转换为浮动窗口吗?
我的FloatingWindow示例代码
public class FloatingWindow extends Service{
WindowManager wm;
LinearLayout ll;
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
wm = (WindowManager) getSystemService(WINDOW_SERVICE);
ll = new LinearLayout(this);
ll.setBackgroundColor(Color.RED);
LinearLayout.LayoutParams layoutParameteres = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, 400);
ll.setBackgroundColor(Color.argb(66,255,0,0));
ll.setLayoutParams(layoutParameteres);
final WindowManager.LayoutParams parameters = new WindowManager.LayoutParams(
500, 200, WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
parameters.gravity = Gravity.CENTER | Gravity.CENTER;
parameters.x = 0;
parameters.y = 0;
Button stop = new Button(this);
stop.setText("Stop");
ViewGroup.LayoutParams btnParameters = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
stop.setLayoutParams(btnParameters);
ll.addView(stop);
wm.addView(ll, parameters);
ll.setOnTouchListener(new View.OnTouchListener() {
WindowManager.LayoutParams updatedParameters = parameters;
double x;
double y;
double pressedX;
double pressedY;
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
x = updatedParameters.x;
y = updatedParameters.y;
pressedX = event.getRawX();
pressedY = event.getRawY();
break;
case MotionEvent.ACTION_MOVE:
updatedParameters.x = (int) (x + (event.getRawX() - pressedX));
updatedParameters.y = (int) (y + (event.getRawY() - pressedY));
wm.updateViewLayout(ll, updatedParameters);
default:
break;
}
return false;
}
});
stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
wm.removeView(ll);
stopSelf();
System.exit(0);
}
});
}
@Override
public void onDestroy() {
super.onDestroy();
stopSelf();
}
所以我有一个活动,我想将其转换为浮动窗口服务……这可能以某种方式吗?请帮帮我...谢谢...
错误
在线-myview = li.inflate(R.layout.activity_customize, null); //
这里的布局
XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.design.widget.AppBarLayout
android:id="@+id/actionBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/black">
<TextView
android:textColor="@color/white"
android:textSize="20dp"
android:text="Customise"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<ScrollView
android:layout_below="@+id/actionBar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/black"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<LinearLayout
android:layout_below="@+id/actionBar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:id="@+id/color"
android:paddingVertical="10dp"
android:paddingHorizontal="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:textSize="25dp"
android:layout_centerVertical="true"
android:textColor="@color/white"
android:text="Color"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/font"
android:paddingVertical="10dp"
android:paddingHorizontal="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:textSize="25dp"
android:layout_centerVertical="true"
android:textColor="@color/white"
android:text="Font"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/size"
android:paddingVertical="10dp"
android:paddingHorizontal="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:textSize="25dp"
android:layout_centerVertical="true"
android:textColor="@color/white"
android:text="Size"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
</LinearLayout>
</ScrollView>
</RelativeLayout>
这是我项目中最简单的活动,因此对此进行了尝试
答案 0 :(得分:1)
您必须在service
上填充UI
View myview;
li= (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
wm = (WindowManager) getSystemService(WINDOW_SERVICE);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
//WindowManager.LayoutParams.TYPE_INPUT_METHOD |
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,// | WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
PixelFormat.TRANSLUCENT);
/////////////////////////Another params
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT)
params.gravity = Gravity.RIGHT | Gravity.TOP;
myview = li.inflate(R.layout.xyz, null); // your layout here
wm.addView(myview, params);
用于添加一些cliclListener
ImageView playButton = (ImageView) myview.findViewById(R.id.play_btn);
playButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(FloatingViewService.this, "Playing the song.", Toast.LENGTH_LONG).show();
}
});
//change this as your per your requirement
希望这可以解决您的问题
答案 1 :(得分:0)
下面的代码将启动具有布局的服务
val layoutInflater = LayoutInflater.from(applicationContext)
if (layoutInflater != null) {
view = layoutInflater.inflate(R.layout.service_floating_windows, null, false)
wm!!.addView(view, parameters)
touchMovementOfTheFloatingWindow(parameters)
}
设计要显示的布局。然后从任何地方启动服务
class Asd : Service() {
private var wm: WindowManager? = null
private var view: View? = null
override fun onBind(intent: Intent): IBinder? {
return null
}
override fun onCreate() {
super.onCreate()
wm = getSystemService(Context.WINDOW_SERVICE) as WindowManager
val parameters: WindowManager.LayoutParams
val minHW = U.getScreenMinHW(this)
parameters = if (Build.VERSION.SDK_INT > 25) {
WindowManager.LayoutParams(
minHW * 2 / 3, minHW * 2 / 3,
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT)
} else {
WindowManager.LayoutParams(
minHW * 2 / 3, minHW * 2 / 3,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT)
}
parameters.gravity = Gravity.CENTER
parameters.x = 0
parameters.y = 100
val layoutInflater = LayoutInflater.from(applicationContext)
if (layoutInflater != null) {
view = layoutInflater.inflate(R.layout.service_floating_windows, null, false)
wm!!.addView(view, parameters)
touchMovementOfTheFloatingWindow(parameters)
}
}
private fun touchMovementOfTheFloatingWindow(parameters: WindowManager.LayoutParams) {
view!!.setOnTouchListener(object : View.OnTouchListener {
internal var updatedParameters = parameters
internal var x: Double = 0.toDouble()
internal var y: Double = 0.toDouble()
internal var pressedX: Double = 0.toDouble()
internal var pressedY: Double = 0.toDouble()
override fun onTouch(v: View, event: MotionEvent): Boolean {
when (event.action) {
MotionEvent.ACTION_DOWN -> {
x = updatedParameters.x.toDouble()
y = updatedParameters.y.toDouble()
pressedX = event.rawX.toDouble()
pressedY = event.rawY.toDouble()
}
MotionEvent.ACTION_MOVE -> {
updatedParameters.x = (x + (event.rawX - pressedX)).toInt()
updatedParameters.y = (y + (event.rawY - pressedY)).toInt()
wm!!.updateViewLayout(view, updatedParameters)
}
else -> {
}
}
return false
}
})
}
override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
return Service.START_NOT_STICKY
}}