1

我在这里提供我的代码。我已将图像分成 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 
4

2 回答 2

1

首先按照@Terril Thomas's 选择两个图像进行交换,

这是在列表中交换两个图像的方法,

  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();
于 2012-10-01T06:56:57.270 回答
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);
     }
于 2012-10-01T08:01:15.443 回答