-1

I want two images to keep changing continuously while the button is pressed down using ontouchlistener in ACTION_DOWN state..

I am using image view to switch images.

Here is the code i want to implement it, i am also playing the sound in loop with which i want to play two images simultaneously when ACTION_DOWN

 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final Button zero = (Button) this.findViewById(R.id.button1);
    zero.setOnTouchListener(this);

    mp = MediaPlayer.create(this, R.raw.sound);

}

@Override
public boolean onTouch(View v, MotionEvent event) 
{   

    switch (event.getAction()) 
    {

    case MotionEvent.ACTION_DOWN:
    {
        mp.setLooping(true);
        mp.start();
    }

    break;
    case MotionEvent.ACTION_UP:
    {
       mp.pause();
    }
    break;
}

return true;
}

}

Aleem Ahmed
  • 61
  • 1
  • 9

1 Answers1

0

Use the below function:

 public static void ImageViewAnimatedChange(Context c, final ImageView v, final Bitmap new_image) {
        final Animation anim_out = AnimationUtils.loadAnimation(c, android.R.anim.fade_out); 
        final Animation anim_in  = AnimationUtils.loadAnimation(c, android.R.anim.fade_in); 
        anim_out.setAnimationListener(new AnimationListener()
        {
            @Override public void onAnimationStart(Animation animation) {}
            @Override public void onAnimationRepeat(Animation animation) {}
            @Override public void onAnimationEnd(Animation animation)
            {
                v.setImageBitmap(new_image); 
                anim_in.setAnimationListener(new AnimationListener() {
                    @Override public void onAnimationStart(Animation animation) {}
                    @Override public void onAnimationRepeat(Animation animation) {}
                    @Override public void onAnimationEnd(Animation animation) {}
                });
                v.startAnimation(anim_in);
            }
        });
        v.startAnimation(anim_out);
    }
Skynet
  • 7,820
  • 5
  • 44
  • 80
  • Thanks for alot of effort you are putting up for me bro. Just a little thing, i cant figure out how to pressing a button with ontouchlistener can this code get along, i dont really know how to connect these both things. – Aleem Ahmed Feb 05 '15 at 11:03
  • You just call the function where you need to change the image, that is in your onClick listener of zero button. Your onTouch Listener needs major changes as the cases are not proper: `break;` should come inside those curly braces. Just call the function depending on your case pass the `context`, the `imageView` and your `bitmap` – Skynet Feb 05 '15 at 11:14
  • Also you should consider changing your onTouch listener to an onClick listener, `MotionEvent.ACTION_UP` will be executed as soon as the user lifts her finger. So that is of no use to you. – Skynet Feb 05 '15 at 11:16
  • A huge favor man, i would love you for this, Please if you could write me code for playing two images when the button is pressed and stops when the finger is up, following up with the sound clip playing in the loop, which works the same as the images, stops when the finger is up. I really tried to do it but i am going nutts :/ – Aleem Ahmed Feb 05 '15 at 11:35