我在活动中有一个两个按钮对象这个对象有一些动画与ObjectAnimator这些动画工作完美但当动画在背景中运行时我想旋转设备有时在ObjectAnimator中纵向模式坐标以横向模式或反向运行.
ObjectAnimator move_next_btn = ObjectAnimator.ofFloat(next_btn, "x", next_btn.getX(), next_btn.getX() + next_btn.getWidth());
在上面的代码中,按钮应该离开屏幕,但是在此行之后,如果设备旋转到横向模式按钮进入中间屏幕,我认为当出现此问题时,纵向坐标在横向模式下运行.
我在图片中给出了更多细节:
这是我的图片查看器,当用户没有触摸屏幕按钮时,侧面有两个按钮,屏幕上出现了动画:
第二个图像显示其中一个箭头是隐藏(也许)我认为这是因为当动画想要运行时我旋转屏幕并且箭头保持在屏幕外:
在第三张图像中,当动画以纵向模式运行时,我旋转到横向模式,箭头停留在屏幕中央,当然,如果再次旋转到横向箭头,此图像将进入纵向模式:
这恰好发生在移动动画(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); } }); }
我记录了我的问题: 我的问题视频在youtube上
什么是防止这个问题的最佳方法?