我正在尝试用布尔值更改 imageview 的颜色,我错过了什么?

I'm trying to change the color of an imageview with a boolean, what am I missing?

我有一个按钮覆盖了我的 phone 的整个屏幕,我在该按钮上有两个点,按下时会在我的布尔值的 true 和 false 之间切换,并且该功能假设切换颜色位于屏幕中间的图像视图

如果布尔值为真,中间的图像应该是红色的 如果布尔值是假的,中间的图像应该变成绿色

我以为我了解如何使用布尔值,但显然没有考虑到我无法像我想的那样自由地来回切换颜色

我希望应用程序启动时中间的 imageview 是红色的,如果我点击正确的位置它会从红色变为绿色,如果它是绿色并且我点击另一个特定的位置它会变回红色,等等

如果有人对我如何做到这一点有任何建议,我将不胜感激谢谢

public class MainActivity extends ActionBarActivity {

Display mainDisplay;
ViewGroup mainLayout;
ImageButton touchpad;
Context ourContext;
ImageView mainblock;
RelativeLayout.LayoutParams layoutPositioner;

boolean redBlock;

float offsetX = 0;
float offsetY = 0;

float padPOS_x = 0;
float padPOS_y = 0;

float designSize_w = 540;
float designSize_h = 960;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);//Remove title bar
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);//Hides notification bar
    this.setContentView(R.layout.activity_main);//set content view AFTER ABOVE sequence (to avoid crash)

    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

    mainDisplay = getWindowManager().getDefaultDisplay();
    mainLayout = (ViewGroup)findViewById(R.id.id_layout);   
    mainLayout.setBackgroundColor(0xFF667C26);

    DisplayMetrics m = new DisplayMetrics();
    this.getWindowManager().getDefaultDisplay().getMetrics(m);
    int windowHeight = m.heightPixels;
    int windowWidth = m.widthPixels;
    int h = windowHeight; 
    int w = windowWidth;
    offsetX = (w / 540.0f);
    offsetY = (h / 960.0f);

    ourContext = this;

    ImageView leftbox = new ImageView(ourContext);
    SetPos(75, 100, 100, 100);
    leftbox.setLayoutParams(layoutPositioner);
    leftbox.setBackgroundColor(0xFF0000A0);
    mainLayout.addView(leftbox);

    ImageView rightbox = new ImageView(ourContext);
    rightbox.setBackgroundColor(0xFF0000A0);
    SetPos(350, 100, 100, 100);
    rightbox.setLayoutParams(layoutPositioner);
    mainLayout.addView(rightbox);

    mainblock = new ImageView(this);
    SetPos(100, 500, 300, 300);
    mainblock.setLayoutParams(layoutPositioner);
    mainLayout.addView(mainblock);
    redBlock = true;

    touchpad = new ImageButton(this);
    SetPos(padPOS_x,padPOS_y,designSize_w,designSize_h);
    touchpad.setLayoutParams(layoutPositioner);
    touchpad.getBackground().setAlpha(0);
    mainLayout.addView(touchpad);
    touchpad.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            //when touch pad is touched

        if (
                (!redBlock) &&
                (event.getX() > 75) && (event.getX() < 175) &&
                (event.getY() > 100) && (event.getY() < 200)
            )
                    //if I touch left block and mainblock is green ...
            {
            redBlock = true;

            //main black becomes red
            }

        if (
                (redBlock) &&
                (event.getX() > 350) && (event.getX() < 450) &&
                (event.getY() > 100) && (event.getY() < 200)
            )
                    //if I touch right block and mainblock is red ...
            {
            redBlock = false;

            //main black becomes red
            }

        return false;
    }
});

    if(redBlock)
    {
    mainblock.setBackgroundColor(0xFFE42217); //lavared

    }

else if(!redBlock)
    {
    mainblock.setBackgroundColor(0xFF57E964); //stoplightgreen

    }
}


public void SetPos(float x, float y, float width, float height) {
    layoutPositioner = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    layoutPositioner.topMargin = (int) (offsetY * y);
    layoutPositioner.leftMargin = (int) (offsetX * x);
    layoutPositioner.width = (int)(width * offsetX);
    layoutPositioner.height = (int)(height * offsetY);      
}
}

好吧,我有一些提示可能会对您有所帮助。

  1. 您更改背景颜色的方法是您的 onCreate 方法。这意味着它只会在创建 Activity 的那一刻执行。而且,正因为如此,您的布尔值实际上可以更改,但就是这样......您没有对 backgroundColor 做任何工作。您需要在触摸事件中调用改变颜色的方法。

  2. Android 中 View 对象的每个子类(包括 ImageView、Button 等等)都具有 TouchListener/ClickListener 的能力。使用它可以帮助您缩小处理被点击内容的工作量。有了它,您将不再需要检查 XY 坐标(如果它在视图范围内)..