Class ImageView

java.lang.Object
icyllis.modernui.view.View
icyllis.modernui.widget.ImageView
All Implemented Interfaces:
Drawable.Callback
Direct Known Subclasses:
ImageButton

public class ImageView extends View
Displays image resources, for example Image or Drawable resources. ImageView is also commonly used to apply tints to an image and handle image scaling.

To learn more about Drawables, see: Drawable Resources.

  • Constructor Details

    • ImageView

      public ImageView(Context context)
  • Method Details

    • verifyDrawable

      protected boolean verifyDrawable(@NonNull Drawable dr)
      Description copied from class: View
      If your view subclass is displaying its own Drawable objects, it should override this function and return true for any Drawable it is displaying. This allows animations for those drawables to be scheduled.

      Be sure to call through to the super class when overriding this function.

      Overrides:
      verifyDrawable in class View
      Parameters:
      dr - The Drawable to verify. Return true if it is one you are displaying, else return the result of calling through to the super class.
      Returns:
      boolean If true then the Drawable is being displayed in the view; else false and it is not allowed to animate.
      See Also:
    • jumpDrawablesToCurrentState

      public void jumpDrawablesToCurrentState()
      Description copied from class: View
      Call Drawable.jumpToCurrentState() on all Drawable objects associated with this view.

      Also calls StateListAnimator.jumpToCurrentState() if there is a StateListAnimator attached to this view.

      Overrides:
      jumpDrawablesToCurrentState in class View
    • invalidateDrawable

      public void invalidateDrawable(@NonNull Drawable dr)
      Description copied from class: View
      Invalidates the specified Drawable.
      Specified by:
      invalidateDrawable in interface Drawable.Callback
      Overrides:
      invalidateDrawable in class View
      Parameters:
      dr - the drawable to invalidate
    • hasOverlappingRendering

      public boolean hasOverlappingRendering()
      Description copied from class: View
      Returns whether this View has content which overlaps.

      This function, intended to be overridden by specific View types, is an optimization when alpha is set on a view. If rendering overlaps in a view with alpha < 1, that view is drawn to an offscreen buffer and then composited into place, which can be expensive. If the view has no overlapping rendering, the view can draw each primitive with the appropriate alpha value directly. An example of overlapping rendering is a TextView with a background image, such as a Button. An example of non-overlapping rendering is a TextView with no background, or an ImageView with only the foreground image. The default implementation returns true; subclasses should override if they have cases which can be optimized.

      Note: The return value of this method is ignored if View.forceHasOverlappingRendering(boolean) has been called on this view.

      Overrides:
      hasOverlappingRendering in class View
      Returns:
      true if the content in this view might overlap, false otherwise.
    • getAdjustViewBounds

      public boolean getAdjustViewBounds()
      True when ImageView is adjusting its bounds to preserve the aspect ratio of its drawable
      Returns:
      whether to adjust the bounds of this view to preserve the original aspect ratio of the drawable
      See Also:
    • setAdjustViewBounds

      public void setAdjustViewBounds(boolean adjustViewBounds)
      Set this to true if you want the ImageView to adjust its bounds to preserve the aspect ratio of its drawable.
      Parameters:
      adjustViewBounds - Whether to adjust the bounds of this view to preserve the original aspect ratio of the drawable.
      See Also:
    • getMaxWidth

      public int getMaxWidth()
      The maximum width of this view.
      Returns:
      The maximum width of this view
      See Also:
    • setMaxWidth

      public void setMaxWidth(int maxWidth)
      An optional argument to supply a maximum width for this view. Only valid if setAdjustViewBounds(boolean) has been set to true. To set an image to be a maximum of 100 x 100 while preserving the original aspect ratio, do the following: 1) set adjustViewBounds to true 2) set maxWidth and maxHeight to 100 3) set the height and width layout params to WRAP_CONTENT.

      Note that this view could be still smaller than 100 x 100 using this approach if the original image is small. To set an image to a fixed size, specify that size in the layout params and then use setScaleType(ScaleType) to determine how to fit the image within the bounds.

      Parameters:
      maxWidth - maximum width for this view
      See Also:
    • getMaxHeight

      public int getMaxHeight()
      The maximum height of this view.
      Returns:
      The maximum height of this view
      See Also:
    • setMaxHeight

      public void setMaxHeight(int maxHeight)
      An optional argument to supply a maximum height for this view. Only valid if setAdjustViewBounds(boolean) has been set to true. To set an image to be a maximum of 100 x 100 while preserving the original aspect ratio, do the following: 1) set adjustViewBounds to true 2) set maxWidth and maxHeight to 100 3) set the height and width layout params to WRAP_CONTENT.

      Note that this view could be still smaller than 100 x 100 using this approach if the original image is small. To set an image to a fixed size, specify that size in the layout params and then use setScaleType(ScaleType) to determine how to fit the image within the bounds.

      Parameters:
      maxHeight - maximum height for this view
      See Also:
    • getDrawable

      @Nullable public Drawable getDrawable()
      Gets the current Drawable, or null if no Drawable has been assigned.
      Returns:
      the view's drawable, or null if no drawable has been assigned.
    • setImageDrawable

      public void setImageDrawable(@Nullable Drawable drawable)
      Sets a drawable as the content of this ImageView.
      Parameters:
      drawable - the Drawable to set, or null to clear the content
    • setImageTintList

      public void setImageTintList(@Nullable ColorStateList tint)
      Applies a tint to the image drawable. Does not modify the current tint mode, which is BlendMode.SRC_IN by default.

      Subsequent calls to setImageDrawable(Drawable) will automatically mutate the drawable and apply the specified tint and tint mode using Drawable.setTintList(ColorStateList).

      Parameters:
      tint - the tint to apply, may be null to clear tint
      See Also:
    • getImageTintList

      @Nullable public ColorStateList getImageTintList()
      Get the current ColorStateList used to tint the image Drawable, or null if no tint is applied.
      Returns:
      the tint applied to the image drawable
      See Also:
    • setImageTintBlendMode

      public void setImageTintBlendMode(@Nullable BlendMode blendMode)
      Specifies the blending mode used to apply the tint specified by setImageTintList(ColorStateList)} to the image drawable. The default mode is BlendMode.SRC_IN.
      Parameters:
      blendMode - the blending mode used to apply the tint, may be null to clear tint
      See Also:
    • getImageTintBlendMode

      @Nullable public BlendMode getImageTintBlendMode()
      Gets the blending mode used to apply the tint to the image Drawable
      Returns:
      the blending mode used to apply the tint to the image Drawable
      See Also:
    • setImage

      public void setImage(@Nullable Image image)
      Sets a Image as the content of this ImageView.
      Parameters:
      image - the image to set
    • setImageState

      public void setImageState(int[] state, boolean merge)
      Set the state of the current StateListDrawable. For more information about State List Drawables, see: the Drawable Resource Guide.
      Parameters:
      state - the state to set for the StateListDrawable
      merge - if true, merges the state values for the state you specify into the current state
    • setSelected

      public void setSelected(boolean selected)
      Description copied from class: View
      Changes the selection state of this view. A view can be selected or not. Note that selection is not the same as focus. Views are typically selected in the context of an AdapterView like ListView or GridView; the selected view is the view that is highlighted.
      Overrides:
      setSelected in class View
      Parameters:
      selected - true if the view must be selected, false otherwise
    • setImageLevel

      public void setImageLevel(int level)
      Sets the image level, when it is constructed from a LevelListDrawable.
      Parameters:
      level - The new level for the image.
    • setScaleType

      public void setScaleType(@NonNull ImageView.ScaleType scaleType)
      Controls how the image should be resized or moved to match the size of this ImageView.
      Parameters:
      scaleType - The desired scaling mode.
    • getScaleType

      @NonNull public ImageView.ScaleType getScaleType()
      Returns the current ScaleType that is used to scale the bounds of an image to the bounds of the ImageView.
      Returns:
      The ScaleType used to scale the image.
      See Also:
    • getImageMatrix

      public Matrix getImageMatrix()
      Returns the view's optional matrix. This is applied to the view's drawable when it is drawn. If there is no matrix, this method will return an identity matrix. Do not change this matrix in place but make a copy. If you want a different matrix applied to the drawable, be sure to call setImageMatrix().
    • setImageMatrix

      public void setImageMatrix(@Nullable Matrix matrix)
      Adds a transformation Matrix that is applied to the view's drawable when it is drawn. Allows custom scaling, translation, and perspective distortion.
      Parameters:
      matrix - The transformation parameters in matrix form.
    • getCropToPadding

      public boolean getCropToPadding()
      Return whether this ImageView crops to padding.
      Returns:
      whether this ImageView crops to padding
      See Also:
    • setCropToPadding

      public void setCropToPadding(boolean cropToPadding)
      Sets whether this ImageView will crop to padding.
      Parameters:
      cropToPadding - whether this ImageView will crop to padding
      See Also:
    • onCreateDrawableState

      @NonNull public int[] onCreateDrawableState(int extraSpace)
      Description copied from class: View
      Generate the new Drawable state for this view. This is called by the view system when the cached Drawable state is determined to be invalid. To retrieve the current state, you should use View.getDrawableState().
      Overrides:
      onCreateDrawableState in class View
      Parameters:
      extraSpace - if non-zero, this is the number of extra entries you would like in the returned array in which you can place your own states.
      Returns:
      an array holding the current Drawable state of the view.
      See Also:
    • onRtlPropertiesChanged

      public void onRtlPropertiesChanged(int layoutDirection)
      Description copied from class: View
      Called when any RTL property (layout direction or text direction or text alignment) has been changed.

      Subclasses need to override this method to take care of cached information that depends on the resolved layout direction, or to inform child views that inherit their layout direction.

      The default implementation does nothing.

      Overrides:
      onRtlPropertiesChanged in class View
      Parameters:
      layoutDirection - the direction of the layout
      See Also:
    • onMeasure

      protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
      Description copied from class: View
      Measure the view and its content to determine the measured width and the measured height. This method is invoked by View.measure(int, int) and should be overridden by subclasses to provide accurate and efficient measurement of their contents.

      CONTRACT: When overriding this method, you must call View.setMeasuredDimension(int, int) to store the measured width and height of this view. Failure to do so will trigger an IllegalStateException, thrown by View.measure(int, int). Calling super.onMeasure() is a valid use.

      The base class implementation of measure defaults to the background size, unless a larger size is allowed by the MeasureSpec. Subclasses should override the base one to provide better measurements of their content.

      Overrides:
      onMeasure in class View
      Parameters:
      widthMeasureSpec - width measure specification imposed by the parent MeasureSpec
      heightMeasureSpec - height measure specification imposed by the parent MeasureSpec
    • onSizeChanged

      protected void onSizeChanged(int width, int height, int prevWidth, int prevHeight)
      Description copied from class: View
      Called when width or height changed
      Overrides:
      onSizeChanged in class View
      Parameters:
      width - new width
      height - new height
      prevWidth - previous width
      prevHeight - previous height
    • drawableStateChanged

      protected void drawableStateChanged()
      Description copied from class: View
      This function is called whenever the state of the view changes in such a way that it impacts the state of drawables being shown.

      If the View has a StateListAnimator, it will also be called to run necessary state change animations.

      Be sure to call through to the superclass when overriding this function.

      Overrides:
      drawableStateChanged in class View
      See Also:
    • drawableHotspotChanged

      public void drawableHotspotChanged(float x, float y)
      Description copied from class: View
      This function is called whenever the view hotspot changes and needs to be propagated to drawables or child views managed by the view.

      Dispatching to child views is handled by View.dispatchDrawableHotspotChanged(float, float).

      Be sure to call through to the superclass when overriding this function.

      Overrides:
      drawableHotspotChanged in class View
      Parameters:
      x - hotspot x coordinate
      y - hotspot y coordinate
    • animateTransform

      public void animateTransform(@Nullable Matrix matrix)
      Applies a temporary transformation Matrix to the view's drawable when it is drawn. Allows custom scaling, translation, and perspective distortion during an animation.

      This method is a lightweight analogue of setImageMatrix(Matrix) to use only during animations as this matrix will be cleared after the next drawable update or view's bounds change.

      Parameters:
      matrix - The transformation parameters in matrix form.
    • onDraw

      protected void onDraw(@NonNull Canvas canvas)
      Description copied from class: View
      Draw the content of this view, implement this to do your drawing.

      Note that (0, 0) will be the top left of the bounds, and (width, height) will be the bottom right of the bounds.

      Overrides:
      onDraw in class View
      Parameters:
      canvas - the canvas to draw content
    • getBaseline

      public int getBaseline()

      Return the offset of the widget's text baseline from the widget's top boundary.

      Overrides:
      getBaseline in class View
      Returns:
      the offset of the baseline within the widget's bounds or -1 if baseline alignment is not supported.
    • setBaseline

      public void setBaseline(int baseline)

      Set the offset of the widget's text baseline from the widget's top boundary. This value is overridden by the setBaselineAlignBottom(boolean) property.

      Parameters:
      baseline - The baseline to use, or -1 if none is to be provided.
      See Also:
    • setBaselineAlignBottom

      public void setBaselineAlignBottom(boolean aligned)
      Sets whether the baseline of this view to the bottom of the view. Setting this value overrides any calls to setBaseline.
      Parameters:
      aligned - If true, the image view will be baseline aligned by its bottom edge.
    • getBaselineAlignBottom

      public boolean getBaselineAlignBottom()
      Checks whether this view's baseline is considered the bottom of the view.
      Returns:
      True if the ImageView's baseline is considered the bottom of the view, false if otherwise.
      See Also:
    • getColorFilter

      @Nullable public ColorFilter getColorFilter()
      Returns the active color filter for this ImageView.
      Returns:
      the active color filter for this ImageView
      See Also:
    • setColorFilter

      public void setColorFilter(@Nullable ColorFilter colorFilter)
      Apply an arbitrary color filter to the image.
      Parameters:
      colorFilter - the color filter to apply (can be null)
      See Also:
    • getImageAlpha

      public int getImageAlpha()
      Returns the alpha that will be applied to the drawable of this ImageView.
      Returns:
      the alpha value that will be applied to the drawable of this ImageView (between 0 and 255 inclusive, with 0 being transparent and 255 being opaque)
      See Also:
    • setImageAlpha

      public void setImageAlpha(int alpha)
      Sets the alpha value that should be applied to the image.
      Parameters:
      alpha - the alpha value that should be applied to the image (between 0 and 255 inclusive, with 0 being transparent and 255 being opaque)
      See Also:
    • onVisibilityAggregated

      public void onVisibilityAggregated(boolean isVisible)
      Description copied from class: View
      Called when the user-visibility of this View is potentially affected by a change to this view itself, an ancestor view or the window this view is attached to.
      Overrides:
      onVisibilityAggregated in class View
      Parameters:
      isVisible - true if this view and all of its ancestors are View.VISIBLE and this view's window is also visible