Reputation: 238
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:
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?
Upvotes: 15
Views: 1159
Reputation: 9153
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);
Upvotes: 1