ImageView.setImageResource未显示正确的drawable

时间:2014-08-31 06:33:10

标签: java android view imageview drawable

我有这个应用程序,它基本上是一个很有趣的' app我是用一个人的脸做的,能用手枪,自动步枪或手榴弹射击它(更多的武器来了 - ))。

我有一个Weapon类,代码如下。 (我对java很新,所以请原谅我,如果我没有按照行之有效的方式,或者按照我的要求做有效的事情)

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

最后,我有一个延伸的手榴弹类,是的,你猜对了,也是武器。

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

我在主视图中有按钮,我已经注册了onClick听众,所以你可以根据自己的喜好切换武器..这里有一个例子:

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

和MainActivity中的装备武器方法。

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 ....

如果您需要更多信息并询问您需要的任何问题,请告知我们。

感谢您的时间..

1 个答案:

答案 0 :(得分:0)

我真是太蠢了......

MainActivity.equipWeapon 

没有打电话

Weapon.equipWeapon.... 

所以武器没有改变...... Oy'维伊...