问题
Details: I would like to add a code drawn line (like you draw with canvas/paint) and display it over other applications.
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
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!
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.
回答1:
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.
来源:https://stackoverflow.com/questions/23681162/how-can-i-display-a-code-drawn-line-over-other-apps