Skip to content Skip to sidebar Skip to footer

Setting Image View Absolute Position

I'm trying to manually set the image-view position within my app, the code is currently working but all the facebook images display at the top right and side on top of each other.

Solution 1:


Solution 2:

i've found a trick to place an imageview at absolute position xy inside a relative layout. the following code place the CENTER of a imageview at x,y, even outside the relativelayout boundaries.

    /*
     * relativelayout allow out-of-boudaries only along the alignment side.
     * if aligned to left then you can have a negative x and overlap the
     * left side, if aligned to right you can overlap the right
     * 
     * let's put the image CENTERED on x,y
     * 
     * If we are on the left side of the container 
     *      |  (x<dx/2) |<---imgDx-->|
     *      |           |____________|
     *      |
     *      |      x 
     *      |<---------------->                  |
     *      |<----------> x-imgDx/2              |
     *      |____________________________________|
     * 
     * if we goes past the half of the container
     *                        <---imgDx-->       |
     *      |                |____________|      |
     *      |           x                        |
     *      |<--------------------->             |
     *      |                             <----->| dx-(x+imgDx/2) 
     *      |____________________________________|
     */
    public void setBitmapPosition(ImageView iv, int x, int y) {
        String log = "";
        RelativeLayout rl = (RelativeLayout) iv.getParent();

        // get bitmap size
        int imgDx = iv.getLayoutParams().width;
        int imgDy = iv.getLayoutParams().height;
        // get container size
        int dx = rl.getWidth();
        int dy = rl.getHeight();

        log = log + " XY:" + new Integer(x).toString() + ","
                + new Integer(y).toString();

        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
                imgDx, imgDy);
        iv.setLayoutParams(lp);

        log = log + " imgXY:" + new Integer(imgDx).toString() + ","
                + new Integer(imgDy).toString();

        log = log + " winXY:" + new Integer(dx).toString() + ","
                + new Integer(dy).toString();

        if (x <= (dx / 2)) {
            // i'm on the left side of the view so let's align to left
            lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT,
                    RelativeLayout.TRUE);
            lp.leftMargin = x - imgDx / 2;
            log = log + " LEFT:" + new Integer(lp.leftMargin).toString();
        } else {
            // align to right. we are past the middle
            lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,
                    RelativeLayout.TRUE);
            lp.rightMargin = dx - (x + imgDx / 2);
            log = log + " RIGHT:" + new Integer(lp.rightMargin).toString();
        }

        if (y <= (dy / 2)) {
            // align to top
            lp.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
            lp.topMargin = y - imgDy / 2;
            log = log + " TOP:" + new Integer(lp.topMargin).toString();

        } else {
            // align to bottom
            lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM,
                    RelativeLayout.TRUE);
            lp.bottomMargin = dy - (y + imgDy / 2);
            log = log + " BOTTOM:"
                    + new Integer(lp.bottomMargin).toString();
        }

        iv.setLayoutParams(lp);

        Log.i("PARAM", log);
    }

Post a Comment for "Setting Image View Absolute Position"