如何在android中防止设备轮换的冲突动画?

时间:2017-01-11 15:11:00

标签: java android android-animation screen-rotation

我在活动中有两个按钮对象此对象具有一些使用ObjectAnimator的移动动画这些动画完美无缺,但是当动画在背景中运行时我想要旋转设备有时在ObjectAnimator中的纵向模式坐标以横向模式运行或反向运行。

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

在上面的代码中,按钮应该离开屏幕但是在此行之后,如果设备旋转到横向模式按钮进入中间屏幕,我认为当出现此问题时,纵向坐标以横向模式运行。

我在图片中提供了更多细节:

  1. 这是我的图片查看器,当用户没有触摸屏幕按钮时,侧面有两个按钮,屏幕上出现动画:
  2. enter image description here

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

        动画以纵向模式运行时第三张图像中的
      1. 我旋转到横向模式 并且箭头停留在屏幕的中央,当然,如果再次旋转到横向箭头,此图像将进入纵向模式:
      2. enter image description here

        这种情况恰好发生在移动动画(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

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

1 个答案:

答案 0 :(得分:0)

暂时屏幕锁定,您可以轻松使用:

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