两个如何避免在动态创建中在Image中随机位置重叠ImageViews

时间:2014-08-02 08:32:38

标签: android

我是Android的新手,我正在尝试使用随机函数在我的活动中随机创建动态的50个图像。问题在于创建这些图像,它们似乎有时会重叠,任何人都可以指导我如何避免动态创建中的重叠,因为我的要求我不想通过XML创建这种图像视图。

for (int i = 0; i<30/*R.nextInt(10-4)+4*/; i ++) 
        {
            Random r = new Random();
            posX=r.nextInt((displaywidth)-(displaywidth-500))+(displaywidth-500);
            posY=r.nextInt((displayheight)-(displayheight-500))+(displayheight-500);
            if(pPosX!=posX&&pPosY!=posY)
            {
                if(posX<=displaywidth&&posY<=displayheight)
                {
                    System.out.println("XX "+posX+" X "+pPosX+" W "+displaywidth);
                    System.out.println("YY "+posY+" Y "+pPosY+" H "+displayheight);
                    image[i] = new ImageView(this);
                    PicID=r.nextInt(pic.length)-1;
                    if(PicID==-1)
                    {
                        PicID++;
                    }
                    image[i].setImageResource(pic[PicID]);
                    image[i].setTag(PicID);
                    int tag = (Integer) image[i].getTag();
                    System.out.println("PicTagTask"+tag);
                    image[i].setLayoutParams(new GridView.LayoutParams(50,50));
                    image[i].setScaleType(ImageView.ScaleType.CENTER);
                    image[i].setPadding(1,1,1,1);   
                    image[i].setX(posX);
                    image[i].setY(posY);
                    image[i].setAdjustViewBounds(true);
                    image[i].startAnimation(animation[R.nextInt(4)]/*animScale*/);
                    image[i].setOnTouchListener(this);
                    System.out.println("ImageViewID ="+image[i].getId());
                    mainlayout.addView(image[i]);

                    pPosX=posX;
                    pPosY=posY;
                    passTagImage[i]=PicID;

                }`enter code here`

0 个答案:

没有答案