Hi All,
I am creating a CustomView with bigger image based on tutorial on and
developers forum.
It is scolling fine.I need exact pixel coordinates of my image.I am getting
that also.
now I want to draw some images upon scrolling image.I am able to draw images
using onDraw().But images are not getting scrolled.Any idea how to resolve
this?
My code is
public class LargeImageScroller extends Activity {
// Physical display width and height.
private static int displayWidth = 0;
private static int displayHeight = 0;
private static Vector<Square> squares;
private static Vector<Tile> tiles;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// displayWidth and displayHeight will change depending on screen
// orientation. To get these dynamically, we should hook
onSizeChanged().
// This simple example uses only landscape mode, so it's ok to get
them
// once on startup and use those values throughout.
Display display = ((WindowManager)
getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
displayWidth = display.getWidth();
displayHeight = display.getHeight();
squares=Util.generateBoardSquares();
tiles=Util.generateTiles(this);
// SampleView constructor must be constructed last as it needs the
// displayWidth and displayHeight we just got.
setContentView(new SampleView(this));
}
private static class SampleView extends View {
private static Bitmap bmLargeImage; //bitmap large enough to be
scrolled
private static Rect displayRect = null; //rect we display to
private Rect scrollRect = null; //rect we scroll over our bitmap
with
private int scrollRectX = 0; //current left location of scroll rect
private int scrollRectY = 0; //current top location of scroll rect
private float scrollByX = 0; //x amount to scroll by
private float scrollByY = 0; //y amount to scroll by
private float startX = 0; //track x from one ACTION_MOVE to the next
private float startY = 0; //track y from one ACTION_MOVE to the next
private final int boardHeight=480-150;
private static Tile movingTile=null;
public SampleView(Context context) {
super(context);
// Destination rect for our main canvas draw. It never changes.
displayRect = new Rect(0, 0, displayWidth, boardHeight);
// Scroll rect: this will be used to 'scroll around' over the
// bitmap in memory. Initialize as above.
scrollRect = new Rect(0, 0, displayWidth, boardHeight);
// Load a large bitmap into an offscreen area of memory.
bmLargeImage = BitmapFactory.decodeResource(getResources(),
R.drawable.woodstandardboard);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// Remember our initial down event location.
///validating for tile touch
int touchx=(int)event.getX();
int touchy=(int)event.getY();
for(int i=0;i<tiles.size();i++){
Tile tile=tiles.get(i);
if(tile.isTouched(touchx, touchy)){
tile.setX(touchx);
tile.setY(touchy);
movingTile=tile;
invalidate();
}
}
if(movingTile != null){
break;
}
if((int)event.getY() > boardHeight){
break;
}
startX = event.getRawX();
startY = event.getRawY();
break;
case MotionEvent.ACTION_MOVE:
if(movingTile != null){
int touchedx=(int)event.getX();
int touchedy=(int)event.getY();
movingTile.setX(touchedx-Util.SQUARE_DIMENSIONS/2);
movingTile.setY(touchedy-Util.SQUARE_DIMENSIONS/2);
invalidate();
break;
}
if((int)event.getY() > boardHeight){
break;
}
float x = event.getRawX();
float y = event.getRawY();
// Calculate move update. This will happen many times
// during the course of a single movement gesture.
scrollByX = x - startX; //move update x increment
scrollByY = y - startY; //move update y increment
startX = x; //reset initial values to latest
startY = y;
invalidate(); //force a redraw
break;
case MotionEvent.ACTION_UP:
///check if touch is on board
int boardx=(int)event.getX()+(int)scrollRectX;
int boardy=(int)event.getY()+(int)scrollRectY;
System.out.println(boardx);
if(movingTile != null){
int id=Util.checkSquareTouched(squares, boardx,
boardy);
System.out.println("ID OF SQUARE="+id);
if(id > 0){//valid square is touched
Square square=Util.getSquareWithID(squares, id);
movingTile.setX(square.getX());
movingTile.setY(square.getY());
System.out.println("ID OF
TILe="+movingTile.getId());
}
movingTile=null;
invalidate();
break;
}
break;
}
return true; //done with this event so consume it
}
@Override
protected void onDraw(Canvas canvas) {
// Our move updates are calculated in ACTION_MOVE in the
opposite direction
// from how we want to move the scroll rect. Think of this as
dragging to
// the left being the same as sliding the scroll rect to the
right.
int newScrollRectX = scrollRectX - (int)scrollByX;
int newScrollRectY = scrollRectY - (int)scrollByY;
// Don't scroll off the left or right edges of the bitmap.
if (newScrollRectX < 0)
newScrollRectX = 0;
else if (newScrollRectX > (bmLargeImage.getWidth() -
displayWidth))
newScrollRectX = (bmLargeImage.getWidth() -
displayWidth);
// Don't scroll off the top or bottom edges of the bitmap.
if (newScrollRectY < 0)
newScrollRectY = 0;
else if (newScrollRectY > (bmLargeImage.getHeight() -
boardHeight))
newScrollRectY = (bmLargeImage.getHeight() -
boardHeight);
// We have our updated scroll rect coordinates, set them and
draw.
if(movingTile == null){
scrollRect.set(newScrollRectX, newScrollRectY,
newScrollRectX + displayWidth, newScrollRectY +
boardHeight);
}
Paint paint = new Paint();
canvas.drawBitmap(bmLargeImage, scrollRect, displayRect,
paint);
// Reset current scroll coordinates to reflect the latest
updates,
// so we can repeat this update process.
scrollRectX = newScrollRectX;
scrollRectY = newScrollRectY;
//drawing tiles here
for(int i=0;i<tiles.size();i++){
Tile tile=tiles.get(i);
// if( movingTile == null && tile.getX() > scrollRectX &&
tile.getY() > scrollRectY)
tile.onDraw(canvas,paint);
}
}
}
}
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en