如何在其他应用程序上方显示代码绘制的线条? [英] How can I display a code drawn line over other apps?
问题描述
详细信息:我想添加一条代码绘制的线(就像您使用画布/绘画进行绘制一样),并将其显示在其他应用程序上.
Details: I would like to add a code drawn line (like you draw with canvas/paint) and display it over other applications.
我目前有一个应用程序,可以让我在其他应用程序上显示图像.有关我的代码,请参见我的 Stackoverflow答案.我已经实现了这样做的权限:
I currently have an application that allows me to display an image over other application. See my Stackoverflow answer for my code. I have implemented the permission to do so:
android.permission.SYSTEM_ALERT_WINDOW
我尝试将画布/绘画添加到WindowManager中而没有错误,但是我的应用程序崩溃了.我曾尝试寻找答案,但已空了.
I have tried to add a canvas/paint to the WindowManager without errors, but my app crashed. I have tried searching for an answer, but have turned up empty.
如果有人可以帮助我解决这个问题,将不胜感激!
If anyone can help me figure this out, you will be greatly appreciated!
为了阐明这一点,我希望可以在可以在其他应用程序上使用它并且仍可以与其他应用程序进行交互的位置绘制此图.另外,我还添加了按钮功能,可以使用WindowManager addView和WindowManager removeView关闭/打开视图,但是每次添加视图时,它都覆盖了整个区域,因此我无法再单击按钮来删除视图./p>
To clarify: I would like this to be drawn where I can have it over other applications and where I can still interact with other applications. Also I've added a button functionality where I turn off/on a view with WindowManager addView and WindowManager removeView, but every time I add a view, it covers the entire area and I can no longer click the button to remove the view.
推荐答案
我找到了一种方法,将在此处分享.如果您需要更多代码,请使用指向我的> Stackoverflow答案我也链接了我的问题.
I have found a way and will share here. If you want more of the code, use the link to my Stackoverflow answer that I also linked in my question.
我想直到现在我都没有设置正确的代码,也没有租用所有的代码.
I guess I wasn't setting up the correct code, or at lease all of it, until now.
DrawView.java
package com.example.floatingicon;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;
public class DrawView extends View{
Paint paint = new Paint();
public DrawView(Context context){
super(context);
paint.setColor(Color.BLACK);
}
@Override
public void onDraw(Canvas canvas){
canvas.drawLine(240, 0, 240, 620, paint);
canvas.drawLine(0, 200, 480, 200, paint);
}
}
这将在屏幕上绘制一个十字形
This will draw a cross on the screen
MainService.java
import com.example.floatingicon.DrawView;
public class MainService extends Service
{
private DrawView drawView;
@Override
public void onCreate()
{
drawView = new DrawView(this);
final WindowManager.LayoutParams paramsDraw = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
PixelFormat.TRANSLUCENT);
paramsDraw.gravity = Gravity.TOP | Gravity.LEFT;
paramsDraw.x=0;
paramsDraw.y=0;
winManager.addView(drawView, paramsDraw);
}
}
如果将其添加到已经存在的代码中,它应该可以正常工作.
If you add that to the already existing code, it should work just fine.
这篇关于如何在其他应用程序上方显示代码绘制的线条?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!