Search code examples
androidanimationtouchmedia-player

Animate weapon ontouch


Hello everyone I would like to implement a method to touch on my image of a ak47, I have written the code but the method does not work on touch as it should. I would like that if you touch the gun that starts the repetition in my code. Thanks in advance

package org.realgunshot;

import android.app.Activity;
import android.app.ActionBar.LayoutParams;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.os.Handler;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

public class B2 extends Activity   {



ImageView bulletshot,bulletempty ;

Boolean click=true;

TextView Display;
Button b1;
int counter = 15 ;
Object mediaPlayerspara,mediaPlayersload;



@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.b2);
bulletshot=(ImageView)findViewById(R.id.bulletshot);
bulletempty=(ImageView)findViewById(R.id.bulletempty);



b1 = (Button)findViewById(R.id.b1);
Display = (TextView)findViewById(R.id.counter);



final Animation animRotate = AnimationUtils.loadAnimation(this, R.anim.gun);
final Animation animgunshot = AnimationUtils.loadAnimation(this, R.anim.gunshot);
final Animation animbulletempty = AnimationUtils.loadAnimation(this, R.anim.bulletempty);



b1.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {





counter --;
Display.setText(""+counter);

if(counter <= 0)
{
counter=1;

mediaPlayerspara = null;

mediaPlayersload = MediaPlayer.create(getBaseContext(),R.raw.sload);
((MediaPlayer) mediaPlayersload).setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mediaPlayersload) {
// TODO Auto-generated method stub

mediaPlayersload.start();

mediaPlayersload.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mediaPlayersload) {
mediaPlayersload.release();

 };
 });
}
});
}
else{

bulletshot.setVisibility(View.VISIBLE);
bulletshot.startAnimation(animgunshot);

Handler handlers = new Handler(); 
handlers.postDelayed(new Runnable() { 
public void run() { 
bulletshot.setVisibility(View.GONE);
bulletempty.setVisibility(View.GONE);
} 
}, 300); 

bulletempty.setVisibility(View.VISIBLE);
bulletempty.startAnimation(animbulletempty);

Handler handlers2 = new Handler(); 
handlers2.postDelayed(new Runnable() { 
public void run() { 
bulletshot.setVisibility(View.GONE);
bulletempty.setVisibility(View.GONE);
} 
}, 2000); 

v.startAnimation(animRotate);
final Button button1 = (Button)findViewById(R.id.b1);
button1.setBackgroundResource(R.drawable.ak47_1);
Handler handler = new Handler(); 
handler.postDelayed(new Runnable() { 
public void run() { 
button1.setBackgroundResource(R.drawable.ak47_0); 
 } 
}, 100); 




mediaPlayerspara = MediaPlayer.create(getBaseContext(),R.raw.ak47);
((MediaPlayer) mediaPlayerspara).setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mediaPlayerspara) {
 // TODO Auto-generated method stub

mediaPlayerspara.start();

mediaPlayerspara.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mediaPlayerspara) {
mediaPlayerspara.release();

};
});
}
});}
if(event.getAction() == MotionEvent.ACTION_UP){



return true;
}
return false;



}

});







Button apri = (Button)findViewById(R.id.loadoadd);
apri.setOnClickListener(new Button.OnClickListener(){
@Override
 public void onClick(View v) {
                  // TODO Auto-generated method stub

LayoutInflater layoutInflater= (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);  
final View popupView = layoutInflater.inflate(R.layout.loadoadd, null);  



final PopupWindow popupWindow = new PopupWindow(popupView,LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);  

 popupWindow.setAnimationStyle(R.style.Animation);

Button chiudi1 = (Button)popupView.findViewById(R.id.dismiss);

chiudi1.setOnClickListener(new Button.OnClickListener(){



@Override
public void onClick(View v) {
 // TODO Auto-generated method stub




popupWindow.dismiss();

}});


 Button load = (Button)popupView.findViewById(R.id.load);
 load.setOnClickListener(new Button.OnClickListener(){



@Override
public void onClick(View v) {
 // TODO Auto-generated method stub
counter = 15;
Display.setText(""+counter);

}});

Button add = (Button)popupView.findViewById(R.id.add);
add.setOnClickListener(new Button.OnClickListener(){



 @Override
public void onClick(View v) {
// TODO Auto-generated method stub
counter = 1015;
Display.setText(""+counter);

}});
if (click){
popupWindow.showAtLocation(chiudi1, Gravity.CENTER, 0, 0);

click=false;
}else{
popupWindow.dismiss();
click=true;
}     

}});








     }
}

Solution

  • package org.realgunshot;
    
    import android.app.Activity;
    import android.app.ActionBar.LayoutParams;
    import android.media.MediaPlayer;
    import android.media.MediaPlayer.OnCompletionListener;
    import android.os.Bundle;
    import android.os.Handler;
    import android.view.Gravity;
    import android.view.LayoutInflater;
    import android.view.MotionEvent;
    import android.view.View;
    import android.view.View.OnTouchListener;
    import android.view.animation.Animation;
    import android.view.animation.AnimationUtils;
    import android.widget.Button;
    import android.widget.ImageView;
    import android.widget.PopupWindow;
    import android.widget.TextView;
    
    public class B2 extends Activity   {
    
    
    
    
    
    
    
    
    
    
    
    
    ImageView proiettilesparo,proiettilevuoto ;
    
        Boolean click=true;
    
        TextView Display;
        Button b1;
        int contatore = 15 ;
         Object mediaPlayerspara,mediaPlayerscarica;
    
    
    
    
    
    
    
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.b2);
            proiettilesparo=(ImageView)findViewById(R.id.proiettilesparo);
            proiettilevuoto=(ImageView)findViewById(R.id.proiettilecade);
    
    
    
            b1 = (Button)findViewById(R.id.b1);
            Display = (TextView)findViewById(R.id.contatore);
    
    
    
            final Animation animRotate = AnimationUtils.loadAnimation(this, R.anim.pistola);
            final Animation animsparoproiettile = AnimationUtils.loadAnimation(this, R.anim.sparoproiettile);
            final Animation animproiettilecade = AnimationUtils.loadAnimation(this, R.anim.proiettilecade);
    
    
    
            b1.setOnTouchListener(new OnTouchListener() {
                private Handler mHandler;
                @Override
                public boolean onTouch(View v, MotionEvent event) {
    
                     switch(event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        if (mHandler != null) return true;
                        mHandler = new Handler();
                        mHandler.postDelayed(mAction, 1);
    
                        break;
                    case MotionEvent.ACTION_UP:
                        if (mHandler == null) return true;
                        mHandler.removeCallbacks(mAction);
                        mHandler = null;
    
                        break;
                    }
    
                            return false;
    
    
    
                }
              Runnable mAction = new Runnable() {
                  @Override public void run() {
                      contatore --;
                      Display.setText(""+contatore);
    
                      if(contatore <= 0)
                {
                        contatore=1;
    
                        mediaPlayerspara = null;
    
                        mediaPlayerscarica = MediaPlayer.create(getBaseContext(),R.raw.scarica);
                        ((MediaPlayer) mediaPlayerscarica).setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                                @Override
                                public void onPrepared(MediaPlayer mediaPlayerscarica) {
                                    // TODO Auto-generated method stub
    
                                    mediaPlayerscarica.start();
    
                                    mediaPlayerscarica.setOnCompletionListener(new OnCompletionListener() {
                                        public void onCompletion(MediaPlayer mediaPlayerscarica) {
                                            mediaPlayerscarica.release();
    
                                        };
                                    });
                                        }
                                    });
                         }
                      else{
    
                          proiettilesparo.setVisibility(View.VISIBLE);
                            proiettilesparo.startAnimation(animsparoproiettile);
    
                            Handler handlers = new Handler(); 
                            handlers.postDelayed(new Runnable() { 
                                 public void run() { 
                                     proiettilesparo.setVisibility(View.GONE);
                                     proiettilevuoto.setVisibility(View.GONE);
                                 } 
                            }, 300); 
    
                            proiettilevuoto.setVisibility(View.VISIBLE);
                            proiettilevuoto.startAnimation(animproiettilecade);
    
                            Handler handlers2 = new Handler(); 
                            handlers2.postDelayed(new Runnable() { 
                                 public void run() { 
                                     proiettilesparo.setVisibility(View.GONE);
                                     proiettilevuoto.setVisibility(View.GONE);
                                 } 
                            }, 2000); 
    
                b1.startAnimation(animRotate);
                final Button button1 = (Button)findViewById(R.id.b1);
                button1.setBackgroundResource(R.drawable.ak47_1);
                Handler handler = new Handler(); 
                handler.postDelayed(new Runnable() { 
                     public void run() { 
                         button1.setBackgroundResource(R.drawable.ak47_0); 
                     } 
                }, 100); 
    
    
    
    
                mediaPlayerspara = MediaPlayer.create(getBaseContext(),R.raw.ak47);
                ((MediaPlayer) mediaPlayerspara).setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                        @Override
                        public void onPrepared(MediaPlayer mediaPlayerspara) {
                            // TODO Auto-generated method stub
    
                            mediaPlayerspara.start();
    
                            mediaPlayerspara.setOnCompletionListener(new OnCompletionListener() {
                                public void onCompletion(MediaPlayer mediaPlayerspara) {
                                    mediaPlayerspara.release();
    
                                };
                            });
                                }
                            });}
                      mHandler.postDelayed(this, 100);
                  }
              };
    
            });
    
    
    
    
    
    
    
            Button apri = (Button)findViewById(R.id.caricaoaggiungi);
              apri.setOnClickListener(new Button.OnClickListener(){
                   @Override
                     public void onClick(View v) {
                      // TODO Auto-generated method stub
    
                       LayoutInflater layoutInflater= (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);  
                        final View popupView = layoutInflater.inflate(R.layout.caricaoaggiungi, null);  
    
    
    
                        final PopupWindow popupWindow = new PopupWindow(popupView,LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);  
    
                       popupWindow.setAnimationStyle(R.style.Animation);
    
                                 Button chiudi1 = (Button)popupView.findViewById(R.id.dismiss);
    
                                 chiudi1.setOnClickListener(new Button.OnClickListener(){
    
    
    
                         @Override
                         public void onClick(View v) {
                          // TODO Auto-generated method stub
    
    
    
    
                          popupWindow.dismiss();
    
                         }});
    
    
                                 Button carica = (Button)popupView.findViewById(R.id.carica);
                                 carica.setOnClickListener(new Button.OnClickListener(){
    
    
    
                         @Override
                         public void onClick(View v) {
                          // TODO Auto-generated method stub
                            contatore = 15;
                            Display.setText(""+contatore);
    
                         }});
    
                               Button aggiungi = (Button)popupView.findViewById(R.id.aggiungi);
                                aggiungi.setOnClickListener(new Button.OnClickListener(){
    
    
    
                         @Override
                         public void onClick(View v) {
                          // TODO Auto-generated method stub
                             contatore = 1015;
                             Display.setText(""+contatore);
    
                         }});
                                if (click){
                                     popupWindow.showAtLocation(chiudi1, Gravity.CENTER, 0, 0);
    
                                     click=false;
                                 }else{
                                     popupWindow.dismiss();
                                     click=true;
                                 }     
    
                   }});
    
    
    
    
    
    
    
    
         }
    

    I changed it this way, but every time the animation starts from 0, there is a way to continue an action and does not stop and start it again?