在Android的图像替换问题的GridView图像、问题、Android、GridView

2023-09-06 15:27:10 作者:無名指的痛

我提供我的code已这儿过得breaked图像分成9个部分,并在网格视图如何通过点击即改变图像显示,(第一次点击图像应更换与第二点击图像,反之亦然)我已经使用位图数组分割图像和把他们安置在网格视图。所以如何通过点击两个图像图像的交换应该做怎样任何一个可以帮助我改变网格视图图像。

 公共类Imagepieces延伸活动{                ArrayList的<&位图GT; breakedimages,重复;                GridView控件克;                INT I = 0,温度,TEMP2,RPOS;                @覆盖                保护无效的onCreate(捆绑savedInstanceState){                    // TODO自动生成方法存根                    super.onCreate(savedInstanceState);                    的setContentView(R.layout.activity_image);                    breakedimages = getIntent()getParcelableArrayListExtra(breaked图像)。                    复制=新的ArrayList<&位图GT;(breakedimages);                    Collections.shuffle(一式两份);                    G =(GridView控件)findViewById(R.id.gridView1);                    g.setAdapter(新CutAdapter(这一点,breakedimages));                    g.setNumColumns((int)的的Math.sqrt(breakedimages.size()));                    g.setOnItemClickListener(新OnItemClickListener(){                        @覆盖                        公共无效onItemClick(适配器视图<>为arg0,ARG1观,诠释ARG2,                                长ARG3){                            // TODO自动生成方法存根                            // ================================================ =                            {                        }                    });                }                类CutAdapter扩展了BaseAdapter {                    INT iwidth,IHEIGHT;                    上下文语境;                    公共CutAdapter(Imagepieces ipieces,ArrayList的<&位图GT; breakedimages){                        // TODO自动生成构造函数存根                        iwidth = breakedimages.get(0).getWidth();                        IHEIGHT = breakedimages.get(0).getHeight();                        上下文= ipieces;                    }                    @覆盖                    公众诠释的getCount(){                        // TODO自动生成方法存根                        返回duplicate.size();                    }                    @覆盖                    公共对象的getItem(INT为arg0){                        // TODO自动生成方法存根                        返回duplicate.get(为arg0);                    }                    @覆盖                    众长getItemId(INT为arg0){                        // TODO自动生成方法存根                        返回将arg0;                    }                    @覆盖                    公共查看getView(INT为arg0,ARG1查看,ViewGroup中ARG2){                        // TODO自动生成方法存根                        ImageView的I =新ImageView的(上下文);                        i.setLayoutParams(新GridView.LayoutParams(iwidth + 5,IHEIGHT +5));                        i.setPadding(0,0,0,0);                        i.setImageBitmap(duplicate.get(arg0中));                        返回我;                    }                }            }任何一个可以为我做的必要性,因为我已经在这里坚持了如何将图片本身之间移动 

解决方案

我们可以实现的另一种方法。

gridview CSDN

onItemClickListener()

 我++;     位图B = NULL;     如果(ⅰ%2!= 0){       TEMP = ARG2;       B = duplicate.get(临时);     }     如果(ⅰ%2 == 0){        TEMP2 = ARG2;        duplicate.set(温度,duplicate.get(ARG2));        duplicate.set(TEMP2,B);     } 

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 

解决方案

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