samedi 2 mai 2015

How to make my ImageView responsive to onClickevents while .startAnimation() is running?

I'm new to android, and I'm facing a problem where I'm trying to make my Imageview, which is playing an animation through startAnimation(), to become Invisible as the user clicks on it. what happens however is that only after the animation is done the imageview becomes invisble. my assumptions was it's because they both run on the UI thread. surprisingly however, when I changed my event handling from setVisibility to startAnimation() it actually listened to the click, interrupted the animation and restarted it! to override my problem, which did turn out nice, I made a new Animation object that opposite to the first, which shows the Imageview, hides the imageview and it worked perfectly, but I'm still curious as to why my Imageview was selectively responsive to different event handling?

this Code change visiblity only after the animation is done

  protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        face = (ImageView)findViewById(R.id.face);
        final Animation showAnimation=AnimationUtils.loadAnimation(MainActivity.this,R.anim.animation);
        face.startAnimation(showAnimation);
        face.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                face.setVisibility(View.INVISIBLE);

            }
        });

this code changes the animation "while" the original is playing

  protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        face = (ImageView)findViewById(R.id.face);
        final Animation showAnimation=AnimationUtils.loadAnimation(MainActivity.this,R.anim.animation);
        face.startAnimation(showAnimation);
        face.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Animation hideAnimation = AnimationUtils.loadAnimation(MainActivity.this,R.anim.animation1);
                face.startAnimation(hideAnimation);

            }
        });

Aucun commentaire:

Enregistrer un commentaire