以下是我認爲可能的工作:
您需要以下值:
紅,綠,藍,Alpha和對比度(R,G,B,A,C)
商店他們偏好使用滑塊後:
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();
}
定義層的Clas它擴展了視圖並用於在畫布上繪製應用的值。
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();
}
}
接下來寫一個應該處理這些值的變化並將它們應用到窗口的服務。
public class ScreenAdjustService extends Service
{
//Handle everything here
}
//聲明視圖用於施加存儲在偏好設置 私有靜態層視圖中的值; ... public static int r; public static int b; public static int g; public static int a; public static int c;
在onCreate方法,
獲取值預先存儲在偏好,
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);
組視圖用於設定檢索到的值,
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);
種
編寫可重用方法
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);
}
電話使用服務類
ScreenAdjustService.setRGB(MyActivity.this.r, MyActivity.this.g, MyActivity.this.b);
ScreenAdjustService.setAlpha(MyActivity.this.a);
ScreenAdjustService.setContrast(MyActivity.this.c);
不要忘記申報您的服務在清單XML文件
<service android:name=".ScreenAdjustService " />
我可能會在必要時這些方法我錯過了一兩件事,因爲我在飛行中這樣做了,但我認爲這應該做到這一點。
此外,您需要添加標記視圖始終處於頂峯。 – tomwesolowski
因此,在這種方法中,這個疊加的活動將如何將觸摸事件傳遞給重疊的活動? – Paras