Example usage for android.view View getMatrix

List of usage examples for android.view View getMatrix

Introduction

In this page you can find the example usage for android.view View getMatrix.

Prototype

public Matrix getMatrix() 

Source Link

Document

The transform matrix of this view, which is calculated based on the current rotation, scale, and pivot properties.

Usage

From source file:Main.java

/**
 * Inverse of {@link #getDescendantCoordRelativeToSelf(View, int[])}.
 *//*from w ww. j ava 2  s .c o m*/
public static float mapCoordInSelfToDescendent(View descendant, View root, int[] coord) {
    ArrayList<View> ancestorChain = new ArrayList<View>();

    float[] pt = { coord[0], coord[1] };

    View v = descendant;
    while (v != root) {
        ancestorChain.add(v);
        v = (View) v.getParent();
    }
    ancestorChain.add(root);

    float scale = 1.0f;
    Matrix inverse = new Matrix();
    int count = ancestorChain.size();
    for (int i = count - 1; i >= 0; i--) {
        View ancestor = ancestorChain.get(i);
        View next = i > 0 ? ancestorChain.get(i - 1) : null;

        pt[0] += ancestor.getScrollX();
        pt[1] += ancestor.getScrollY();

        if (next != null) {
            pt[0] -= next.getLeft();
            pt[1] -= next.getTop();
            next.getMatrix().invert(inverse);
            inverse.mapPoints(pt);
            scale *= next.getScaleX();
        }
    }

    coord[0] = (int) Math.round(pt[0]);
    coord[1] = (int) Math.round(pt[1]);
    return scale;
}

From source file:Main.java

public static void invalidateGlobalRegion(View view, RectF childBounds) {
    //childBounds.offset(view.getTranslationX(), view.getTranslationY());
    if (DEBUG_INVALIDATE)
        Log.v(TAG, "-------------");
    while (view.getParent() != null && view.getParent() instanceof View) {
        view = (View) view.getParent();
        view.getMatrix().mapRect(childBounds);
        view.invalidate((int) Math.floor(childBounds.left), (int) Math.floor(childBounds.top),
                (int) Math.ceil(childBounds.right), (int) Math.ceil(childBounds.bottom));
        if (DEBUG_INVALIDATE) {
            Log.v(TAG,/*w w w. j  av  a  2  s . co  m*/
                    "INVALIDATE(" + (int) Math.floor(childBounds.left) + "," + (int) Math.floor(childBounds.top)
                            + "," + (int) Math.ceil(childBounds.right) + ","
                            + (int) Math.ceil(childBounds.bottom));
        }
    }
}

From source file:Main.java

/**
 * Maps a coorindate in a descendant view into the parent.
 *///from ww w .  ja  v a2s.com
public static float mapCoordInDescendentToSelf(View descendant, View root, float[] coord,
        boolean includeRootScroll) {
    ArrayList<View> ancestorChain = new ArrayList<View>();

    float[] pt = { coord[0], coord[1] };

    View v = descendant;
    while (v != root && v != null) {
        ancestorChain.add(v);
        v = (View) v.getParent();
    }
    ancestorChain.add(root);

    float scale = 1.0f;
    int count = ancestorChain.size();
    for (int i = 0; i < count; i++) {
        View v0 = ancestorChain.get(i);
        // For TextViews, scroll has a meaning which relates to the text position
        // which is very strange... ignore the scroll.
        if (v0 != descendant || includeRootScroll) {
            pt[0] -= v0.getScrollX();
            pt[1] -= v0.getScrollY();
        }

        v0.getMatrix().mapPoints(pt);
        pt[0] += v0.getLeft();
        pt[1] += v0.getTop();
        scale *= v0.getScaleX();
    }

    coord[0] = pt[0];
    coord[1] = pt[1];
    return scale;
}

From source file:Main.java

/**
 * Maps a coordinate in a descendant view into the parent.
 *///from   w w  w.  j  a va  2  s  .c o m
public static float mapCoordInDescendentToSelf(View descendant, View root, float[] coord,
        boolean includeRootScroll) {
    ArrayList<View> ancestorChain = new ArrayList<>();

    float[] pt = { coord[0], coord[1] };

    View v = descendant;
    while (v != root && v != null) {
        ancestorChain.add(v);
        v = (View) v.getParent();
    }
    ancestorChain.add(root);

    float scale = 1.0f;
    int count = ancestorChain.size();
    for (int i = 0; i < count; i++) {
        View v0 = ancestorChain.get(i);
        // For TextViews, scroll has a meaning which relates to the text position
        // which is very strange... ignore the scroll.
        if (v0 != descendant || includeRootScroll) {
            pt[0] -= v0.getScrollX();
            pt[1] -= v0.getScrollY();
        }

        v0.getMatrix().mapPoints(pt);
        pt[0] += v0.getLeft();
        pt[1] += v0.getTop();
        scale *= v0.getScaleX();
    }

    coord[0] = pt[0];
    coord[1] = pt[1];
    return scale;
}

From source file:Main.java

static void offsetDescendantMatrix(ViewParent target, View view, Matrix m) {
    final ViewParent parent = view.getParent();
    if (parent instanceof View && parent != target) {
        final View vp = (View) parent;
        offsetDescendantMatrix(target, vp, m);
        m.preTranslate(-vp.getScrollX(), -vp.getScrollY());
    }//from   w ww.ja  v  a  2s  .  c  om

    m.preTranslate(view.getLeft(), view.getTop());

    if (!view.getMatrix().isIdentity()) {
        m.preConcat(view.getMatrix());
    }
}

From source file:Main.java

/**
 * Given a coordinate relative to the descendant, find the coordinate in a parent view's
 * coordinates./*  w  w w.  j  av  a  2 s .  c  o  m*/
 *
 * @param descendant The descendant to which the passed coordinate is relative.
 * @param root The root view to make the coordinates relative to.
 * @param coord The coordinate that we want mapped.
 * @param includeRootScroll Whether or not to account for the scroll of the descendant:
 *          sometimes this is relevant as in a child's coordinates within the descendant.
 * @return The factor by which this descendant is scaled relative to this DragLayer. Caution
 *         this scale factor is assumed to be equal in X and Y, and so if at any point this
 *         assumption fails, we will need to return a pair of scale factors.
 */
public static float getDescendantCoordRelativeToParent(View descendant, View root, int[] coord,
        boolean includeRootScroll) {
    ArrayList<View> ancestorChain = new ArrayList<View>();

    float[] pt = { coord[0], coord[1] };

    View v = descendant;
    while (v != root && v != null) {
        ancestorChain.add(v);
        v = (View) v.getParent();
    }
    ancestorChain.add(root);

    float scale = 1.0f;
    int count = ancestorChain.size();
    for (int i = 0; i < count; i++) {
        View v0 = ancestorChain.get(i);
        // For TextViews, scroll has a meaning which relates to the text position
        // which is very strange... ignore the scroll.
        if (v0 != descendant || includeRootScroll) {
            pt[0] -= v0.getScrollX();
            pt[1] -= v0.getScrollY();
        }

        v0.getMatrix().mapPoints(pt);
        pt[0] += v0.getLeft();
        pt[1] += v0.getTop();
        scale *= v0.getScaleX();
    }

    coord[0] = (int) Math.round(pt[0]);
    coord[1] = (int) Math.round(pt[1]);
    return scale;
}

From source file:Main.java

/**
 * Maps a coordinate in the root to a descendent.
 *//*from   ww w  .  j a  va  2s.co m*/
public static float mapCoordInSelfToDescendent(View descendant, View root, float[] coord,
        Matrix tmpInverseMatrix) {
    ArrayList<View> ancestorChain = new ArrayList<View>();

    float[] pt = { coord[0], coord[1] };

    View v = descendant;
    while (v != root) {
        ancestorChain.add(v);
        v = (View) v.getParent();
    }
    ancestorChain.add(root);

    float scale = 1.0f;
    int count = ancestorChain.size();
    tmpInverseMatrix.set(IDENTITY_MATRIX);
    for (int i = count - 1; i >= 0; i--) {
        View ancestor = ancestorChain.get(i);
        View next = i > 0 ? ancestorChain.get(i - 1) : null;

        pt[0] += ancestor.getScrollX();
        pt[1] += ancestor.getScrollY();

        if (next != null) {
            pt[0] -= next.getLeft();
            pt[1] -= next.getTop();
            next.getMatrix().invert(tmpInverseMatrix);
            tmpInverseMatrix.mapPoints(pt);
            scale *= next.getScaleX();
        }
    }

    coord[0] = pt[0];
    coord[1] = pt[1];
    return scale;
}

From source file:Main.java

/**
 * Maps a coordinate in the root to a descendent.
 *///  w w  w .  ja  va2 s  . co  m
public static float mapCoordInSelfToDescendent(View descendant, View root, float[] coord,
        Matrix tmpInverseMatrix) {
    ArrayList<View> ancestorChain = new ArrayList<>();

    float[] pt = { coord[0], coord[1] };

    View v = descendant;
    while (v != root) {
        ancestorChain.add(v);
        v = (View) v.getParent();
    }
    ancestorChain.add(root);

    float scale = 1.0f;
    int count = ancestorChain.size();
    tmpInverseMatrix.set(IDENTITY_MATRIX);
    for (int i = count - 1; i >= 0; i--) {
        View ancestor = ancestorChain.get(i);
        View next = i > 0 ? ancestorChain.get(i - 1) : null;

        pt[0] += ancestor.getScrollX();
        pt[1] += ancestor.getScrollY();

        if (next != null) {
            pt[0] -= next.getLeft();
            pt[1] -= next.getTop();
            next.getMatrix().invert(tmpInverseMatrix);
            tmpInverseMatrix.mapPoints(pt);
            scale *= next.getScaleX();
        }
    }

    coord[0] = pt[0];
    coord[1] = pt[1];
    return scale;
}

From source file:android.support.transition.ChangeTransform.java

private void captureValues(TransitionValues transitionValues) {
    View view = transitionValues.view;
    if (view.getVisibility() == View.GONE) {
        return;/*w w  w.  jav  a  2 s  .  c  o m*/
    }
    transitionValues.values.put(PROPNAME_PARENT, view.getParent());
    Transforms transforms = new Transforms(view);
    transitionValues.values.put(PROPNAME_TRANSFORMS, transforms);
    Matrix matrix = view.getMatrix();
    if (matrix == null || matrix.isIdentity()) {
        matrix = null;
    } else {
        matrix = new Matrix(matrix);
    }
    transitionValues.values.put(PROPNAME_MATRIX, matrix);
    if (mReparent) {
        Matrix parentMatrix = new Matrix();
        ViewGroup parent = (ViewGroup) view.getParent();
        ViewUtils.transformMatrixToGlobal(parent, parentMatrix);
        parentMatrix.preTranslate(-parent.getScrollX(), -parent.getScrollY());
        transitionValues.values.put(PROPNAME_PARENT_MATRIX, parentMatrix);
        transitionValues.values.put(PROPNAME_INTERMEDIATE_MATRIX, view.getTag(R.id.transition_transform));
        transitionValues.values.put(PROPNAME_INTERMEDIATE_PARENT_MATRIX, view.getTag(R.id.parent_matrix));
    }
}

From source file:com.android.launcher3.Utilities.java

/**
 * Given a coordinate relative to the descendant, find the coordinate in a parent view's
 * coordinates.//from   w  w w.jav  a2s.  co m
 *
 * @param descendant The descendant to which the passed coordinate is relative.
 * @param ancestor The root view to make the coordinates relative to.
 * @param coord The coordinate that we want mapped.
 * @param includeRootScroll Whether or not to account for the scroll of the descendant:
 *          sometimes this is relevant as in a child's coordinates within the descendant.
 * @return The factor by which this descendant is scaled relative to this DragLayer. Caution
 *         this scale factor is assumed to be equal in X and Y, and so if at any point this
 *         assumption fails, we will need to return a pair of scale factors.
 */
public static float getDescendantCoordRelativeToAncestor(View descendant, View ancestor, int[] coord,
        boolean includeRootScroll) {
    float[] pt = { coord[0], coord[1] };
    float scale = 1.0f;
    View v = descendant;
    while (v != ancestor && v != null) {
        // For TextViews, scroll has a meaning which relates to the text position
        // which is very strange... ignore the scroll.
        if (v != descendant || includeRootScroll) {
            pt[0] -= v.getScrollX();
            pt[1] -= v.getScrollY();
        }

        v.getMatrix().mapPoints(pt);
        pt[0] += v.getLeft();
        pt[1] += v.getTop();
        scale *= v.getScaleX();

        v = (View) v.getParent();
    }

    coord[0] = Math.round(pt[0]);
    coord[1] = Math.round(pt[1]);
    return scale;
}