2
\$\begingroup\$

I've started my first "job" like 2 weeks ago, and to be honest I've received a quite hard task to do, since I'm beginner with Android, and quite fresh at programming. The thing I needed was to find the way to rotate the buttons while changing orientation of the phone, after some research I've found a solution on the web, and after adapting it for my purposes the functionality worked.

What I'm asking you for is to guide me on how I could refactor the code. My boss mentioned that in the OrientationListener class onOrientationChanged method repeats it's implementation with the same kind of objects and that the code is doubled.

public class MainActivity extends Activity implements ZoomListener, View.OnClickListener {

private static final String TAG = MainActivity.class.getSimpleName();

private static final int PERMISSION_REQUEST_CODE = 512;

private Animation toLandAnim, toPortAnim, toRevPortAnim, toRevLandAnim;
private OrientationListener orientationListener;

private ImageButton zoomIn;
private ImageButton zoomOut;
private ImageButton cameraSwitch;

private TextView mCameraStatusTv;
private CameraView cameraPreview;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    setContentView(R.layout.activity_main);
    mCameraStatusTv = (TextView) findViewById(R.id.camera_status);

    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

    zoomIn = (ImageButton) findViewById(R.id.btnZoomIn);
    zoomIn.setOnClickListener(this);

    zoomOut = (ImageButton) findViewById(R.id.btnZoomOut);
    zoomOut.setOnClickListener(this);

    cameraSwitch = (ImageButton) findViewById(R.id.btnCameraSwitch);
    cameraSwitch.setOnClickListener(this);

    toLandAnim= AnimationUtils.loadAnimation(this, R.anim.rotate90degrees);
    toPortAnim= AnimationUtils.loadAnimation(this, R.anim.rotate_anticlockwise);
    toRevPortAnim = AnimationUtils.loadAnimation(this, R.anim.rotate180degrees);
    toRevLandAnim = AnimationUtils.loadAnimation(this, R.anim.rotate270deegres);

    orientationListener = new OrientationListener(this);

    cameraPreview = (CameraView) findViewById(R.id.custom_preview);
    cameraPreview.setTestResultListener(this);


}

   @Override protected void onStart() {
    orientationListener.enable();
    super.onStart();
}
}


  public class OrientationListener extends OrientationEventListener {
    final int ROTATION_O = 1;
    final int ROTATION_90   = 2;
    final int ROTATION_180  = 3;
    final int ROTATION_270  = 4;

    private int rotation = 0;
    public OrientationListener(Context context) { super(context); }


    @Override public void onOrientationChanged(int orientation) {
        if( (orientation < 35 || orientation > 325) && rotation!= ROTATION_O){ // request portrait mode
            rotation = ROTATION_O;

            zoomIn.startAnimation(toPortAnim);
            zoomOut.startAnimation(toPortAnim);
            cameraSwitch.startAnimation(toPortAnim);
        }
        else if( orientation > 145 && orientation < 215 && rotation!=ROTATION_180){ // request reverse portrait mode
            rotation = ROTATION_180;

            zoomIn.startAnimation(toRevPortAnim);
            zoomOut.startAnimation(toRevPortAnim);
            cameraSwitch.startAnimation(toRevPortAnim);


        }
        else if(orientation > 235 && orientation < 305 && rotation!=ROTATION_270){ // request reverse landscape mode
            rotation = ROTATION_270;

            zoomIn.startAnimation(toRevLandAnim);
            zoomOut.startAnimation(toRevLandAnim);
            cameraSwitch.startAnimation(toRevLandAnim);


        }
        else if(orientation > 55 && orientation < 125 && rotation!=ROTATION_90){ // request landscape mode
            rotation = ROTATION_90;

            zoomIn.startAnimation(toLandAnim);
            zoomOut.startAnimation(toLandAnim);
            cameraSwitch.startAnimation(toLandAnim);
        }
    }

}
\$\endgroup\$
1
\$\begingroup\$

Something I'd recommend:

Create a ZoomInZoomOut method which contains the zoomin/zoomout code and then call it whenever you need it. For example:

public void ZoomInOut(Animation toPortAnim){
    zoomIn.startAnimation(toRevPortAnim);
    zoomOut.startAnimation(toRevPortAnim);
    cameraSwitch.startAnimation(toRevPortAnim);
    return;
}

if( (orientation < 35 || orientation > 325) && rotation!= ROTATION_O){ // request portrait mode
    rotation = ROTATION_O;
    ZoomInOut(toPortAnim);
}
//etc

This allows you to make changes later on which will affect all four if statements, which I'm sure that is what your boss meant by it being redundant.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.