Example usage for android.graphics.drawable Drawable isVisible

List of usage examples for android.graphics.drawable Drawable isVisible

Introduction

In this page you can find the example usage for android.graphics.drawable Drawable isVisible.

Prototype

public final boolean isVisible() 

Source Link

Usage

From source file:Main.java

/**
 * Copies various properties from one drawable to the other.
 * @param to drawable to copy properties to
 * @param from drawable to copy properties from
 *//*from  w  w  w.  j  av  a2  s.c  o  m*/
public static void copyProperties(Drawable to, Drawable from) {
    if (from == null || to == null || to == from) {
        return;
    }

    to.setBounds(from.getBounds());
    to.setChangingConfigurations(from.getChangingConfigurations());
    to.setLevel(from.getLevel());
    to.setVisible(from.isVisible(), /* restart */false);
    to.setState(from.getState());
}

From source file:android.support.v7.testutils.TestUtils.java

/**
 * This method takes a view and returns a single bitmap that is the layered combination
 * of background drawables of this view and all its ancestors. It can be used to abstract
 * away the specific implementation of a view hierarchy that is not exposed via class APIs
 * or a view hierarchy that depends on the platform version. Instead of hard-coded lookups
 * of particular inner implementations of such a view hierarchy that can break during
 * refactoring or on newer platform versions, calling this API returns a "combined" background
 * of the view.//  w  w w  . j av a2s.  co m
 *
 * For example, it is useful to get the combined background of a popup / dropdown without
 * delving into the inner implementation details of how that popup is implemented on a
 * particular platform version.
 */
public static Bitmap getCombinedBackgroundBitmap(View view) {
    final int bitmapWidth = view.getWidth();
    final int bitmapHeight = view.getHeight();

    // Create a bitmap
    final Bitmap bitmap = Bitmap.createBitmap(bitmapWidth, bitmapHeight, Bitmap.Config.ARGB_8888);
    // Create a canvas that wraps the bitmap
    final Canvas canvas = new Canvas(bitmap);

    // As the draw pass starts at the top of view hierarchy, our first step is to traverse
    // the ancestor hierarchy of our view and collect a list of all ancestors with non-null
    // and visible backgrounds. At each step we're keeping track of the combined offsets
    // so that we can properly combine all of the visuals together in the next pass.
    List<View> ancestorsWithBackgrounds = new ArrayList<>();
    List<Pair<Integer, Integer>> ancestorOffsets = new ArrayList<>();
    int offsetX = 0;
    int offsetY = 0;
    while (true) {
        final Drawable backgroundDrawable = view.getBackground();
        if ((backgroundDrawable != null) && backgroundDrawable.isVisible()) {
            ancestorsWithBackgrounds.add(view);
            ancestorOffsets.add(Pair.create(offsetX, offsetY));
        }
        // Go to the parent
        ViewParent parent = view.getParent();
        if (!(parent instanceof View)) {
            // We're done traversing the ancestor chain
            break;
        }

        // Update the offsets based on the location of current view in its parent's bounds
        offsetX += view.getLeft();
        offsetY += view.getTop();

        view = (View) parent;
    }

    // Now we're going to iterate over the collected ancestors in reverse order (starting from
    // the topmost ancestor) and draw their backgrounds into our combined bitmap. At each step
    // we are respecting the offsets of our original view in the coordinate system of the
    // currently drawn ancestor.
    final int layerCount = ancestorsWithBackgrounds.size();
    for (int i = layerCount - 1; i >= 0; i--) {
        View ancestor = ancestorsWithBackgrounds.get(i);
        Pair<Integer, Integer> offsets = ancestorOffsets.get(i);

        canvas.translate(offsets.first, offsets.second);
        ancestor.getBackground().draw(canvas);
        canvas.translate(-offsets.first, -offsets.second);
    }

    return bitmap;
}