画上的所有应用程序顶部的Andr​​oid的印刷品吗? [英] Draw an Android canvas on top of all applications?

查看:106
本文介绍了画上的所有应用程序顶部的Andr​​oid的印刷品吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怀疑这是不可能的,因为Android的性质,但有一个方法来创建不爽(可能使用Canvas对象?)一个视图,显示在所有的应用程序之上?

I suspect this isn't possible, given the nature of Android, but is there a way to create a view of sorts (perhaps using a Canvas object?) that is shown on top of all applications?

我对这个项目的意图不是恶意的。我创建了用于Windows的某些简单的颜色过滤软件,仅仅增加了一个窗口上的所有窗口的顶部,这是充满用户选择的颜色,并且具有低不透明度。这增加了光色调到屏幕上,它可以帮助用户Scoptopic敏感综合症用电脑很长一段时间。我想提出这个软件的Andr​​oid版本。例如:

My intent for this project is not malicious. I have created some simple Color Filter software for Windows that simply adds a window on top of all windows, which is filled with a user-chosen color, and has a low opacity. This adds a light tint to the screen which helps users with Scoptopic Sensitivity Syndrome use a computer for long periods of time. I would like to make an Android version of this software. For example:

在左侧的图像是原来的,和一个在右边就是我想要的目的。

The image on the left is the original, and the one on the right is what I am trying to achieve.

有没有办法在所有与Android做到这一点?据我所知,应用程序将作为一个系统服务,所以它不意外停止运行,但据我所知,是没有办法一般借鉴其它应用程序上方的屏幕上。如果可能的话,你可以建议哪些API来看看?

Is there any way at all to do this with Android? I understand that the application will have to run as a system service so that it isn't stopped unexpectedly, but to my knowledge, there is no way to generically draw on the screen on top of other applications. If possible, can you suggest what APIs to look into?

推荐答案

下面是我认为可以工作:

Here is what I think may work:

您需要以下值:

红,绿,蓝,Alpha和对比度(R,G,B,A,C)

Red, Green, Blue, Alpha and Contrast (r,g,b,a,c)

使用滑块后,它们存储在preference:

Store them in a preference after using sliders:

  private SharedPreferences pref;
  //In onCreate..
  this.pref = getSharedPreferences("pref", 0);
  //Method to save the values in Preferences
  private void save()
  {
    SharedPreferences.Editor localEditor = this.pref.edit();
    localEditor.putInt("a", this.a);
    localEditor.putInt("r", this.r);
    localEditor.putInt("g", this.g);
    localEditor.putInt("b", this.b);
    localEditor.putInt("c", this.c);
    localEditor.commit();
  }

定义一个图层类延伸的观点,并用于绘制在画布上的应用价值。

Define a Layer Class which extends the view and is used to draw the applied values on the canvas.

import android.view.View;
import android.graphics.Canvas;
import android.content.Context;

public class Layer extends View
{
  private int a;
  private int b;
  private int g;
  private int r;

  public Layer(Context context){
    super(context)
  }

  protected void onDraw(Canvas canvas){
    super.onDraw(canvas);
    canvas.drawARGB(this.a, this.r, this.g, this.b);
  }

  public void setColor(int a, int r, int g, int b){
    this.a = a;
    this.r = r;
    this.g = g;
    this.b = b;
    invalidate();
  }
}

接下来写的这是应该处理的变化,这些价值观,并将其应用到windows服务。

Next write a service which is supposed to handle the changes in these values and apply them to the windows.

public class ScreenAdjustService extends Service
{
     //Handle everything here
}

//声明次用于施加存储在preFS值 私有静态层视图; ... 公共静态INT R; 公共静态INT B: 公共静态INT克; 公共静态INT一个; 公共静态诠释三;

//Declare views for applying the values that are stored in Prefs private static Layer view; ... public static int r; public static int b; public static int g; public static int a; public static int c;

在onCreate方法,

In the onCreate method,

获取值previously存储在preferences,

Get values previously stored in preferences,

SharedPreferences localSharedPreferences = getSharedPreferences("pref", 0);
a = localSharedPreferences.getInt("a", 0);
r = localSharedPreferences.getInt("r", 0);
g = localSharedPreferences.getInt("g", 0);
b = localSharedPreferences.getInt("b", 0);
c = localSharedPreferences.getInt("c", 0);

设置视图设置检索到的值,

set views for setting the retrieved values,

view = new Layer(this);
redView = new Layer(this);
...

添加这些视图窗口

//Pass the necessary values for localLayoutParams
WindowManager.LayoutParams localLayoutParams = new WindowManager.LayoutParams(...);
WindowManager localWindowManager = (WindowManager)getSystemService("window");
localWindowManager.addView(view, localLayoutParams);
localWindowManager.addView(redView, localLayoutParams);
localWindowManager.addView(greenView, localLayoutParams);

编写可复用的方法

Write reusable methods

public static void setAlpha(int alpha){
        //Handle all conditions
        view.setColor(alpha, 0, 0, 0);
}
public static void setContrast(int contrast){
    //Handle all conditions
    view.setColor(c, 100, 100, 100);
}

public static void setRGB(int r, int g, int b){
    //Handle all conditions
    redView.setColor(r, 255, 0, 0);
    greenView.setColor(g, 0, 255, 0);
    blueView.setColor(b, 0, 0, 255);
}

在必要时使用服务类调用这些方法

Call these methods wherever necessary using the service class

ScreenAdjustService.setRGB(MyActivity.this.r, MyActivity.this.g, MyActivity.this.b);
ScreenAdjustService.setAlpha(MyActivity.this.a);
ScreenAdjustService.setContrast(MyActivity.this.c);

不要忘了在清单XML文件中声明你的服务

Don't forget to declare your service in the Manifest xml file

<service android:name=".ScreenAdjustService " />

我可能会错过一两个事情,因为我这样做对飞,但我认为这应该这样做。

I might have missed a thing or two as I did this on the fly, but I think this should do it.

这篇关于画上的所有应用程序顶部的Andr​​oid的印刷品吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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