【问题标题】:How to prevent conflict animation with device rotation in android?如何防止在android中与设备旋转冲突动画?
【发布时间】:2017-01-11 15:10:59
【问题描述】:

我在活动中有两个按钮对象,这个对象有一些带有 ObjectAnimator 的移动动画,这些动画效果很好,但是当动画在后台运行时,我想在 ObjectAnimator 中以横向模式或反向运行时旋转设备几次纵向模式坐标。

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

在上面的代码中,按钮应该离开屏幕但是在这条线运行之后,如果设备旋转到横向模式按钮会在中间屏幕上我认为当这个问题发生时,纵向坐标在横向模式下运行。

我用图片给出更多细节:

  1. 这是我的图片查看器,当用户不触摸屏幕时,侧面有两个按钮,动画按钮消失:

  1. 第二张图片显示其中一个箭头是隐藏的(也许)我认为这是因为当动画想要运行时我旋转了屏幕并且箭头留在了屏幕外:

  1. 在第三张图片中,当动画以纵向模式运行时,我旋转到横向模式 和箭头停留在屏幕的中心,当然,如果再次旋转到横向,此图像将进入纵向模式箭头继续居中:

这种情况恰好发生在移动动画(上面的代码中的 int)和屏幕旋转同时运行时,并非总是如此。

完整代码:

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);
            }
        });
    }

我记录了我的问题: my problem video on youtube

防止此问题的最佳方法是什么?

【问题讨论】:

  • 你能发布一些关于你的问题的视频或图片吗?目前还不是很清楚,你的问题是什么。您的问题会在屏幕旋转后出现一次,还是总是在方向更改后出现?
  • 谢谢@rom4ek。我已经编辑了我的帖子。
  • 还是很难理解
  • @LeandroBorgesFerreira 我不知道如何解释问题,但是如果我旋转屏幕,当动画开始时,所有这些事情都会发生,右键坐标设置错误并且按钮转到屏幕中心这种情况很少发生。我发送所有代码可能是你有什么问题。
  • 为什么不在动画运行时锁定方向,等动画结束再解锁?

标签: java android android-animation screen-rotation


【解决方案1】:

对于您可以轻松使用的临时屏幕锁定:

//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);

【讨论】:

    猜你喜欢
    • 2013-02-07
    • 2023-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多