Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Hi,

I am working on an app, part of which has a bubble level which I have implemented with the accelerometer sensor. The problem is that there is nothing to limit it's movement, so if I tip the phone too far, the bubble disappears off the screen. The level line (on which the bubble travels up and down on) and the bubble are contained within a relative layout. So I though to get the y coordinate of the top and bottom of the layout, the get the y coordinate of the imageview, then if this coordinate was greater than the bottom of the layout or less than the top then set the image view y coordinate to that view, unfortunately that doesn't seem to work...here is how I've done it below:

Java
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
   @Override
   public void onSensorChanged(SensorEvent event) {

       boolean initialized = false;
       int x;
       double pos;
       if ((this.CurrentApi >= 11) && (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER))
       {
           levelBar.getLocationOnScreen(LevelValues);

           x = LevelValues[0];
           pos = event.values[1]/2.5;

           int[] posXY = new int[2];

           y = (int)Math.pow(pos, 5);
           if ((pos <= -0.5) || (pos >= 0.5)){
               indicator.setImageResource(R.drawable.level_bad);
           }
           else
           {
               indicator.setImageResource(R.drawable.level_good);
           }
           indicator.setTranslationY(-y);

           indicator.getLocationOnScreen(posXY);
           int yPos = posXY[1];

           float maxTemp = (float)levelH.getTop();
           float minTemp = (float)levelH.getBottom();

           max = maxTemp + 20.0f;
           min = minTemp - 20.0f;
           Log.d("Y COORDINATES", Integer.toString(yPos));
           Log.d("MAX", Float.toString(max));
           Log.d("MIN", Float.toString(min));

           if (yPos >= min){
               indicator.setY(min);
           }
           else if (yPos <= max){
              indicator.setY(max);
           }
       }
   }


When the bubble gets to the top or bottom, it flickers and then disappears off screen, could anyone help me with this, I really don't know what to do next.

Any help is greatly appreciated
Posted
Comments
GaryDoo 8-Sep-13 13:01pm    
f anyone can help I'd greatly appreciate it. I've changed the code a little to 'float yMin = (float)levelH.getTop()+90.0f; float yMax = (float)levelH.getBottom();' & 'if(yPos<=yMin) indicator.setY(yMin); if(yPos>=yMax) indicator.setY(yMax);' The problem is if either of these conditions are met, the bubble position is set but it's not at the top or bottom of the level line. The bubble is repositioned, I don't really understand this, my assumption is there's a crossover between ypos of the screen & ypos of the relative layout. if someone can help I'd appreciate it

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900