0

我正在拆分图像并放置在网格视图中。在网格视图中单击两个图像时,我想交换它们的位置。我可以改变他们的位置,但形象没有改变。我使用位图数组来分割图像并将它们放置在网格视图中。那么如何通过单击两个图像来更改网格视图中的图像?我应该如何进行图像交换?谁能帮助我?

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;
                }

            }
        }

如何在网格视图中交换图像?当我在第一次单击时单击网格视图时,我必须获得位置,然后单击第二个位置。然后我在这两个位置的图像,并在更改所有图像之后,如果两者相等,我想与已分割的图像进行比较。然后我想在游戏结束时显示祝酒词。谁能帮我

4

1 回答 1

0

GridView,当你想改变一个项目时,你所需要的只是改变Adapter你链接到的GridView

例如,交换两个项目:(GridAdapter extends ArrayAdapter)

GridAdapter adapter = (GridAdapter)gridv.getAdapter();
String firstItem = adapter.getItem(firstPosition);
String secondItem = adapter.getItem(secondPosition);
adapter.remove(firstItem);
adapter.insert(firstItem, secondPosition);
adapter.remove(secondItem);
adapter.insert(secondItem, firstPosition);

Adapter您还可以通过覆盖来更改自定义中的其他内容getView()

于 2012-10-01T05:14:29.033 回答