我可以将活动转换为浮动窗口吗?

Posted

技术标签:

【中文标题】我可以将活动转换为浮动窗口吗?【英文标题】:can i convert an activity into a floating window? 【发布时间】:2019-02-08 04:01:21 【问题描述】:

我知道浮动窗口是服务...但是有没有办法将活动/片段转换为浮动窗口?

我的 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_
android:layout_
xmlns:app="http://schemas.android.com/apk/res-auto">


<android.support.design.widget.AppBarLayout
    android:id="@+id/actionBar"
    android:layout_
    android:layout_
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_
        android:layout_
        android:background="@color/black">

        <TextView
            android:textColor="@color/white"
            android:textSize="20dp"
            android:text="Customise"
            android:layout_
            android:layout_ />


    </android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>

<ScrollView
    android:layout_below="@+id/actionBar"
    android:layout_
    android:layout_
    android:background="@color/black"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <LinearLayout
        android:layout_below="@+id/actionBar"
        android:layout_
        android:layout_
        android:orientation="vertical">

        <RelativeLayout
            android:id="@+id/color"
            android:paddingVertical="10dp"
            android:paddingHorizontal="20dp"
            android:layout_
            android:layout_>
        <TextView
            android:textSize="25dp"
            android:layout_centerVertical="true"
            android:textColor="@color/white"
            android:text="Color"
            android:layout_
            android:layout_ />
    </RelativeLayout>

        <RelativeLayout
            android:id="@+id/font"
            android:paddingVertical="10dp"
            android:paddingHorizontal="20dp"
            android:layout_
            android:layout_>
            <TextView
                android:textSize="25dp"
                android:layout_centerVertical="true"
                android:textColor="@color/white"
                android:text="Font"
                android:layout_
                android:layout_ />
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/size"
            android:paddingVertical="10dp"
            android:paddingHorizontal="20dp"
            android:layout_
            android:layout_>
            <TextView
                android:textSize="25dp"
                android:layout_centerVertical="true"
                android:textColor="@color/white"
                android:text="Size"
                android:layout_
                android:layout_ />
        </RelativeLayout>
</LinearLayout>
</ScrollView>
</RelativeLayout>

这是我项目中最简单的活动,所以尝试了这个

【问题讨论】:

AFAIK 你不能,但你可以使用 acitvity's xml 作为浮动窗口,使用服务,也可以在 UI 上执行操作 我该怎么做?? @AbdulKawee 检查下面的答案:) 您的要求是什么? 你能在这里张贴activity_customize布局吗 【参考方案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

希望这能解决您的问题

【讨论】:

myview 未定义...什么是 myview? 遇到错误...请查看我编辑的问题 你的activity_customize布局有问题,贴出来 完成...请检查 是的...错误不能与布局有关,因为我尝试了许多布局并得到相同的错误【参考方案2】:

下面的代码将启动一个带有布局的服务

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

【讨论】:

whats d 导致在线上出现 inflate 异常的原因......inflate(R.layout.abc,null)..... @MichelleKinsten 您必须创建一个布局。然后充气。 我可以从我膨胀的布局中实现 onClicklisteners 吗???

以上是关于我可以将活动转换为浮动窗口吗?的主要内容,如果未能解决你的问题,请参考以下文章

1.2 Illustrator多文档的几种排列方式

使用 SwiftUI 更改 macOS 窗口级别(制作浮动窗口)

MFC DockablePane 不浮动或隐藏

如何使用 android 活动转换为浮动操作按钮设置动画?

怎么取消DIV 浮动?

苹果mac系统如何查看cpu占用率?