Making Bitmaps Clickable in SurfaceView Android Studio

Viewed 38

I have this screen with two bird animatons using Bitmap. I want the bitmaps to be clickable but I am unable to figure out how. I can easily make the entire screen clickable by setting view.OnClickListener but that is not what I want, I want separate things to happen depending on which bird the user clicks on.

Also, how and where would I create a local variable for my bitmaps, for now I can only access them inside of the draw() method.

The run() method is for the animation and works fine.

I am new to all of this and appreciate any help!

Code

public class PlayActivity extends AppCompatActivity {

surfaceView view;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    view = new surfaceView(this);
    setContentView(view);

}

public class surfaceView extends SurfaceView {


    public surfaceView(Context context) {
        super(context);
        new Anim().start();
    }


    private class Anim extends Thread {
        int counter = 0;
        int counter2 = 0;



        @Override
        public void run() {
            long last_updated_time = 0;
            long delay = 150;

            int[] purple_bird = {
                    R.drawable.bird1,
                    R.drawable.bird2
            };
            int[] red_bird = {
                    R.drawable.red1,
                    R.drawable.red2,
                    R.drawable.red3,
                    R.drawable.red4,
                    R.drawable.red5,
                    R.drawable.red6,
                    R.drawable.red7,
                    R.drawable.red8,
                    R.drawable.red9,
                    R.drawable.red10
            };


            while (true) {
                boolean playing = true;
                if (playing) {

                    long current_time = System.currentTimeMillis();
                    if (current_time > last_updated_time + delay) {
                        if (counter >= 2) {
                            counter = 0;
                        }
                        if (counter2 >= 4) {
                            counter2 = 0;
                        }
                        draw(purple_bird[counter], red_bird[counter2]);

                        last_updated_time = current_time;
                        counter++;
                        counter2++;
                    }
                }
            }




        }

        private void draw(int red_bird, int purple_bird) {

            SurfaceHolder holder = getHolder();
            Canvas canvas = holder.lockCanvas();

            if (canvas != null) {

                canvas.drawColor(Color.WHITE);
                Paint paint = new Paint();

                Bitmap purpleBird = BitmapFactory.decodeResource(getContext().getResources(), purple_bird);
                Bitmap redBird = BitmapFactory.decodeResource(getContext().getResources(), red_bird);


                Bitmap resizedRedBird = Bitmap.createScaledBitmap(redBird, (int) (redBird.getWidth() * 0.1), (int) (redBird.getHeight() * 0.1), true);
                Bitmap resizedPurpleBird = Bitmap.createScaledBitmap(purpleBird, (int) (purpleBird.getWidth() * 0.8), (int) (purpleBird.getHeight() * 0.8), true);

                canvas.drawBitmap(resizedRedBird, 100, 100, paint);
                canvas.drawBitmap(resizedPurpleBird, 100, 600, paint);


                holder.unlockCanvasAndPost(canvas);

}
        }
    }
}

}

0 Answers
Related