Search code examples
androidimageviewbringtofront

Bring multiple images front in order


I have a problem with bringtofront method. I use many images at once. All moves ceaselessly. I want to change z index of images depends on their coordinates. Is there any way for that?

I may check coordinates of every images when i move an image. And use bringtofront in order... but the solution doesn't seem clear to me.

Edit:

I added some code. The calculations are not that important. rl is relative layout. When I click on rl, creates an image. The problem is when I create an image, It comes over olds. I want, which image has bigger y index value. Will be front of others.

An image :http://i.hizliresim.com/blkyzj.jpg

Step 1: I created green square. Step 2: I created brown square. ->Brown is at the behind of green because green has bigger y value. I may check y coordinates every time that I create an image, but images also move. It is problem to check y values and do bring to front every time they move. Besides there are 30 40 images... Is there any way to do sth like image.zindex = ycoordinate*-1;

    rl.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View arg0, MotionEvent arg1) {
                    if (arg1.getAction() == MotionEvent.ACTION_DOWN) {
                        final int finalX = (int) (Math.floor(arg1.getX() - (width * 5.93667 / 100))/(width*12.53290/100));
                        final int finalY = (int) (Math.floor(arg1.getY() - (height * 11.04784 / 100))/(height*7.73524/100));
                        characters.add(new Character());
                        characters.get(characters.size()-1).image = new ImageView(InPlay.this);
                        characters.get(characters.size()-1).image.setScaleType(ImageView.ScaleType.FIT_XY);
                        characters.get(characters.size()-1).image.setBackgroundResource(getId(towerid[activet], R.drawable.class));
                        characters.get(characters.size()-1).anim = (AnimationDrawable)     characters.get(characters.size()-1).image.getBackground();
                        rl.addView(characters.get(characters.size()-1).image, characters.get(characters.size()-1).lp);
                        characters.get(characters.size()-1).image.getLayoutParams().width = (int) (width*12.53290/100) + (int)(characters.get(characters.size()-1).size * (width/10));
                        characters.get(characters.size()-1).image.getLayoutParams().height = (int) (height*7.73524/100) +  (int)(characters.get(characters.size()-1).size * (height/10));
                        characters.get(characters.size() - 1).x= (float) ((finalX * (width*12.53290/100))+ (width * 5.93667 / 100)) - (float)(characters.get(characters.size()-1).size * (width/20));
                        characters.get(characters.size() - 1).y= (float) ((finalY * (height*7.73524/100))+ (height * 11.04784 / 100)) - (float)(characters.get(characters.size()-1).size * (height/10));
                        characters.get(characters.size()-1).image.setX((float) ((finalX * (width*12.53290/100))+ (width * 5.93667 / 100)) -(float)(characters.get(characters.size()-1).size * (width/20)));
                        characters.get(characters.size()-1).image.setY((float) ((finalY * (height*7.73524/100))+ (height * 11.04784 / 100)) - (float)(characters.get(characters.size()-1).size * (height/10)));

        }

Solution

  • Check out LayerDrawable, which manages many Drawables and you can set which layer is on top based on the array order. This may require a little bit of array manipulation, but I think it's a relatively light way to keep track of the layers. you could easily check the location of the drawable on screen and change the array order to suit your needs.