我试图用布尔值更改图像视图的颜色,我缺少什么?

时间:2015-03-05 22:19:33

标签: java android imageview boolean

我有一个按钮,它包含了我手机的整个屏幕,我在该按钮上有两个点,按下时会切换我的布尔值的真和假,该功能假设切换位于的图像视图的颜色屏幕中间

如果布尔值为真,则中间图像为红色 如果布尔值为假,则中间图像变为绿色

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

我希望应用程序启动时中间的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);      
}
}

enter image description here

1 个答案:

答案 0 :(得分:2)

好吧,我有一些可能对你有帮助的提示。

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

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