简体   繁体   中英

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 :

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

bby is drawn and work fine,
my problem is How can i change the Resource (R.drawble.babybutton) to a different resource for eg (R.drawble.babybutton2) on Touch?

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. than you can override this method in your surfaceview class.

@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. after initialize the surfaceView . pass this surfaceview to the

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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