Search code examples
androidbuttonoverridingsensorsextend

android button rotation with device orientation


in my android app, when i rotate/tilt my device, i want to rotate my button controls in the opposite direction, which compensates the device's orientation. kinda like the idea of screen orientation in android

here is my sensor class:

@Override
public void onSensorChanged(SensorEvent event) {
    // TODO Auto-generated method stub
    synchronized (this) {
        double alpha = 0.8;
        double gravity[] = new double[3];
        switch (event.sensor.getType()){
            case Sensor.TYPE_ACCELEROMETER:
                // alpha is calculated as t / (t + dT)
                // with t, the low-pass filter's time-constant
                // and dT, the event delivery rate                                  
                //gravity[0] = alpha * gravity[0] + (1 - alpha) * event.values[0];
                //gravity[1] = alpha * gravity[1] + (1 - alpha) * event.values[1];
                //gravity[2] = alpha * gravity[2] + (1 - alpha) * event.values[2];

                //sensorX = event.values[0] - gravity[0];
                //sensorY = event.values[1] - gravity[1];
                //sensorZ = event.values[2] - gravity[2];
                    //ignore this section!
            break;
        case Sensor.TYPE_ORIENTATION:   
                sensorZ = event.values[0];
                sensorX = event.values[1];
                sensorY = event.values[2];
                outputZ.setText("Z:"+Float.toString(event.values[0]));
                outputX.setText("X:"+Float.toString(event.values[1]));
                outputY.setText("Y:"+Float.toString(event.values[2]));
                //Toast.makeText(getApplicationContext(), "SWITCH TO ",Toast.LENGTH_LONG).show();
                //RelativeLayout s_r_t = (RelativeLayout) findViewById(R.id.snap_r_t);
                //RotateView r_flash = new RotateView(this , s_r_t.getWidth(), s_r_t.getHeight(), 90)(RotateView) findViewById(R.id.flash);
                //RotateView r_flash = new RotateView(this , s_r_t.getWidth(), s_r_t.getHeight(), 90);
                s_r_t.addView(r_flash);
                //s_r_t.setLayoutParams(params)
                //flash.getWidth();
                //flash.getHeight();

        break;

        }
    }

}

along with my extended button:

 public class RotateView extends Button {

private float cx,cy;
private double degree;
private Button button;

public RotateView(Context context , int x, int y, double deg) {
    super(context);     
    cx = (float)x/2;
    cy = (float)y/2;
    degree = deg;
}

public RotateView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

@Override
protected void onDraw(Canvas canvas) {
     canvas.save();
     canvas.rotate((float)degree,cx,cy);
     super.onDraw(canvas);
     canvas.restore();

}

}

how do i create my button with the extended button class?

here is my button:

 flash = (Button)findViewById(R.id.flash);

basically, this is what i want to achieve(pseudo code):

new button = find my button id;
onSensorChanged{
 set button rotation to sensor.getdata degrees;
}

if possible, please give complete segments of the code, i've seen many answers almost answered my question, but missing the essential part of how to implement the extended button back to my button in my main class.


Solution

  • for future reference, follow the guide on android dev about custom components;

    in my case, i've forgot to add the xml layout

    the procedure goes like this->

    1. Create a class for the component then override parts that i want to change

      in this case:

      void setValue(double deg) {//this changes the rotation of canvas....
          degree = deg;
      }
      
    2. add xml for the custom class

    3. find view in my activity and do changes

    done