借鉴其他Android应用程序的顶部线条 [英] Draw lines on top of another android application

查看:218
本文介绍了借鉴其他Android应用程序的顶部线条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以借鉴其它应用程序之上的线?

Is it possible to draw a line on top of other app?

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

For example, there is an app that shows a map, and I want to draw a line on top of that other application, for example a route from A to B.

我添加权限

android.permission.SYSTEM_ALERT_WINDOW

android.permission.SYSTEM_ALERT_WINDOW

和添加code,以显示在其他应用程序的顶部视图。

And added code to show the view on top of other app.

       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);
}

我的问题是,当我打开我的应用我看到了整个活动,我想的活动被隐藏,只看到该视图的是另一个应用程序的顶部。
即在我的地图应用的例子,我想看到​​的地图,在它上面看到我画的线条。我不希望看到我的应用程序等的名称。

My problem is that when i open my app I see the entire activity, and I want the activity to be hidden and to see only the view the is on top of another application. i.e. in my example of the map application, I want to see the map and on top of it to see the lines that I draw. I don't want to see the name of my application,etc.

我怎么能这样做?

推荐答案

基本上,这是这里解释的Facebook如何使上述以外的应用程序,并且您有运行它作为一个服务的Chathead应用浮动。我跟着一个例如,使图像图标浮高于一切。下面是从例如code。这不是一个确切的答案你想要什么,但它应该帮助,你可以调整code到你需要做了什么。你在正确的方向开始了,我希望这将有助于缩小你需要的绝对方向。

Basically, it was explained here how Facebook made their Chathead app float above other apps and that you have to run it as a service. I followed an example that makes an image icon float above everything else. Here is the code from the example. It is not an exact answer to what you want, but it should help, and you can tweak the code to what you need to have done. You started off in the right direction and i hope this will help narrow that absolute direction you need.

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();
    }
}

MainService.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);
    }
}

的main.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>

这应该帮助您开始,除非你毕竟这个时候已经完成了。

This should help you get started, unless you have already finished after all of this time.

这篇关于借鉴其他Android应用程序的顶部线条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆