Android Sliding Puzzzle Game: the slide effect -
i'm trying make android puzzle game out of picture. i've done 'till is: -i've cut picture in how many parts edittext field; -i've put cut pictures in gridview , got puzzle table; -i've used collection.suffle(list) suffle images in gridview;
now, trying implement sliding effect on 2 images gridview. want achieve sliding effect similar candycrush app or 1 http://www.youtube.com/watch?v=quop87fnb8w . mean, want hold image , drag top should slide top. if click picture , slide bottom, should change picture below , on...
here code:
reset = (button) findviewbyid(r.id.breset); splitno = (edittext) findviewbyid(r.id.edsplitby); splitby = integer.parseint(splitno.gettext().tostring()); imgno = splitby * splitby; puzzle = (gridview) findviewbyid(r.id.puzzle); rootimg = (imageview) findviewbyid(r.id.source_image); splitimage(rootimg); formpuzzle(smallimages); reset.setonclicklistener(new onclicklistener() { public void onclick(view v) { reset.settext("reset"); splitno.geteditabletext().tostring(); splitby = integer.parseint(splitno.gettext().tostring()); imgno = splitby * splitby; collections.shuffle(smallimages); formpuzzle(smallimages); } });
and here how form puzzle:
private void formpuzzle(arraylist<puzzleitem> smallimages) { smi = new smallimageadapter(this, smallimages); puzzle.setadapter(smi); puzzle.setnumcolumns(splitby); puzzle.sethorizontalspacing(0); puzzle.setverticalspacing(0); }
puzzleitem.class:
public class puzzleitem { bitmap puzzlepartimage; int correctposition; public puzzleitem(bitmap d, int index) { puzzlepartimage = d; correctposition = index; }
i used class remember position of pieces can check if puzzle finished...
so i'm interested in how implement sliding motion draging item gridview...
thanks :)
edit: should use ontouchlistener ?
ok, i've managed find solution myself :
private void formpuzzle(final arraylist<puzzleitem> smallimages) { smi = new smallimageadapter(this, smallimages); puzzle.setadapter(smi); puzzle.setnumcolumns(splitby); puzzle.sethorizontalspacing(0); puzzle.setverticalspacing(0); puzzle.setontouchlistener(new ontouchlistener() { @override public boolean ontouch(view v, motionevent event) { switch (event.getaction()) { case motionevent.action_down: { startx = event.getx(); starty = event.gety(); smallimage_position = puzzle.pointtoposition((int) startx, (int) starty); return true; } case motionevent.action_move: { endx = event.getx(); endy = event.gety(); log.d("x", "..." +endx); log.d("y", "..." +endy); if(endx > startx + smallimage_width/2 && (endy < starty - smallimage_height/2 || endy < starty + smallimage_height/2) ){ puzzleitem p1 = smi.smallimages.get(smallimage_position); puzzleitem p2 = smi.smallimages.get(smallimage_position +1); smi.smallimages.set(smallimage_position, p2); smi.smallimages.set(smallimage_position +1, p1); smi.notifydatasetchanged(); startx = endx*200; starty = endy*200; } return true; } } return true; } });
the slide given motionevent.action_move , calculating pixels can find out in direction finger moving ... example .. if action_move case recognising swipe right ...
i hope of guys ...
Comments
Post a Comment