操纵Android屏幕颜色RGB

时间:2014-01-03 17:41:26

标签: java android view cordova screen

我正在尝试为Android操作其屏幕的应用程序(其颜色,具体而言)。我有以下两个课程:

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();
       }
}

我在此功能中向管理员添加了视图:

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,红色,绿色,蓝色);

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

我做错了什么?

提前致谢!

2 个答案:

答案 0 :(得分:3)

更改

    @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);

答案 1 :(得分:1)

看起来,从WindowManager的Android规范:

Throws WindowManager.BadTokenException for certain programming errors, 
such as adding a second view to a window without removing the first view.

在每个addView(...)之间添加以下行,它会删除上一个视图,并加载下一个视图:

localWindowManager.removeView(View view);

或者,您可以将后续的三行更改为以下内容,让它们替换当前视图:

lastWindowManager.updateViewLayout(view, params);

如果要显示用户输入的RGB颜色,还应更改为以下代码(或类似代码):

view = new Layer(cordova.getActivity());
view.setColor(int a, int r, int g, int b)      //enter values here somehow?
localWindowManager.addView(view, layoutParams);

这将从新类创建一个视图,为A,R,G和B赋值,然后将该新视图添加为当前视图。

P.S。 - 我的Android开发经验有限,所以我不确定这是否正确,但看看文档似乎指向这个解决方案。此外,没有看到所有的代码,我不确定这是修复...

相关问题