2014-08-31 5 views
-1

이 앱은 기본적으로 사람의 얼굴과 권총, 자동 소총 또는 수류탄으로 촬영할 수있는 '재미있는'앱입니다 (더 많은 무기를 오는 :-)).ImageView.setImageResource가 올바른 drawable을 표시하지 않습니다.

다음 코드와 함께 Weapon 클래스가 있습니다.

public class Weapon { 
    // Parent 
    public static MainActivity ma; 

    // Constants 
    public static final int BULLET_WIDTH = 97; 
    public static final int BULLET_HEIGHT = 92; 
    public static final int EXPLOD_WIDTH = 299; 
    public static final int EXPLOD_HEIGHT = 237; 
    public static final int PISTOL = 0; 
    public static final int RIFLE = 1; 
    public static final int GRENADE = 2; 

    protected static int[] position = {0, 0}; 
    protected static RelativeLayout rl = null; 
    protected static MediaPlayer mp; 

    // Protected stuff 
    // Object variables. 
    protected int type; 
    protected int drawableEffect; 
    protected int soundEffect; 

    // Functionals 
    protected ImageView effectImage; 

    public void equipWeapon() { 
     // Clear any existing listeners and assign a new one. 
     MainActivity.mainImage.setOnTouchListener(null); 
     MainActivity.mainImage.setOnTouchListener(new OnTouchListener() { 

      @Override 
      public boolean onTouch(View v, MotionEvent event) { 
      // Which weapon are we using? 
       switch (type) { 
       case Weapon.PISTOL: 
       case Weapon.GRENADE: 
        if (event.getAction() == MotionEvent.ACTION_DOWN) { 
        // Fire the weapon. 
        fireWeapon(v, event); 
       } 
       case Weapon.RIFLE: 
        switch (event.getAction()) { 
        case MotionEvent.ACTION_DOWN: 
        case MotionEvent.ACTION_MOVE: // This doesn't work ??? 
         // Fire the weapon. 
         fireWeapon(v, event); 
        } 
       } 

       // perfomClick needed. 
       return v.performClick(); 
      } 

     }); 
    } 

    public void fireWeapon(View v, MotionEvent event) { 
     // Reference the layout. 
     Weapon.rl = (RelativeLayout)Weapon.ma.findViewById(R.id.relativeLayout); 

     // Define properties. 
     RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
       RelativeLayout.LayoutParams.WRAP_CONTENT, 
       RelativeLayout.LayoutParams.WRAP_CONTENT);  

     // Get the touch position. 
     Weapon.position[0] = (int)event.getX() - ((type == Weapon.PISTOL || type ==  Weapon.RIFLE) ? Weapon.BULLET_WIDTH:Weapon.EXPLOD_WIDTH); 
     Weapon.position[1] = (int)event.getY() - ((type == Weapon.PISTOL || type ==  Weapon.RIFLE) ? Weapon.BULLET_HEIGHT:Weapon.EXPLOD_HEIGHT); 

     // Set the position. 
     lp.setMargins(Weapon.position[0], Weapon.position[1], 0, 0); 
     effectImage.setLayoutParams(lp); 

     // Add the view to the layout. 
     Weapon.rl.addView(effectImage, lp); 

     // Play the sound. 
     Weapon.mp.seekTo(0); 
     Weapon.mp.start(); 

     // Reload 
     reload(); 
    } 

    public void reload() { 
     // Create the ImageView 
     this.effectImage = new ImageView(Weapon.ma); 
     this.effectImage.setImageResource(this.drawableEffect); 
    } 
} 

을 (내가 입증 된 방법을 따르거나 효율적으로 내가해야 같은 일을 실패하면 자바 그렇게 용서에 아주 새로운 해요) 그리고 무기

public class Pistol extends Weapon { 

    public Pistol() { 
     // First save the type. 
     this.type = Weapon.PISTOL; 

     // Fetch the sound effect and image. 
     this.soundEffect = R.raw.gunshot; 
     this.drawableEffect = R.drawable.bullet_hole1; 

     // Create the media player and initialize the sound. 
     Weapon.mp = MediaPlayer.create(Weapon.ma, this.soundEffect); 

     // Create the ImageView 
     this.effectImage = null; 
     this.effectImage = new ImageView(Weapon.ma); 
     this.effectImage.setImageResource(this.drawableEffect); 
    } 
} 
를 확장하는 권총 클래스가

뿐만 아니라

public class Rifle extends Weapon { 

    public Rifle() { 
     // First save the type. 
     this.type = Weapon.RIFLE; 

     // Fetch the sound effect and image. 
     this.soundEffect = R.raw.gunshot; 
     this.drawableEffect = R.drawable.bullet_hole1; 

     // Create the media player and initialize the sound. 
     Weapon.mp = MediaPlayer.create(Weapon.ma, this.soundEffect); 

     // Create the ImageView 
     this.effectImage = null; 
     this.effectImage = new ImageView(Weapon.ma); 
     this.effectImage.setImageResource(this.drawableEffect); 
    } 
} 

무기

을 연장하는 소총 클래스 마지막으로, 나는 수류탄 클래스 확장을 가지고, 당신이 그것을 짐작 네, 무기뿐만 아니라.

grenadeButton = (ImageButton)findViewById(R.id.grenadeButton); 
    grenadeButton.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      if (MainActivity.this.weapon != Weapon.GRENADE) { 
       // Change the background color. 
       resetButtons(); 
       v.setBackgroundResource(R.drawable.background_selected); 

       // Change weapons. 
       MainActivity.this.currentWeapon = equipWeapon(Weapon.GRENADE); 
      }   
     } 
    }); 

과만큼 무기 방법 :

public class Grenade extends Weapon { 

    public Grenade() { 
     // First save the type. 
     this.type = Weapon.GRENADE; 

     // Fetch the sound effect and image. 
     this.soundEffect = R.raw.grenade; 
     this.drawableEffect = R.drawable.boom; 

     // Create the media player and initialize the sound. 
     Weapon.mp = MediaPlayer.create(Weapon.ma, this.soundEffect); 

     // Create the ImageView 
     this.effectImage = new ImageView(Weapon.ma); 
     this.effectImage.setImageResource(this.drawableEffect); 
    } 
} 

은 내가 그래서 당신은 여기에 .. 원하는대로 무기를 전환 할 수 있습니다에 온 클릭 리스너를 등록 주 화면에서 버튼을 예를 들어 하나가 주요 활동.

public Weapon equipWeapon(int type) { 
    Weapon weapon = null; 
    switch (type) { 
    case Weapon.PISTOL: 
     weapon = new Pistol(); 
     break; 
    case Weapon.RIFLE: 
     weapon = new Rifle(); 
     break; 
    case Weapon.GRENADE: 
     weapon = new Grenade(); 
     break; 
    } 

    // Play a sound and save changes. 
    MainActivity.this.weapon = type; 
    MainActivity.mp.seekTo(0); 
    MainActivity.mp.start(); 

    return weapon; 
} 

이제이 코드를 모두 검토해 주셔서 감사합니다. 나는 그것이 내가 간단한 문제라고 가정하고있는 것을 소화 할 것이 많다는 것을 알고있다. 또한이 포럼의 규칙을 알고 있으며 이미이 문제를 검색하려고 시도했지만 올바른 쿼리를 사용하고 있는지 확실하지 않습니다.이 문제와 관련된 내용을 찾지 못했습니다.

여기에 우리가 간다 : 당신이 응용 프로그램을 시작하면 바로 촬영을 시작할 수 있도록, 권총이 자동으로

을 갖추고 있습니다. 그것은 잘 쏘고 총알 소리가 나고 사람의 얼굴 전체에 총알 구멍이 생깁니다. 라이플은 잘 작동합니다 (하지만 권총과 동일한 잡음과 그래픽을 사용하기 때문에 실패 할 수도 있습니다). 내가 수류탄로 전환하면

은, 여기에 내가 사랑스러운 폭발 소리 만 표시되는 이미지는 내가 어떻게이 문제를 해결하는 지상의 생각이 나는 ..

을 난처한 상황에 빠진입니다 .. 여전히 총알 구멍이 아닌 폭발하다 나는 수류탄 개체를 만들 때 내가 특별히 인스턴스 변수에 그리기 가능 자원을 할당

...

this.drawableEffect = R.drawable.boom; 

그러나 여전히 R.drawable.bullet_hole1를 표시합니다 ....

날 경우 알려 주시기 바랍니다 더 많은 정보가 필요하며 질문이 있으면 y OU는 .....

+0

아래쪽 투표는 무엇입니까? 알아낼 수 없었기 때문에 여기에 왔습니다 ... 여기에 질문을하기위한 지침을 따랐습니다 (상황을 철저히 설명하고 코드 등을 제공했습니다). –

답변

0

내가 너무 바보 야 ..

이 시간 내 주셔서 감사 ..에 따라서

MainActivity.equipWeapon 

호출되지

Weapon.equipWeapon.... 

필요 무기가 바뀌지 않았어 ... 오이 'vey ...

관련 문제