动画武器ontouch

时间:2013-12-30 13:17:16

标签: android animation touch media-player

大家好我想实现一个方法来触摸我的ak47图像,我已经编写了代码,但该方法在触摸时不起作用。如果你触摸我的代码中开始重复的枪,我想这样。提前致谢

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;
}     

}});








     }
}

3 个答案:

答案 0 :(得分:0)

我认为没有人可以帮助您 - 除非您完全发布您的代码。有很多东西丢失......你的OnTouchListener甚至不检查触摸事件,如ACTION_DOWN,ACTION_MOVE或其他任何东西。

答案 1 :(得分:0)

我建议你在用户点击时采取行动,即

public boolean onTouch(View v, MotionEvent event) {
    switch(event.getAction())
    {
        case MotionEvent.ACTION_DOWN:

            // Do your firing even
            break;

        case MotionEvent.ACTION_UP:
            //action when user removes finger.
            break;

        default:
            return false;

    } // end of switch case

    return true;
}

另外,只需在onTouch()中添加一个断点并调试以了解正在发生的事情以及未执行的部分并相应地进行修复。

答案 2 :(得分:0)

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;
                             }     

               }});








     }

我改变了这种方式,但每次动画从0开始时,有一种方法可以继续动作并且不会停止并再次启动它吗?