以下是我认为可能有效的方法:
您将需要以下值:
红色、绿色、蓝色、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();
}
定义一个扩展视图并用于在画布上绘制应用值的层类。
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
}
//声明用于应用存储在 Prefs 私有静态层视图中的值的视图;... 公共静态 int r; 公共静态int b;公共静态int g;公共静态int a;公共静态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);
不要忘记在 Manifest xml 文件中声明您的服务
<service android:name=".ScreenAdjustService " />
当我在飞行中做这件事时,我可能错过了一两件事,但我认为应该这样做。