gpt4 book ai didi

java - 操纵 Android 屏幕颜色 RGB

转载 作者:搜寻专家 更新时间:2023-11-01 08:01:21 24 4
gpt4 key购买 nike

我正在尝试为 Android 制作可操纵屏幕(具体来说是颜色)的应用程序。我有以下两个类(class):

static class ScreenAdjuster {

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

public static void setColor(Layer redView, Layer greenView, Layer blueView, 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);
Log.d("display", "setting..." + r + " " + g + " " + b);
}

}

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

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

@Override
protected void onDraw(Canvas canvas){
super.onDraw(canvas);
canvas.drawARGB(this.a, this.r, this.g, this.b);
Log.d("display", "rendering..");
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
int parentHeight = MeasureSpec.getSize(heightMeasureSpec);
this.setMeasuredDimension(parentWidth/2, parentHeight);
this.setLayoutParams(new ViewGroup.LayoutParams(parentWidth/2,parentHeight));
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
Log.d("display", "filling...");
}

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

我在此函数中向管理器添加 View :

public void setColorsViews() {
if(!colorsFirstTime) {
view = new Layer(cordova.getActivity());
redView = new Layer(cordova.getActivity());
greenView = new Layer(cordova.getActivity());
blueView = new Layer(cordova.getActivity());


WindowManager localWindowManager = (WindowManager)cordova.getActivity().getSystemService("window");
WindowManager.LayoutParams layoutParams = cordova.getActivity().getWindow().getAttributes();
localWindowManager.addView(view, layoutParams);
localWindowManager.addView(greenView, layoutParams);
localWindowManager.addView(redView, layoutParams);
localWindowManager.addView(blueView, layoutParams);
colorsFirstTime = true;
Log.d("display", "views added");
}
}

(它将成为 Cordova 的插件,所以我通过 cordova.getActivity() 获取 Activity。它与我的其他函数一起使用,所以我想它在这里也可以使用)。

我通过以下方式调用所有这些: ScreenAdjuster.setColor(redView, greenView, blueView, red, green, blue) ;

但是……没有任何反应。调用所有函数(onDraw()onMeasure()setColorsViews(),但屏幕保持不变。

我做错了什么?

提前致谢!

最佳答案

变化:

    @Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
int parentHeight = MeasureSpec.getSize(heightMeasureSpec);
this.setMeasuredDimension(parentWidth / 2, parentHeight);
//Since you are attatching it to the window use window layout params.
WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams(parentWidth / 2,
parentHeight);
this.setLayoutParams(layoutParams);

super.onMeasure(widthMeasureSpec, heightMeasureSpec);
Log.d("display", "filling...");
}

添加: layoutParams.format = PixelFormat.TRANSLUCENT;

public void setColorsViews() {
if (!colorsFirstTime) {
view = new Layer(cordova.getActivity());
redView = new Layer(cordova.getActivity());
greenView = new Layer(cordova.getActivity());
blueView = new Layer(cordova.getActivity());

WindowManager localWindowManager = (WindowManager) cordova.getActivity()
.getSystemService("window");
WindowManager.LayoutParams layoutParams = cordova.getActivity().getWindow()
.getAttributes();
layoutParams.format = PixelFormat.TRANSLUCENT;

localWindowManager.addView(view, layoutParams);
localWindowManager.addView(greenView, layoutParams);
localWindowManager.addView(redView, layoutParams);
localWindowManager.addView(blueView, layoutParams);
colorsFirstTime = true;
Log.d("display", "views added");
}
}

ScreenAdjuster.setColor(redView, greenView, blueView, 50, 150, 0); ScreenAdjuster.setColor(redView, greenView, blueView, 50, 150, 0);

关于java - 操纵 Android 屏幕颜色 RGB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20909560/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com