Tuesday 8 January 2013

Adding Bitmap Image into Canvas in Android


Hi,
                    I’m developing  a Maze Game in Android.    I  faced a problem with adding the image into Canvas. Actually I draw the circle by using canvas is working but I need to add the image from drawable.This is for Sample code for draw circle by using canvas….
Paint ball = new Paint();
ball.setTextSize(cellHeight*0.75f);
canvas.drawCircle((currentX * totalCellWidth)+(cellWidth/2),   //x of center
                  (currentY * totalCellHeight)+(cellWidth/2),  //y of center
                  (cellWidth*0.45f), ball);

                   Here  In this place I want add the image into canvas from drawable..
I use the below code for adding image into canvas.but it is not working touch Listener to drawable image.
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.green);
canvas.drawBitmap(bmp,current,currentY ,null);   
                          
            Finally   I got the solution to this touch event functionality for drawable image. Following code I used its working if any other way is their please let me know.

Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.green);
       Bitmap img = Bitmap.createScaledBitmap( bmp,25,25, true );
canvas.drawBitmap(img,(currentX * totalCellWidth)+(cellWidth/6) ,(currentY * totalCellHeight)+(cellWidth/6),null);

And In the onTouchEvent method. I use the below code:

public boolean onTouchEvent(MotionEvent event) {
float touchX = event.getX();
float touchY = event.getY();
int currentX = maze.getCurrentX();
int currentY = maze.getCurrentY();
switch (event.getAction() & MotionEvent.ACTION_MASK) {
   case MotionEvent.ACTION_DOWN:
                //touch gesture started
                if(Math.floor(touchX/totalCellWidth) == currentX &&
                Math.floor(touchY/totalCellHeight) == currentY) {
                //touch gesture in the cell where the ball is
                dragging = true;
                return true;
                }
                break;
   case MotionEvent.ACTION_UP:
                //touch gesture completed
                dragging = false;
                return true;
                case MotionEvent.ACTION_MOVE:
                if(dragging) {
                int cellX = (int)Math.floor(touchX/totalCellWidth);
                int cellY = (int)Math.floor(touchY/totalCellHeight);                                                          
                if((cellX != currentX && cellY == currentY) ||
                (cellY != currentY && cellX == currentX)) {
                //either X or Y changed
                boolean moved = false;
                //check horizontal ball movement
        switch(cellX-currentX) {
                case 1:
                moved = maze.move(Maze.RIGHT);
                break;
                case -1:
                moved = maze.move(Maze.LEFT);
                }
                //check vertical ball movement
                switch(cellY-currentY) {
                case 1:
                moved = maze.move(Maze.DOWN);
                break;
                case -1:
                moved = maze.move(Maze.UP);
                }
                if(moved) {
                //the ball was moved so we'll redraw the view
                invalidate();
                if(maze.isGameComplete()) {
                                //game is finished
                                showFinishDialog();
                }
                  }
                          }
                return true;
                      }
                           }
                 return false;
                }

No comments:

Post a Comment