How to adjust relative layout when zooming an item

2019-07-24 03:47发布

问题:

I have created a screen in which have created RelativeLayout dynamically. After that I have added some ImageView under RelativeLayout.

The requirement is that when I click on any Image View then that ImageView should be zoom by 20% and other images show be adjusted automatically.

So I have two problems:

  • How to zoom a particular ImageView (other ImagesView should be zoom out if already zoomed in)
  • How to adjust other Image View when perform zoom in and zoom out

Please suggest, I have not worked on Animation before.

Here is code to create layout dynamically:

// Method for adding layout and images
private void addRow(List<HashMap<String, String>> list) {
    int breaker = 4, j, row = 0, tempSize = 0;

    // Getting number of rows to create
    row = (drinkList.size() % 5) == 0 ? drinkList.size() / 5 : (drinkList.size() / 5) + 1;

    // Looping for rows
    for (j = 0; j < row; j++) {

        // Specifying breaker value to break the images row
        if (j % 2 != 0) {
            breaker = 4;
        } else {
            breaker = 5;
        }

        // Creating layout for rows
        RelativeLayout layout = new RelativeLayout(Drinks.this);
        layout.setId(111 + j);

        // Setting layout parameters to row layout
        // layout.setLayoutParams(new
        // LayoutParams(LayoutParams.WRAP_CONTENT,
        // LayoutParams.WRAP_CONTENT, Gravity.CENTER));

        RelativeLayout.LayoutParams newParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);

        if (j > 0) {
            newParams.addRule(RelativeLayout.BELOW, (111 + j) - 1);
        }
        layout.setLayoutParams(newParams);

        int c = 0;

        tempSize = list.size();

        // Looping for placing Images
        while (tempSize != 0 && c < breaker) {

            // getting hash map from list
            HashMap<String, String> h = new HashMap<String, String>();
            h = list.get(0);

            // Creating Image
            final ImageView imageView = new ImageView(Drinks.this);
            // Layout Parameters for Image
            LayoutParams lpImage = new LayoutParams(150, 150);
            imageView.setId(111 + c);

            if (c > 0) {
                lpImage.addRule(RelativeLayout.RIGHT_OF, (111 + c) - 1);
            }

            imageView.setTag(c);
            lpImage.setMargins(10, 10, 10, 10);
            imageView.setLayoutParams(lpImage);

            URI uri = null;
            URL imageUrl;
            String brandID, categoryID, flavourID;

            try {
                imageUrl = new URL(h.get(Utility.KEY_ICON).toString().replace(" ", "%20"));
                brandID = h.get(Utility.KEY_BRAND_ID);
                categoryID = h.get(Utility.KEY_CATEGORY_ID);
                flavourID = h.get(Utility.KEY_FLAVOUR_ID);

                // HashMap<String, String> hash = new HashMap<String,
                // String>();
                // hash.put(Utility.KEY_BRAND_ID, brandID);
                // hash.put(Utility.KEY_CATEGORY_ID, categoryID);

                // Setting tag
                imageView.setTag(brandID);

                uri = new URI(imageUrl.toString());
            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (URISyntaxException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            // Loading image from URLs
            ImageLoader imageLoader = new ImageLoader(Drinks.this);
            imageLoader.DisplayImage(uri.toString(), R.drawable.ic_launcher, imageView);

            /**
             * Click Listener of Image
             * **/
            imageView.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub

                    Toast.makeText(Drinks.this, "TAG " + imageView.getTag(), 2000).show();

                    if (!pressed) {
                        imageView.startAnimation(zoomIn);
                        pressed = !pressed;
                    } else {
                        imageView.startAnimation(zoomOut);
                        pressed = !pressed;
                    }

                }
            });
            layout.addView(imageView);

            // removing items from list
            list.remove(0);

            c++;
            tempSize--;

        }

        containerLayout.addView(layout);

    }