Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent conflict animation with device rotation in android?

I have a two button objects in activity this object has some move animation with ObjectAnimator these animations work perfect but when the animation is run in background i want to rotate device some times portrait mode coordinate in ObjectAnimator run in landscape mode or reverse.

ObjectAnimator move_next_btn = ObjectAnimator.ofFloat(next_btn, "x", next_btn.getX(),
                        next_btn.getX() + next_btn.getWidth());

in above code, the button should go out of the screen but after this line run, if device rotates to landscape mode button goes on middle screen I think when this problem occurs portrait coordinate run in landscape mode.

I give more detail with the picture:

  1. this is my picture viewer with two buttons on the side when the user doesn't touch screen buttons with animation goes out of the screen:

enter image description here

  1. second image show one of the arrows is hide (perhaps) I think it's because when animation wants to run I am rotated screen and arrow remain in out of screen:

enter image description here

  1. in the third image when animation run in portrait mode I rotate to landscape mode and arrow stay in the center of the screen, of course, this image takes in portrait mode if again rotate to landscape arrow go on center:

enter image description here

this happens to occur just when move animation (int above code) and screen rotation run at the same time not always.

complete code:

private void init() {
        index = getIntent().getIntExtra("index", 0);
        current_index = getIntent().getIntExtra("current_index", 0);

        final View previous_btn_rl = findViewById(R.id.previous_btn_rl);
        findViewById(R.id.previous_btn_rl).findViewById(R.id.previous_btn)
                .setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                time_to_hide_button = 3;
                try {
                    AssetManager assetManager = getAssets();
                    if (current_index > 0) {
                        current_index--;
                        String path = "img" + "/" + String.valueOf(index + 1) + "/gallery/" + String.valueOf(current_index + 1) + ".jpg";
                        imageView.setImageBitmap(BitmapFactory.decodeStream(assetManager.open(path)));
                    }
                } catch (Exception e) {
                    Log.d(TAG, "onClick: " + e.getMessage());
                }
            }
        });

        final View next_btn_rl = findViewById(R.id.next_btn_rl);
        findViewById(R.id.next_btn_rl).findViewById(R.id.next_btn)
            .setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                time_to_hide_button = 3;

                try {
                    AssetManager assetManager = getAssets();
                    int fileCount = assetManager.list("img" + "/" + String.valueOf(index + 1) + "/gallery").length;
                    if (current_index < (fileCount - 1)) {
                        current_index++;
                        String path = "img" + "/" + String.valueOf(index + 1) + "/gallery/" + String.valueOf(current_index + 1) + ".jpg";
                        imageView.setImageBitmap(BitmapFactory.decodeStream(assetManager.open(path)));
                    }
                } catch (Exception e) {
                    Log.d(TAG, "onClick: " + e.getMessage());
                }

            }
        });

        imageView = (ImageView)findViewById(R.id.image_view);
        imageView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {

                if (previous_btn_rl.getX() == -previous_btn_rl.getWidth()) {

                    ObjectAnimator move_previous_btn = ObjectAnimator.ofFloat(previous_btn_rl, "x", -previous_btn_rl.getWidth(), 0);
                    ObjectAnimator move_next_btn = ObjectAnimator.ofFloat(next_btn_rl, "x",
                            next_btn_rl.getX(),
                            next_btn_rl.getX() -next_btn_rl.getWidth());

                    AnimatorSet set = new AnimatorSet();
                    set.setDuration(500);
                    set.setInterpolator(new AccelerateInterpolator());
                    set.playTogether(move_previous_btn, move_next_btn);
                    set.start();
                }

                time_to_hide_button = 3;
                return false;
            }
        });

        try {
            AssetManager assetManager = getAssets();
            String path = "img" + "/" + String.valueOf(index + 1) + "/gallery/" + String.valueOf(current_index + 1) + ".jpg";
            imageView.setImageBitmap(BitmapFactory.decodeStream(assetManager.open(path)));
        } catch (Exception e) {
            Log.d(TAG, "init: ");
        }

        handler.post(new Runnable() {
            @Override
            public void run() {

                if (time_to_hide_button <= 0 && previous_btn_rl.getX() == 0) {

                    ObjectAnimator move_previous_btn = ObjectAnimator.ofFloat(previous_btn_rl, "x", 0, -previous_btn_rl.getWidth());
                    ObjectAnimator move_back_btn = ObjectAnimator.ofFloat(next_btn_rl, "x",
                            next_btn_rl.getX(),
                            next_btn_rl.getX() + next_btn_rl.getWidth());


                    AnimatorSet set = new AnimatorSet();
                    set.setDuration(500);
                    set.setInterpolator(new AccelerateInterpolator());
                    set.playTogether(move_previous_btn, move_back_btn);
                    set.start();

                } else {
                    if (time_to_hide_button > 0) {
                        time_to_hide_button--;
                    }
                }

                handler.postDelayed(this, 1000);
            }
        });
    }

I've recorded my problem: my problem video on youtube

Whats the best way to prevent this problem?

like image 628
Khalil Avatar asked Jan 11 '17 15:01

Khalil


People also ask

How do you stop android apps from rotating?

In the menu, make sure Rotation Control and Per App Settings are enabled. By default, they should be. Tap Per App rotation settings, choose the apps whose settings you wish to alter, then change their settings.

What happens when android screen rotates?

Rotating the android device changes the device configuration. Android replaces the current resources with the best suited resources for that screen orientation. Thats why when we add a resource in android we add different versions of that resource.


1 Answers

for a temporarily screen lock you can easily use:

//for android tablets **<uses-sdk android:minSdkVersion="12" />**
//works perfectly... **WATCH OUT**: look portrait to reverse-portrait on api level 13 :)

currentActivity.setRequestedOrientation(currentActivity.getResources().getConfiguration().orientation);

//to re-enable sensor, just do:

currentActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
like image 96
Jon Goodwin Avatar answered Sep 24 '22 06:09

Jon Goodwin