# Canvas scroll

**URL:** <https://www.ruby-forum.com/t/canvas-scroll/168257>\
**Category:** Rails\
**Created:** [May 18, 2009, 8:14pm UTC](https://www.ruby-forum.com/t/canvas-scroll/168257 "2009-05-18T20:14:21Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![mpathare](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9fc29f/32.png) [@mpathare](https://www.ruby-forum.com/u/mpathare)\
**Post date:** [May 18, 2009, 8:14pm UTC](https://www.ruby-forum.com/t/canvas-scroll/168257/1 "2009-05-18T20:14:21Z")

</div>

I am writing a solitaire app. I use a canvas to draw everything on the  
screen. Sometimes a pile of cards will get big and extend below the  
bottom of the screen. I would like to make the canvas scrollable so  
the user can access those cards.

Main Activity.  
Java:

package com.maulinpathare.solitaire;

public class Solitaire extends Activity{  
/\*\* Called when the activity is first created. \*/  
private Context mContext;  
private GameView mG;

```
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mContext = this;
    setContentView(R.layout.main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    boolean result = super.onCreateOptionsMenu(menu);
    menu.add(0, 1, 0, R.string.newGame);
    menu.add(0, 2, 0, R.string.exitGame);
    return result;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case 1:
      mG = new GameView(this);
      setContentView(mG);
        return true;
    case 2:
        finish();
        return true;
    }
    return false;
}

```

}//end class

GameView Class  
This is where everything happens  
I have removed most of the game logic…  
Java:

package com.maulinpathare.solitaire;

public class GameView extends ScrollView implements OnTouchListener{  
private Deck mD;  
private boolean mInit, mDone;  
private Stack mPile1, mPile2, mPile3, mPile4, mPile5,  
mPile6, mPile7,  
mOpenPile, mEndPile1, mEndPile2,  
mEndPile3, mEndPile4, mTemp;  
private Canvas mCanvas;  
private int mPile;  
private boolean mPickUp, mVictory;  
private Card mCard;  
private final int mSpacing = 22;  
private Paint rectPaint, wonPaint1, wonPaint2;

```
 public GameView(Context context) {
    super(context);
                //initialize everything needed for the game
}

@Override
protected void onDraw(Canvas canvas) {
 mCanvas = canvas;
 if(mInit){
      initialize();
      mInit = false;
 }
    //draw stuff...

}//end onDraw

public boolean onTouch(View v, MotionEvent event){
 int eventAction = event.getAction();
 int x = Math.round(event.getX());
 int y = Math.round(event.getY());

 switch(eventAction){
 case MotionEvent.ACTION_DOWN:
                //check if touch has happened on one of the card

```

piles  
break; //end case action\_touch

```
 case MotionEvent.ACTION_MOVE:
                //if card touched move it
      break; //end case action_move

 case MotionEvent.ACTION_UP:
                //check if legal move has been made
      break; //end case action_up

 }//end switch(event)

 invalidate();
 return true;
}//end onTouch

```

}

Where and how should I implement screen scrolling? I would like the  
user to be able to slide their finger across the screen in the  
direction they want to scroll.
