0

是否可以在其他应用程序之上画一条线?

例如,有一个显示地图的应用程序,我想在其他应用程序的顶部画一条线,例如从 A 到 B 的路线。

我添加了权限

android.permission.SYSTEM_ALERT_WINDOW

并添加代码以在其他应用程序之上显示视图。

       super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final WindowManager.LayoutParams param=new WindowManager.LayoutParams();
    param.flags=WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
    final View view=findViewById(R.id.my_floating_view);
    final ViewGroup parent=(ViewGroup)view.getParent();
    if(parent!=null)
      parent.removeView(view);
    param.format=PixelFormat.RGBA_8888;
    param.type=WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
    param.gravity=Gravity.CENTER;
    param.width=parent!=null?LayoutParams.WRAP_CONTENT:view.getLayoutParams().width;
    param.height=parent!=null?LayoutParams.WRAP_CONTENT:view.getLayoutParams().height;
    final WindowManager wmgr=(WindowManager)getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
    wmgr.addView(view,param);
}

我的问题是,当我打开我的应用程序时,我看到了整个活动,并且我希望隐藏活动并且只看到位于另一个应用程序之上的视图。即在我的地图应用程序示例中,我想查看地图并在其顶部查看我绘制的线条。我不想看到我的应用程序的名称等。

我怎样才能做到这一点?

4

1 回答 1

0

基本上,这里解释了 Facebook 如何让他们的 Chathead 应用程序浮动在其他应用程序之上,并且您必须将其作为服务运行。我遵循了一个示例,该示例使图像图标浮动在其他所有内容之上。这是示例中的代码。这不是您想要的确切答案,但它应该会有所帮助,并且您可以将代码调整为您需要做的事情。您从正确的方向开始,我希望这将有助于缩小您需要的绝对方向。

MainActivity.java

package com.example.floatingicon;

import com.example.floatingicon.R;
import com.example.floatingicon.MainService;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity
{
    @Override
    protected void onCreate(Bundle icicle){
        super.onCreate(icicle);
        setContentView(R.layout.main);

        Bundle bund = getIntent().getExtras();

        if(bund != null && bun.getString("LAUNCH").equals("YES")){
            startService(new Intent(MainActivity.this, MainService.class));
        }

        Button start = (Button)findViewById(R.id.btnStart);
        start.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v){
                startService(new Intent(MainActivity.this, MainService.class));
            }
        });

        Button stop = (Button)findViewById(R.id.btnStop);
        stop.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v){
                stopService(new Intent(MainActivity.this, MainService.class));
            }
        });
    }

    @Override
    protected void onResume(){
        Bundle bund = getIntent().getExtras();

        if(bund != null && bund.getString("LAUNCH").equals("YES")){
            startService(new Intent(MainActivity.this, MainService.class));
        }
        super.onResume();
    }
}

主服务.java

package com.example.floatingicon;

import com.example.floatingicon.R;
import android.app.Service;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.IBinder;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;

public class MainService extends Service {

    private WindowManager windowManager;
    private ImageView floatIcon;

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();

        windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

        floatIcon = new ImageView(this);

        floatIcon.setImageResource(R.drawable.floating);

        final 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.TOP | Gravity.LEFT;
        params.x = 0;
        params.y = 100;

        windowManager.addView(floatIcon, params);

        try {
            floatIcon.setOnTouchListener(new View.OnTouchListener() {
                private WindowManager.LayoutParams paramsF = params;
                private int initialX;
                private int initialY;
                private float initialTouchX;
                private float initialTouchY;

                @Override 
                public boolean onTouch(View v, MotionEvent event) {
                    switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        // Get current time in nano seconds.
                        initialX = paramsF.x;
                        initialY = paramsF.y;
                        initialTouchX = event.getRawX();
                        initialTouchY = event.getRawY();
                        break;
                    case MotionEvent.ACTION_UP:
                        break;
                    case MotionEvent.ACTION_MOVE:
                        paramsF.x = initialX + (int) (event.getRawX() - initialTouchX);
                        paramsF.y = initialY + (int) (event.getRawY() - initialTouchY);
                        windowManager.updateViewLayout(floatIcon, paramsF);
                        break;
                    }
                    return false;
                }
            });
        } catch (Exception e) {
            // TODO: handle exception
        }

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (floatIcon != null) windowManager.removeView(floatIcon);
    }
}

主要的.xml

<?xml version="1.0" encoding="utf-8" ?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/btnStart"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="100dp"
        android:text="@string/start" />
    <Button
        android:id="@+id/btnStop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@id/btnStart"
        android:layout_alignRight="@id/btnStart"
        android:layout_below="@id/btnStart"
        android:layout_marginTop="10dp"
        android:text="@string/stop" />
</RelativeLayout>

这应该可以帮助您开始,除非您已经完成了所有这些时间。

于 2014-05-14T21:11:04.867 回答