我在这里提供我的代码。我已将图像分成9个部分,并在网格视图中显示如何通过点击它们来更改图像,即(首先点击的图像应该被第二个单击的图像替换,反之亦然)我有使用位图数组来分割图像并将它们放在网格视图中。那么如何通过点击两个图像来更改网格视图中的图像应该完成图像的交换,任何人都可以帮助我。
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
答案 0 :(得分:1)
首先按照@Terril Thomas的选择两个图像进行交换,
以下是在列表中交换两个图像的方法,
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);
}
}
像这样改变onCreate
CutAdapter ca = new CutAdapter(this, breakedimages);
g.setAdapter(ca);
而不是刷新适配器
ca.notifyDataSetChanged();
答案 1 :(得分:0)
我们可以实施的另一种方式。
在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);
}