0
\$\begingroup\$
 private void moveViewToScreenCenter(final ImageView img, int x) {
    DisplayMetrics dm = new DisplayMetrics();
    this.getWindowManager().getDefaultDisplay().getMetrics(dm);

    img.animate()
            .translationX(0)
            .withEndAction(new Runnable() {
                @Override
                public void run() {
                    enableAll();
                    showDialog();
                }
            })
            .translationY(-x * dm.heightPixels / 6)
            .setDuration(2000)
            .setInterpolator(new LinearInterpolator())
            .setStartDelay(0);
}

public void showDialog()
{
    final CharSequence[] items = {"0", "1", "2","3","4","5"};

    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);

    alertDialogBuilder.setTitle("which floor is your destination");

    alertDialogBuilder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int item) {

            if (items[item].equals("0")) {

                moveViewToScreen(img, 0);
            } else if (items[item].equals("1")) {

                moveViewToScreen(img, 1);
            } else if (items[item].equals("2")) {

                moveViewToScreen(img, 2);
            } else if (items[item].equals("3")) {

                moveViewToScreen(img, 3);
            } else if (items[item].equals("4")) {

                moveViewToScreen(img,4);
            }else if (items[item].equals("5")) {

                moveViewToScreen(img,5);
            }

            dialog.dismiss();
        }
    });
    alertDialogBuilder.show();

}

private void moveViewToScreen(final ImageView img, int x) {
    DisplayMetrics dm = new DisplayMetrics();
    this.getWindowManager().getDefaultDisplay().getMetrics(dm);

    img.animate()
            .translationX(0)
            .withEndAction(new Runnable() {
                @Override
                public void run() {
                    enableAll();

                }
            })
            .translationY(-x * dm.heightPixels / 6)
            .setDuration(2000)
            .setInterpolator(new LinearInterpolator())
            .setStartDelay(0);
}

Buttons are disabled (disableALL()) during animation but animation seems fast between far floors and slow between consecutive ones . i'm still new to android and simple animation stuff like this

\$\endgroup\$
3
  • \$\begingroup\$ Hi! Welcome to Code Review. Please include a description of your code before your code, so that reviewers may see that short description when browsing. \$\endgroup\$ Commented Jan 11, 2016 at 18:53
  • \$\begingroup\$ Welcome to Code Review. Unfortunately I personally do not consider this a good question (In particular, see the "On-topic", "Screenshot" and "Motivation" sections, and work on your Description). With a few changes to your question, you can make it much more clearer and more interesting for reviewers. I am hoping to give you an upvote soon. \$\endgroup\$ Commented Jan 11, 2016 at 19:46
  • \$\begingroup\$ About the slow movement: The duration is always 2 seconds, so you need to scale that with the distance of your image. \$\endgroup\$
    – GiantTree
    Commented Jan 11, 2016 at 20:48

1 Answer 1

2
\$\begingroup\$

Janos' advice is applying DRY principle to your code. It can be extended to also cover moveViewToScreenCenter and moveViewToScreen functions, since they are almost identical:

private void moveViewToScreenCenter(final ImageView img, int x, boolean center) {
    DisplayMetrics dm = new DisplayMetrics();
    this.getWindowManager().getDefaultDisplay().getMetrics(dm);

    img.animate()
            .translationX(0)
            .withEndAction(new Runnable() {
                @Override
                public void run() {
                    enableAll();
                    if (center)
                       showDialog();
                }
            })
            .translationY(-x * dm.heightPixels / 6)
            .setDuration(2000)
            .setInterpolator(new LinearInterpolator())
            .setStartDelay(0);
}

and call this function by providing true/false to your center parameter.

Also, functional scalability might be an issue: what if you have to handle 11 eleven floors in the future? (0 -> 10). You will be forced make several changes to handle a simple request like this.

In order to accommodate larger values, I would dynamically define your array and not base my condition on char (which allows only one character). Something like this (not tested):

// place this in some generic place
List<Integer> makeSequence(int begin, int end) {
  List<Integer> ret = new ArrayList(end - begin + 1);
  for(int i = begin; i <= end; i++, ret.add(i));
  return ret;  
}

int maxFloors = 5;
List<Integer> items = makeSequence(0, 5);

public void onClick(DialogInterface dialog, int item) {
    moveViewToScreen(img, items[item]);
    dialog.dismiss();
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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