ImageView


public class ImageView
extends View

java.lang.Object
   ↳ android.view.View
     ↳ android.widget.ImageView


Displays image resources, for example Bitmap or Drawable resources. ImageView is also commonly used to apply tints to an image and handle image scaling.

The following XML snippet is a common example of using an ImageView to display an image resource:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/my_image"
        android:contentDescription="@string/my_image_description"
        />
</LinearLayout>

To learn more about Drawables, see: Drawable Resources. To learn more about working with Bitmaps, see: Handling Bitmaps.

Summary

XML attributes

android:adjustViewBounds Set this to true if you want the ImageView to adjust its bounds to preserve the aspect ratio of its drawable. 
android:baseline The offset of the baseline within this view. 
android:baselineAlignBottom If true, the image view will be baseline aligned with based on its bottom edge. 
android:cropToPadding If true, the image will be cropped to fit within its padding. 
android:maxHeight An optional argument to supply a maximum height for this view. 
android:maxWidth An optional argument to supply a maximum width for this view. 
android:scaleType Controls how the image should be resized or moved to match the size of this ImageView. 
android:src Sets a drawable as the content of this ImageView. 
android:tint The tinting color for the image. 
android:tintMode Blending mode used to apply the image tint. 

Inherited XML attributes

Inherited constants

Inherited fields

Public constructors

ImageView(Context context)
ImageView(Context context, AttributeSet attrs)
ImageView(Context context, AttributeSet attrs, int defStyleAttr)
ImageView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)

Public methods

void animateTransform(Matrix matrix)

Applies a temporary transformation Matrix to the view's drawable when it is drawn.

final void clearColorFilter()

Removes the image's ColorFilter.

void drawableHotspotChanged(float x, float y)

This function is called whenever the view hotspot changes and needs to be propagated to drawables or child views managed by the view.

CharSequence getAccessibilityClassName()

Return the class name of this object to be used for accessibility purposes.

boolean getAdjustViewBounds()

True when ImageView is adjusting its bounds to preserve the aspect ratio of its drawable

int getBaseline()

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

boolean getBaselineAlignBottom()

Checks whether this view's baseline is considered the bottom of the view.

ColorFilter getColorFilter()

Returns the active color filter for this ImageView.

boolean getCropToPadding()

Return whether this ImageView crops to padding.

Drawable getDrawable()

Gets the current Drawable, or null if no Drawable has been assigned.

int getImageAlpha()

Returns the alpha that will be applied to the drawable of this ImageView.

Matrix getImageMatrix()

Returns the view's optional matrix.

BlendMode getImageTintBlendMode()

Gets the blending mode used to apply the tint to the image Drawable

ColorStateList getImageTintList()

Get the current ColorStateList used to tint the image Drawable, or null if no tint is applied.

PorterDuff.Mode getImageTintMode()

Gets the blending mode used to apply the tint to the image Drawable

int getMaxHeight()

The maximum height of this view.

int getMaxWidth()

The maximum width of this view.

ImageView.ScaleType getScaleType()

Returns the current ScaleType that is used to scale the bounds of an image to the bounds of the ImageView.

boolean hasOverlappingRendering()

Returns whether this View has content which overlaps.

void invalidateDrawable(Drawable dr)

Invalidates the specified Drawable.

boolean isOpaque()

Indicates whether this View is opaque.

void jumpDrawablesToCurrentState()

Call Drawable.jumpToCurrentState() on all Drawable objects associated with this view.

int[] onCreateDrawableState(int extraSpace)

Generate the new Drawable state for this view.

void onRtlPropertiesChanged(int layoutDirection)

Called when any RTL property (layout direction or text direction or text alignment) has been changed.

void onVisibilityAggregated(boolean isVisible)

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.

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.

void setAlpha(int alpha)

This method was deprecated in API level 16. use #setImageAlpha(int) instead

void setBaseline(int baseline)

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

void setBaselineAlignBottom(boolean aligned)

Sets whether the baseline of this view to the bottom of the view.

final void setColorFilter(int color, PorterDuff.Mode mode)

Sets a tinting option for the image.

void setColorFilter(ColorFilter cf)

Apply an arbitrary colorfilter to the image.

final void setColorFilter(int color)

Set a tinting option for the image.

void setCropToPadding(boolean cropToPadding)

Sets whether this ImageView will crop to padding.

void setImageAlpha(int alpha)

Sets the alpha value that should be applied to the image.

void setImageBitmap(Bitmap bm)

Sets a Bitmap as the content of this ImageView.

void setImageDrawable(Drawable drawable)

Sets a drawable as the content of this ImageView.

void setImageIcon(Icon icon)

Sets the content of this ImageView to the specified Icon.

void setImageLevel(int level)

Sets the image level, when it is constructed from a LevelListDrawable.

void setImageMatrix(Matrix matrix)

Adds a transformation Matrix that is applied to the view's drawable when it is drawn.

void setImageResource(int resId)

Sets a drawable as the content of this ImageView.

void setImageState(int[] state, boolean merge)

Set the state of the current StateListDrawable.

void setImageTintBlendMode(BlendMode blendMode)

Specifies the blending mode used to apply the tint specified by setImageTintList(ColorStateList)} to the image drawable.

void setImageTintList(ColorStateList tint)

Applies a tint to the image drawable.

void setImageTintMode(PorterDuff.Mode tintMode)

Specifies the blending mode used to apply the tint specified by setImageTintList(ColorStateList)} to the image drawable.

void setImageURI(Uri uri)

Sets the content of this ImageView to the specified Uri.

void setMaxHeight(int maxHeight)

An optional argument to supply a maximum height for this view.

void setMaxWidth(int maxWidth)

An optional argument to supply a maximum width for this view.

void setScaleType(ImageView.ScaleType scaleType)

Controls how the image should be resized or moved to match the size of this ImageView.

void setSelected(boolean selected)

Changes the selection state of this view.

void setVisibility(int visibility)

Set the visibility state of this view.

Protected methods

void drawableStateChanged()

This function is called whenever the state of the view changes in such a way that it impacts the state of drawables being shown.

void onAttachedToWindow()

This is called when the view is attached to a window.

void onDetachedFromWindow()

This is called when the view is detached from a window.

void onDraw(Canvas canvas)

Implement this to do your drawing.

void onMeasure(int widthMeasureSpec, int heightMeasureSpec)

Measure the view and its content to determine the measured width and the measured height.

boolean setFrame(int l, int t, int r, int b)

boolean verifyDrawable(Drawable dr)

If your view subclass is displaying its own Drawable objects, it should override this function and return true for any Drawable it is displaying.

Inherited methods