Reputation: 199
I have an imageView that currently just moves from left to right of the screen with translateAnimation. I would like the user to click the imageView as it goes across the screen and then this would set the ImageView to INVISIBLE. My problem is because I use Translate Animation this can not be done. What is the way I should do this? and could an example be provided.
My code:
package com.example.mr_br.ibcc_bomber_command;
import android.content.pm.ActivityInfo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.RelativeLayout;
public class front_gunner extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_front_gunner);
//sets screen orientation on created
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
//animation
final TranslateAnimation moveLefttoRight = new TranslateAnimation(-400, 2500, 0, 0);
moveLefttoRight.setDuration(10000);
moveLefttoRight.setFillAfter(false);
//enemies
final ImageView enemy1 = (ImageView) findViewById(R.id.enemy1);
enemy1.setAnimation(moveLefttoRight);
enemy1.setOnClickListener (new View.OnClickListener(){
@Override
public void onClick(View v) {
enemy1.setVisibility(View.INVISIBLE);
}
});
}
}
Thank you.
Upvotes: 0
Views: 76
Reputation: 62189
The fluent approach is much more pleasant:
enemy1.animate()
.translationX(2500.0f)
.setDuration(10000)
.withStartAction(() -> enemy1.setTranslationX(-400.0f));
Upvotes: 1
Reputation: 199
The solution I came to:
ObjectAnimator anim = ObjectAnimator.ofFloat(enemy1, "translationX", -400f, 2500f);
anim.setDuration(10000); // Duration in milliseconds
anim.start();
I used a different type of animator. This worked great. If anyone else encounters the same issue this is a decent solution.
Upvotes: 1