简体   繁体   English

如何在TouchEvent上更改位图资源

[英]How To Change The Bitmap Resource On TouchEvent

This Should be easy and simple right?! 这应该简单易行吗?!
So, i have a Gameobject as a Button i reference it on a GamePanel that extents SurfaceView like this : 所以,我有一个Gameobject作为Button我在GamePanel上引用它,它扩展了SurfaceView如下所示:

bby = new Buttons(BitmapFactory.decodeResource(getResources(), R.drawable.babybutton),123 ,114 ,1);    

bby is drawn and work fine, bby被绘制并且工作正常,
my problem is How can i change the Resource (R.drawble.babybutton) to a different resource for eg (R.drawble.babybutton2) on Touch? 我的问题是如何在Touch上将资源(R.drawble.babybutton)更改为不同的资源(例如(R.drawble.babybutton2)?

just like pressing buttons animations! 就像按下按钮动画一样! Thanks in advance 提前致谢
(If my question look stupid please don't dislike! i'm very new to this). (如果我的问题看起来很愚蠢,请不要讨厌!我对此很新)。

if you want to change the button's drawable on touch on the surfaceview. 如果你想在surfaceview上触摸按钮的drawable。 than you can override this method in your surfaceview class. 您可以在surfaceview类中覆盖此方法。

@Override
public boolean onTouch(View v, MotionEvent event) {
    switch(event.getAction()){
    case MotionEvent.ACTION_DOWN:
        //here you can set an another drawable to your button or can do something else
        break;
    }
    return true;
}

In your activity set a touchListener on the surfaceview. 在您的活动中,在surfaceview上设置touchListener。 after initialize the surfaceView . 初始化surfaceView pass this surfaceview to the 将此surfaceview传递给

surfaceView.setOnTouchListener(surfaceView)

I may have misunderstood you, but if you want to change the button's rescource, this is how you should do it: 我可能误解了你,但是如果你想改变按钮的资源,你应该这样做:

button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                button.setBackgroundResource(R.drawable.icon);
            }
        });
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM