move View from on spot to another spot - Android User Interface

Android examples for User Interface:View Slide

Description

move View from on spot to another spot

Demo Code


//package com.java2s;

import android.view.View;
import android.widget.RelativeLayout.LayoutParams;

public class Main {
    public static void move(View v, int upDown, int leftRight) {
        LayoutParams params = getMoveParams(v, upDown, leftRight);
        v.setLayoutParams(params);//  w  w w.  ja v  a  2  s . c om
    }

    public static LayoutParams getMoveParams(View v, int upDown,
            int leftRight) {
        LayoutParams original = (LayoutParams) v.getLayoutParams();
        //RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(original);
        LayoutParams params = cloneParams(original);
        params.leftMargin += leftRight;
        params.rightMargin -= leftRight;
        params.topMargin -= upDown;
        params.bottomMargin += upDown;
        return params;
    }

    public static LayoutParams cloneParams(LayoutParams params) {
        LayoutParams copy = new LayoutParams(params.width, params.height);
        copy.leftMargin = params.leftMargin;
        copy.topMargin = params.topMargin;
        copy.rightMargin = params.rightMargin;
        copy.bottomMargin = params.bottomMargin;
        int[] rules = params.getRules();
        for (int i = 0; i < rules.length; i++) {
            copy.addRule(i, rules[i]);
        }
        copy.setMarginStart(params.getMarginStart());
        copy.setMarginEnd(params.getMarginEnd());

        return copy;
    }
}

Related Tutorials