Java Code Examples for android.graphics.drawable.LayerDrawable#getNumberOfLayers()

The following examples show how to use android.graphics.drawable.LayerDrawable#getNumberOfLayers() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RoundedImageView.java    From UltimateAndroid with Apache License 2.0 6 votes vote down vote up
private void updateAttrs(Drawable drawable) {
  if (drawable == null) { return; }

  if (drawable instanceof RoundedDrawable) {
    ((RoundedDrawable) drawable)
        .setScaleType(mScaleType)
        .setCornerRadius(cornerRadius)
        .setBorderWidth(borderWidth)
        .setBorderColor(borderColor)
        .setOval(isOval);
  } else if (drawable instanceof LayerDrawable) {
    // loop through layers to and set drawable attrs
    LayerDrawable ld = ((LayerDrawable) drawable);
    for (int i = 0, layers = ld.getNumberOfLayers(); i < layers; i++) {
      updateAttrs(ld.getDrawable(i));
    }
  }
}
 
Example 2
Source File: RoundedDrawable.java    From ClipCircleHeadLikeQQ with Apache License 2.0 6 votes vote down vote up
public static Drawable fromDrawable(Drawable drawable) {
  if (drawable != null) {
    if (drawable instanceof RoundedDrawable) {
      // just return if it's already a RoundedDrawable
      return drawable;
    } else if (drawable instanceof LayerDrawable) {
      LayerDrawable ld = (LayerDrawable) drawable;
      int num = ld.getNumberOfLayers();

      // loop through layers to and change to RoundedDrawables if possible
      for (int i = 0; i < num; i++) {
        Drawable d = ld.getDrawable(i);
        ld.setDrawableByLayerId(ld.getId(i), fromDrawable(d));
      }
      return ld;
    }

    // try to get a bitmap from the drawable and
    Bitmap bm = drawableToBitmap(drawable);
    if (bm != null) {
      return new RoundedDrawable(bm);
    }
  }
  return drawable;
}
 
Example 3
Source File: RoundedImageView.java    From AndroidMaterialDesign with Apache License 2.0 6 votes vote down vote up
public static Drawable fromDrawable(Drawable drawable, Resources r) {
    if (drawable != null) {
        if (drawable instanceof SelectableRoundedCornerDrawable) {
            return drawable;
        }
        else if (drawable instanceof LayerDrawable) {
            LayerDrawable ld = (LayerDrawable) drawable;
            final int num = ld.getNumberOfLayers();
            for (int i = 0; i < num; i++) {
                Drawable d = ld.getDrawable(i);
                ld.setDrawableByLayerId(ld.getId(i), fromDrawable(d, r));
            }
            return ld;
        }

        Bitmap bm = drawableToBitmap(drawable);
        if (bm != null) {
            return new SelectableRoundedCornerDrawable(bm, r);
        } else {
            Log.w(TAG, "Failed to create bitmap from drawable!");
        }
    }
    return drawable;
}
 
Example 4
Source File: RoundedImageView.java    From UltimateAndroid with Apache License 2.0 6 votes vote down vote up
private void updateAttrs(Drawable drawable) {
  if (drawable == null) { return; }

  if (drawable instanceof RoundedDrawable) {
    ((RoundedDrawable) drawable)
        .setScaleType(mScaleType)
        .setCornerRadius(cornerRadius)
        .setBorderWidth(borderWidth)
        .setBorderColor(borderColor)
        .setOval(isOval);
  } else if (drawable instanceof LayerDrawable) {
    // loop through layers to and set drawable attrs
    LayerDrawable ld = ((LayerDrawable) drawable);
    for (int i = 0, layers = ld.getNumberOfLayers(); i < layers; i++) {
      updateAttrs(ld.getDrawable(i));
    }
  }
}
 
Example 5
Source File: RoundedDrawable.java    From ImageLetterIcon with Apache License 2.0 6 votes vote down vote up
public static Drawable fromDrawable(Drawable drawable) {
    if (drawable != null) {
        if (drawable instanceof RoundedDrawable) {
            // just return if it's already a RoundedDrawable
            return drawable;
        } else if (drawable instanceof ColorDrawable) {
            // FIXME we don't support ColorDrawables yet
            return drawable;
        } else if (drawable instanceof LayerDrawable) {
            LayerDrawable ld = (LayerDrawable) drawable;
            int num = ld.getNumberOfLayers();
            // loop through layers to and change to RoundedDrawables if possible
            for (int i = 0; i < num; i++) {
                Drawable d = ld.getDrawable(i);
                ld.setDrawableByLayerId(ld.getId(i), fromDrawable(d));
            }
            return ld;
        }
        // try to get a bitmap from the drawable and
        Bitmap bm = drawableToBitmap(drawable);
        if (bm != null) {
            return new RoundedDrawable(bm);
        }
    }
    return drawable;
}
 
Example 6
Source File: RoundedDrawable.java    From Common with Apache License 2.0 6 votes vote down vote up
public static Drawable fromDrawable(Drawable drawable) {
    if (drawable != null) {
        if (drawable instanceof RoundedDrawable) {
            // just return if it's already a RoundedDrawable
            return drawable;
        } else if (drawable instanceof LayerDrawable) {
            LayerDrawable ld = (LayerDrawable) drawable;
            int num = ld.getNumberOfLayers();

            // loop through layers to and change to RoundedDrawables if possible
            for (int i = 0; i < num; i++) {
                Drawable d = ld.getDrawable(i);
                ld.setDrawableByLayerId(ld.getId(i), fromDrawable(d));
            }
            return ld;
        }

        // try to get a bitmap from the drawable and
        Bitmap bm = drawableToBitmap(drawable);
        if (bm != null) {
            return new RoundedDrawable(bm);
        }
    }
    return drawable;
}
 
Example 7
Source File: CubeImageView.java    From cube-sdk with Apache License 2.0 5 votes vote down vote up
/**
 * Notifies the drawable that it's displayed state has changed.
 *
 * @param drawable
 * @param isDisplayed
 */
private static void notifyDrawable(Drawable drawable, final boolean isDisplayed) {
    if (drawable instanceof RecyclingBitmapDrawable) {
        // The drawable is a CountingBitmapDrawable, so notify it
        ((RecyclingBitmapDrawable) drawable).setIsDisplayed(isDisplayed);
    } else if (drawable instanceof LayerDrawable) {
        // The drawable is a LayerDrawable, so recurse on each layer
        LayerDrawable layerDrawable = (LayerDrawable) drawable;
        for (int i = 0, z = layerDrawable.getNumberOfLayers(); i < z; i++) {
            notifyDrawable(layerDrawable.getDrawable(i), isDisplayed);
        }
    }
}
 
Example 8
Source File: FloatingActionButton.java    From floating-action-button with Apache License 2.0 5 votes vote down vote up
private void updateBackground(Drawable background) {
    if (background instanceof LayerDrawable) {
        LayerDrawable layers = (LayerDrawable) background;
        if (layers.getNumberOfLayers() == 2) {
            Drawable shadow = layers.getDrawable(0);
            Drawable circle = layers.getDrawable(1);

            if (shadow instanceof GradientDrawable) {
                if (mShadow) {
                    ((GradientDrawable) shadow.mutate()).setGradientRadius(getShadowRadius(shadow, circle));
                } else {
                    shadow.setAlpha(0);
                }
            }

            if (circle instanceof GradientDrawable) {
                initCircleDrawable(circle);
            }
        }
    } else if (background instanceof GradientDrawable) {
        initCircleDrawable(background);
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        if (mImplicitElevation) {
            setElevation(mShadow ? getResources().getDimension(R.dimen.floating_action_button_elevation) : 0f);
        }
    }

    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
        //noinspection deprecation
        setBackgroundDrawable(background);
    } else {
        setBackground(background);
    }
}
 
Example 9
Source File: RecyclingImageView.java    From FanXin-based-HuanXin with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Notifies the drawable that it's displayed state has changed.
 *
 * @param drawable
 * @param isDisplayed
 */
private static void notifyDrawable(Drawable drawable, final boolean isDisplayed) {
    if (drawable instanceof RecyclingBitmapDrawable) {
        // The drawable is a CountingBitmapDrawable, so notify it
        ((RecyclingBitmapDrawable) drawable).setIsDisplayed(isDisplayed);
    } else if (drawable instanceof LayerDrawable) {
        // The drawable is a LayerDrawable, so recurse on each layer
        LayerDrawable layerDrawable = (LayerDrawable) drawable;
        for (int i = 0, z = layerDrawable.getNumberOfLayers(); i < z; i++) {
            notifyDrawable(layerDrawable.getDrawable(i), isDisplayed);
        }
    }
}
 
Example 10
Source File: RoundedDrawable.java    From MyBlogDemo with Apache License 2.0 5 votes vote down vote up
public static Drawable fromDrawable(Drawable drawable) {
    if (drawable != null) {
        if (drawable instanceof RoundedDrawable) {
            // just return if it's already a RoundedDrawable
            return drawable;
        } else if (drawable instanceof LayerDrawable) {
            LayerDrawable ld = (LayerDrawable) drawable;
            int num = ld.getNumberOfLayers();

            // loop through layers to and change to RoundedDrawables if possible
            for (int i = 0; i < num; i++) {
                Drawable d = ld.getDrawable(i);
                ld.setDrawableByLayerId(ld.getId(i), fromDrawable(d));
            }
            return ld;
        }

        // try to get a bitmap from the drawable and
        Bitmap bm = drawableToBitmap(drawable);
        if (bm != null) {
            return new RoundedDrawable(bm);
        } else {
            Log.w(TAG, "Failed to create bitmap from drawable!");
        }
    }
    return drawable;
}
 
Example 11
Source File: RequestFunction.java    From sketch with Apache License 2.0 5 votes vote down vote up
/**
 * 修改Drawable显示状态
 *
 * @param callingStation 调用位置
 * @param drawable       Drawable
 * @param isDisplayed    是否已显示
 * @return true:drawable或其子Drawable是SketchDrawable
 */
private static boolean notifyDrawable(@NonNull String callingStation, @Nullable Drawable drawable, final boolean isDisplayed) {
    if (drawable == null) {
        return false;
    }

    boolean isSketchDrawable = false;

    if (drawable instanceof LayerDrawable) {
        LayerDrawable layerDrawable = (LayerDrawable) drawable;
        for (int i = 0, z = layerDrawable.getNumberOfLayers(); i < z; i++) {
            isSketchDrawable |= notifyDrawable(callingStation, layerDrawable.getDrawable(i), isDisplayed);
        }
    } else {
        if (!isDisplayed && drawable instanceof SketchLoadingDrawable) {
            SketchLoadingDrawable loadingDrawable = (SketchLoadingDrawable) drawable;
            DisplayRequest displayRequest = loadingDrawable.getRequest();
            if (displayRequest != null && !displayRequest.isFinished()) {
                displayRequest.cancel(CancelCause.BE_REPLACED_ON_SET_DRAWABLE);
            }
        }

        if (drawable instanceof SketchRefDrawable) {
            ((SketchRefDrawable) drawable).setIsDisplayed(callingStation, isDisplayed);
        } else if (drawable instanceof SketchGifDrawable) {
            if (!isDisplayed) {
                ((SketchGifDrawable) drawable).recycle();
            }
        }

        isSketchDrawable = drawable instanceof SketchDrawable;
    }

    return isSketchDrawable;
}
 
Example 12
Source File: RoundedImageView.java    From ClipCircleHeadLikeQQ with Apache License 2.0 5 votes vote down vote up
private void updateAttrs(Drawable drawable) {
  if (drawable == null) { return; }

  if (drawable instanceof RoundedDrawable) {
    ((RoundedDrawable) drawable)
        .setScaleType(mScaleType)
        .setBorderWidth(mBorderWidth)
        .setBorderColor(mBorderColor)
        .setOval(mIsOval)
        .setTileModeX(mTileModeX)
        .setTileModeY(mTileModeY);

    if (mCornerRadii != null) {
      ((RoundedDrawable) drawable).setCornerRadius(mCornerRadii[CORNER_TOP_LEFT],
          mCornerRadii[CORNER_TOP_RIGHT], mCornerRadii[CORNER_BOTTOM_RIGHT],
          mCornerRadii[CORNER_BOTTOM_LEFT]);
    }

    applyColorMod();
  } else if (drawable instanceof LayerDrawable) {
    // loop through layers to and set drawable attrs
    LayerDrawable ld = ((LayerDrawable) drawable);
    for (int i = 0, layers = ld.getNumberOfLayers(); i < layers; i++) {
      updateAttrs(ld.getDrawable(i));
    }
  }
}
 
Example 13
Source File: ActiveItemImageView.java    From letv with Apache License 2.0 5 votes vote down vote up
private void updateAttrs(Drawable drawable) {
    if (drawable != null) {
        if (drawable instanceof RoundedDrawable) {
            ((RoundedDrawable) drawable).setScaleType(this.mScaleType).setCornerRadius(this.cornerRadius).setBorderWidth(this.borderWidth).setBorderColor(this.borderColor).setOval(this.isOval);
        } else if (drawable instanceof LayerDrawable) {
            LayerDrawable ld = (LayerDrawable) drawable;
            int layers = ld.getNumberOfLayers();
            for (int i = 0; i < layers; i++) {
                updateAttrs(ld.getDrawable(i));
            }
        }
    }
}
 
Example 14
Source File: RoundedDrawable.java    From UltimateAndroid with Apache License 2.0 5 votes vote down vote up
public static Drawable fromDrawable(Drawable drawable) {
  if (drawable != null) {
    if (drawable instanceof RoundedDrawable) {
      // just return if it's already a RoundedDrawable
      return drawable;
    } else if (drawable instanceof LayerDrawable) {
      LayerDrawable ld = (LayerDrawable) drawable;
      int num = ld.getNumberOfLayers();

      // loop through layers to and change to RoundedDrawables if possible
      for (int i = 0; i < num; i++) {
        Drawable d = ld.getDrawable(i);
        ld.setDrawableByLayerId(ld.getId(i), fromDrawable(d));
      }
      return ld;
    }

    // try to get a bitmap from the drawable and
    Bitmap bm = drawableToBitmap(drawable);
    if (bm != null) {
      return new RoundedDrawable(bm);
    } else {
      Log.w(TAG, "Failed to create bitmap from drawable!");
    }
  }
  return drawable;
}
 
Example 15
Source File: BadgerTest.java    From Badger with Apache License 2.0 5 votes vote down vote up
void assertBadged(Drawable drawable, TestBadgeDrawable badge, Drawable... drawables) {
    assertThat(drawable, instanceOf(LayerDrawable.class));
    LayerDrawable layer = (LayerDrawable) drawable;
    assertThat(layer.findDrawableByLayerId(R.id.badger_drawable), is((Drawable) badge));
    int size = layer.getNumberOfLayers();
    if (drawables.length < size) {
        assertThat(layer.getDrawable(--size), is((Drawable) badge));
    }
    List<Drawable> layers = new ArrayList<>(size);
    for (int i = 0; i < size; i++) {
        layers.add(layer.getDrawable(i));
    }
    assertThat(layers, contains(drawables));
}
 
Example 16
Source File: RoundedImageView.java    From DMusic with Apache License 2.0 5 votes vote down vote up
private void updateAttrs(Drawable drawable, ScaleType scaleType) {
    if (drawable == null) {
        return;
    }

    if (drawable instanceof RoundedDrawable) {
        ((RoundedDrawable) drawable)
                .setScaleType(scaleType)
                .setBorderWidth(mBorderWidth)
                .setBorderColor(mBorderColor)
                .setOval(mIsOval)
                .setTileModeX(mTileModeX)
                .setTileModeY(mTileModeY);

        if (mCornerRadii != null) {
            ((RoundedDrawable) drawable).setCornerRadius(
                    mCornerRadii[Corner.TOP_LEFT],
                    mCornerRadii[Corner.TOP_RIGHT],
                    mCornerRadii[Corner.BOTTOM_RIGHT],
                    mCornerRadii[Corner.BOTTOM_LEFT]);
        }

        applyColorMod();
    } else if (drawable instanceof LayerDrawable) {
        // loop through layers to and set drawable attrs
        LayerDrawable ld = ((LayerDrawable) drawable);
        for (int i = 0, layers = ld.getNumberOfLayers(); i < layers; i++) {
            updateAttrs(ld.getDrawable(i), scaleType);
        }
    }
}
 
Example 17
Source File: RecyclingImageView.java    From vocefiscal-android with Apache License 2.0 5 votes vote down vote up
/**
 * Notifies the drawable that it's displayed state has changed.
 *
 * @param drawable
 * @param isDisplayed
 */
private static void notifyDrawable(Drawable drawable, final boolean isDisplayed) {
    if (drawable instanceof RecyclingBitmapDrawable) {
        // The drawable is a CountingBitmapDrawable, so notify it
        ((RecyclingBitmapDrawable) drawable).setIsDisplayed(isDisplayed);
    } else if (drawable instanceof LayerDrawable) {
        // The drawable is a LayerDrawable, so recurse on each layer
        LayerDrawable layerDrawable = (LayerDrawable) drawable;
        for (int i = 0, z = layerDrawable.getNumberOfLayers(); i < z; i++) {
            notifyDrawable(layerDrawable.getDrawable(i), isDisplayed);
        }
    }
}
 
Example 18
Source File: RecyclingImageView.java    From RoMote with Apache License 2.0 5 votes vote down vote up
/**
 * Notifies the drawable that it's displayed state has changed.
 *
 * @param drawable
 * @param isDisplayed
 */
private static void notifyDrawable(Drawable drawable, final boolean isDisplayed) {
    if (drawable instanceof RecyclingBitmapDrawable) {
        // The drawable is a CountingBitmapDrawable, so notify it
        ((RecyclingBitmapDrawable) drawable).setIsDisplayed(isDisplayed);
    } else if (drawable instanceof LayerDrawable) {
        // The drawable is a LayerDrawable, so recurse on each layer
        LayerDrawable layerDrawable = (LayerDrawable) drawable;
        for (int i = 0, z = layerDrawable.getNumberOfLayers(); i < z; i++) {
            notifyDrawable(layerDrawable.getDrawable(i), isDisplayed);
        }
    }
}
 
Example 19
Source File: RecyclingImageView.java    From graphics-samples with Apache License 2.0 5 votes vote down vote up
/**
 * Notifies the drawable that it's displayed state has changed.
 */
private static void notifyDrawable(Drawable drawable, final boolean isDisplayed) {
    if (drawable instanceof RecyclingBitmapDrawable) {
        // The drawable is a CountingBitmapDrawable, so notify it
        ((RecyclingBitmapDrawable) drawable).setIsDisplayed(isDisplayed);
    } else if (drawable instanceof LayerDrawable) {
        // The drawable is a LayerDrawable, so recurse on each layer
        LayerDrawable layerDrawable = (LayerDrawable) drawable;
        for (int i = 0, z = layerDrawable.getNumberOfLayers(); i < z; i++) {
            notifyDrawable(layerDrawable.getDrawable(i), isDisplayed);
        }
    }
}
 
Example 20
Source File: OverlayPinchImageView.java    From Augendiagnose with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Add the current overlay to the partial Bitmap. (This is done similar to refresh().)
 *
 * @param partialBitmap the partial bitmap before applying the overlay
 * @return the partial bitmap with overlay.
 */
public final Bitmap addOverlayToPartialBitmap(@NonNull final Bitmap partialBitmap) {
	List<Integer> overlayPositions = getOverlayPositions();
	if (overlayPositions.size() == 0) {
		return partialBitmap;
	}

	Drawable[] layers = new Drawable[overlayPositions.size() + 1];

	layers[0] = new BitmapDrawable(getResources(), partialBitmap);

	for (int i = 0; i < overlayPositions.size(); i++) {
		layers[i + 1] = getOverlayDrawable(overlayPositions.get(i));
	}
	LayerDrawable layerDrawable = new LayerDrawable(layers);

	// position overlays
	for (int i = 1; i < layerDrawable.getNumberOfLayers(); i++) {
		boolean isPupil = overlayPositions.get(i - 1) == OVERLAY_PUPIL_INDEX;

		if (isPupil) {
			float totalPupilOverlayScaleFactor = mPupilOverlayScaleFactor * mOverlayScaleFactor * OVERLAY_SIZE / 2;
			float pupilAdjustedOverlayX =
					mOverlayX + mPupilOverlayX * mOverlayScaleFactor * OVERLAY_SIZE * OVERLAY_CIRCLE_RATIO / mBitmap.getWidth();
			float pupilAdjustedOverlayY =
					mOverlayY + mPupilOverlayY * mOverlayScaleFactor * OVERLAY_SIZE * OVERLAY_CIRCLE_RATIO / mBitmap.getHeight();
			layerDrawable.setLayerInset(i,
					(int) (((pupilAdjustedOverlayX - mPosX) * mBitmap.getWidth() - totalPupilOverlayScaleFactor) * mScaleFactor
							+ (float) partialBitmap.getWidth() / 2),
					(int) (((pupilAdjustedOverlayY - mPosY) * mBitmap.getHeight() - totalPupilOverlayScaleFactor) * mScaleFactor
							+ (float) partialBitmap.getHeight() / 2),
					(int) (((mPosX - pupilAdjustedOverlayX) * mBitmap.getWidth() - totalPupilOverlayScaleFactor) * mScaleFactor
							+ (float) partialBitmap.getWidth() / 2),
					(int) (((mPosY - pupilAdjustedOverlayY) * mBitmap.getHeight() - totalPupilOverlayScaleFactor) * mScaleFactor
							+ (float) partialBitmap.getHeight() / 2));
		}
		else {
			layerDrawable.setLayerInset(i,
					(int) (((mOverlayX - mPosX) * mBitmap.getWidth() - OVERLAY_SIZE / 2 * mOverlayScaleFactor) * mScaleFactor
							+ (float) partialBitmap.getWidth() / 2),
					(int) (((mOverlayY - mPosY) * mBitmap.getHeight() - OVERLAY_SIZE / 2 * mOverlayScaleFactor) * mScaleFactor
							+ (float) partialBitmap.getHeight() / 2),
					(int) (((mPosX - mOverlayX) * mBitmap.getWidth() - OVERLAY_SIZE / 2 * mOverlayScaleFactor) * mScaleFactor
							+ (float) partialBitmap.getWidth() / 2),
					(int) (((mPosY - mOverlayY) * mBitmap.getHeight() - OVERLAY_SIZE / 2 * mOverlayScaleFactor) * mScaleFactor
							+ (float) partialBitmap.getHeight() / 2));
		}
	}

	layerDrawable.setBounds(0, 0, partialBitmap.getWidth(), partialBitmap.getHeight());

	Bitmap canvasBitmap = Bitmap.createBitmap(partialBitmap.getWidth(), partialBitmap.getHeight(), Bitmap.Config.ARGB_8888);
	Canvas canvas = new Canvas(canvasBitmap);
	layerDrawable.draw(canvas);

	return canvasBitmap;
}