Reputation: 35
I am providing my code here.i have breaked an image into 9 parts and shown in grid view how to change images by clicking on them i.e.,(first clicked image should be replaced with second clicked image and vice versa)i have used bitmap array for splitting images and placed them in grid view. so how to change images in grid view by clicking two images the swapping of images should be done how can any one help me.
public class Imagepieces extends Activity {
ArrayList<Bitmap> breakedimages,duplicate;
GridView g;
int i=0,temp,temp2,rpos;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image);
breakedimages = getIntent().getParcelableArrayListExtra("breaked image");
duplicate = new ArrayList<Bitmap>(breakedimages);
Collections.shuffle(duplicate);
g = (GridView) findViewById(R.id.gridView1);
g.setAdapter(new CutAdapter(this, breakedimages));
g.setNumColumns((int) Math.sqrt(breakedimages.size()));
g.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
//=================================================
{
}
});
}
class CutAdapter extends BaseAdapter {
int iwidth, iheight;
Context context;
public CutAdapter(Imagepieces ipieces, ArrayList<Bitmap> breakedimages) {
// TODO Auto-generated constructor stub
iwidth = breakedimages.get(0).getWidth();
iheight = breakedimages.get(0).getHeight();
context = ipieces;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return duplicate.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return duplicate.get(arg0);
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
// TODO Auto-generated method stub
ImageView i=new ImageView(context);
i.setLayoutParams(new GridView.LayoutParams(iwidth +5,iheight +5));
i.setPadding(0, 0, 0, 0);
i.setImageBitmap(duplicate.get(arg0));
return i;
}
}
}
can any one do the need for me as i have stuck here how to move the images among themselves
Upvotes: 1
Views: 521
Reputation: 1204
Another way we can implement.
In onItemClickListener()
i++;
Bitmap b = null;
if (i % 2 != 0) {
temp = arg2;
b = duplicate.get(temp);
}
if (i % 2 == 0) {
temp2 = arg2;
duplicate.set(temp, duplicate.get(arg2));
duplicate.set(temp2, b);
}
Upvotes: 0
Reputation: 12685
first follow the @Terril Thomas's to select two image for swapping,
Here is the method for swap two Image in List,
public void swapImage(Bitmap i1,Bitmap i2){
int position1,position2;
position1 = duplicate.indexOf(i1);
duplicate.remove(position1);
position2 = duplicate.indexOf(i1);
duplicate.remove(position2);
if(position2>position1){
duplicate.add(position2, i1);
duplicate.add(position1, i2);
}
if(position2<position1){
duplicate.add(position1, i2);
duplicate.add(position2, i1);
}
}
change in onCreate like this
CutAdapter ca = new CutAdapter(this, breakedimages);
g.setAdapter(ca);
than refresh your adapter
ca.notifyDataSetChanged();
Upvotes: 1