java - onTouch movement for ImageView? -


i relatively new android development.

but wondering whether possible move these images, can swap each other in grid. board game trying implement.

this looks like: http://i.imgur.com/2shaugd.png

the code array of images:

    private integer[] mthumbids = {         r.drawable.blank, r.drawable.blank, r.drawable.peg, r.drawable.peg, r.drawable.peg, r.drawable.blank, r.drawable.blank,          r.drawable.blank, r.drawable.blank, r.drawable.peg, r.drawable.peg, r.drawable.peg, r.drawable.blank, r.drawable.blank,          r.drawable.peg, r.drawable.peg, r.drawable.peg, r.drawable.peg, r.drawable.peg, r.drawable.peg, r.drawable.peg,          r.drawable.peg, r.drawable.peg, r.drawable.peg, r.drawable.empty, r.drawable.peg, r.drawable.peg, r.drawable.peg,         r.drawable.peg, r.drawable.peg, r.drawable.peg, r.drawable.peg, r.drawable.peg, r.drawable.peg, r.drawable.peg,          r.drawable.blank, r.drawable.blank, r.drawable.peg, r.drawable.peg, r.drawable.peg, r.drawable.blank, r.drawable.blank,          r.drawable.blank, r.drawable.blank, r.drawable.peg, r.drawable.peg, r.drawable.peg, r.drawable.blank, r.drawable.blank,         }; 

xml:

<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="fill_parent"     android:layout_height="fill_parent"     android:orientation="vertical" >  <gridview       android:id="@+id/gridview"     android:layout_width="fill_parent"      android:layout_height="fill_parent"     android:layout_margintop="40dp"     android:columnwidth="50dp"     android:numcolumns="7"     android:verticalspacing="2dp"     android:horizontalspacing="2dp"     android:stretchmode="columnwidth"     android:gravity="center" />  </linearlayout>  

if don't need drag , drop should simple enough.

private view firstclicked = null;  private onclicklistener swapimages = new onclicklistener() {      @override     public void onclick(view v) {         if(firstclicked != null){             layoutparams p1 = firstclicked.getlayoutparams();             layoutparams p2 = v.getlayoutparams();             v.setlayoutparams(p1);             firstclicked.setlayoutparams(p2);                             firstclicked = null;         }         else              firstclicked = v;     } }; 

this way swap positions, not images inside them.

edit: if interfere game logic, swap instead of layout parameters, swap images.

edit: code swaps drawables, need create own logic

package com.example.stackoverflow; import android.os.bundle; import android.view.view; import android.widget.adapterview; import android.widget.adapterview.onitemclicklistener; import android.widget.gridview; import android.widget.imageview; import android.widget.toast; import android.app.activity; import android.graphics.drawable.drawable;   public class game extends activity {  private imageview firstclicked = null;      @override     protected void oncreate(bundle savedinstancestate) {             super.oncreate(savedinstancestate);             setcontentview(r.layout.game);              gridview gridview = (gridview) findviewbyid(r.id.gridview);         gridview.setadapter(new imageadapter(this));          gridview.setonitemclicklistener(new onitemclicklistener() {             public void onitemclick(adapterview<?> parent, view v, int position, long id) {                 toast.maketext(game.this, "" + position, toast.length_short).show();                 if(firstclicked != null){                     imageview v2 = (imageview)v;                     drawable d2 = v2.getdrawable();                     drawable d1 = firstclicked.getdrawable();                     v2.setimagedrawable(d1);                     firstclicked.setimagedrawable(d2);                     firstclicked = null;                 }                 else {                     firstclicked = (imageview)v;                 }             }          });     } } 

hope helps , enjoy work.


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -