Skip to content

Apply visual Transformations to child views of ViewGroup

rutura edited this page Apr 16, 2017 · 1 revision
  • Quick Code :
private void init() {
    // Enable static transformations so each child will
    // have getChildStaticTransformation() called.
        setStaticTransformationsEnabled(true);
    }
    @Override
    protected boolean getChildStaticTransformation(View child,
                                                   Transformation t) {
        // Clear any existing transformation
        t.clear();
        if (getOrientation() == HORIZONTAL) {
        // Scale children based on distance from left edge
            float delta = 1.0f - ((float) child.getLeft() / getWidth());
            t.getMatrix().setScale(delta, delta, child.getWidth() / 2,
                    child.getHeight() / 2);
        } else {
            // Scale children based on distance from top edge
            float delta = 1.0f - ((float) child.getTop() / getHeight());
            t.getMatrix().setScale(delta, delta, child.getWidth() / 2,
                    child.getHeight() / 2);
            //Also apply a fade effect based on its location
            t.setAlpha(delta);
        }
        return true;
    }
  • It is possible to add transformations such as rotation, scale and alpha to ViewGroups without resorting to animations.
  • This is also very convenient in applying transformations from the context of a parent view such as scale or color that changes with position
  • The first step in enabling these transformations is calling
  setStaticTranformationsEnabled(true)

during the initialization of the ViewGroup class.(Usually in the constructor)

  • Then implement the
          getChildStaticTransformation(View child,Transformation t)

override method and in there apply your transformations.

Clone this wiki locally