0% found this document useful (0 votes)
22 views37 pages

Image View Demo

The ImageView class in Android is used to display image resources such as Bitmaps and Drawables, and it allows for image scaling and tinting. It includes various XML attributes for customization, such as 'scaleType', 'src', and 'tint', along with public methods for manipulating images and their properties. The class also provides constructors for creating ImageView instances with different parameters, and supports accessibility features.

Uploaded by

ff.ssg227
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views37 pages

Image View Demo

The ImageView class in Android is used to display image resources such as Bitmaps and Drawables, and it allows for image scaling and tinting. It includes various XML attributes for customization, such as 'scaleType', 'src', and 'tint', along with public methods for manipulating images and their properties. The class also provides constructors for creating ImageView instances with different parameters, and supports accessibility features.

Uploaded by

ff.ssg227
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

ImageView

bookmark_border
Kotlin |Java

public class ImageView


extends View

↳ [Link]
[Link]

↳ [Link]
Known direct subclasses
ImageButton, QuickContactBadge
Known indirect subclasses
ZoomButton

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="[Link]
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:adjustViewBou Set this to true if you want the ImageView to adjust its bounds to preserve the aspect ratio of its dra
nds
android:baseline The offset of the baseline within this view.
android:baselineAlignB If true, the image view will be baseline aligned with based on its bottom edge.
ottom
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


From class [Link]

Inherited constants
From class [Link]

Inherited fields
From class [Link]

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 m

Applies a temporary transformatio


drawable when it is drawn.
final void clearColorFilter()

Removes the image's ColorFilter


void drawableHotspotChanged(fl
This function is called whenever t
changes and needs to be propagate
child views managed by the view.
CharSequence getAccessibilityClassName(

Return the class name of this obje


accessibility purposes.
boolean getAdjustViewBounds()

True when ImageView is adjustin


preserve the aspect ratio of its dra
int getBaseline()

Return the offset of the widget's te


widget's top boundary.
boolean getBaselineAlignBottom()

Checks whether this view's baselin


bottom of the view.
ColorFilter getColorFilter()

Returns the active color filter for t


boolean getCropToPadding()

Return whether this ImageView c


Drawable getDrawable()

Gets the current Drawable, or null


been assigned.
int getImageAlpha()

Returns the alpha that will be appl


this ImageView.
Matrix getImageMatrix()

Returns the view's optional matrix


BlendMode getImageTintBlendMode()

Gets the blending mode used to ap


image Drawable
ColorStateList getImageTintList()

Get the current ColorStateList


Drawable, or null if no tint is appl
[Link] getImageTintMode()

Gets the blending mode used to ap


image Drawable
int getMaxHeight()

The maximum height of this view


int getMaxWidth()

The maximum width of this view.


[Link] getScaleType()

Returns the current ScaleType tha


bounds of an image to the bounds
boolean hasOverlappingRendering()

Returns whether this View has con


void invalidateDrawable(Drawab

Invalidates the specified Drawable


boolean isOpaque()

Indicates whether this View is opa


void jumpDrawablesToCurrentSt

Call [Link]
Drawable objects associated with
int[] onCreateDrawableState(int

Generate the new Drawable stat


void onRtlPropertiesChanged(int

Called when any RTL property (la


direction or text alignment) has be
void onVisibilityAggregated(boo

Called when the user-visibility of


affected by a change to this view i
or the window this view is attache
void setAdjustViewBounds(boole
adjustViewBounds)
Set this to true if you want the Ima
bounds to preserve the aspect ratio
void setAlpha(int alpha)

This method was deprecated in AP


#setImageAlpha(int) instead
void setBaseline(int baseline)

Set the offset of the widget's text b


widget's top boundary.
void setBaselineAlignBottom(bo

Sets whether the baseline of this v


the view.
final void setColorFilter(int
color, [Link] mod

Sets a tinting option for the image


void setColorFilter(ColorFilter cf)

Apply an arbitrary colorfilter to th


final void setColorFilter(int color)

Set a tinting option for the image.


void setCropToPadding(boolean

Sets whether this ImageView will


void setImageAlpha(int alpha)

Sets the alpha value that should be


void setImageBitmap(Bitmap bm

Sets a Bitmap as the content of thi


void setImageDrawable(Drawab

Sets a drawable as the content of t


void setImageIcon(Icon icon)

Sets the content of this ImageView


void setImageLevel(int level)

Sets the image level, when it is co


a LevelListDrawable.
void setImageMatrix(Matrix mat

Adds a transformation Matrix tha


view's drawable when it is drawn.
void setImageResource(int resId

Sets a drawable as the content of t


void setImageState(int[] state, b

Set the state of the current StateL


void setImageTintBlendMode(Bl
de)

Specifies the blending mode used


specified
by setImageTintList(android
StateList)} to the image drawab
void setImageTintList(ColorState

Applies a tint to the image drawab


void setImageTintMode(PorterD
)

Specifies the blending mode used


specified
by setImageTintList(android
StateList)} to the image drawab
void setImageURI(Uri uri)

Sets the content of this ImageView


void setMaxHeight(int maxHeigh

An optional argument to supply a


this view.
void setMaxWidth(int maxWidth

An optional argument to supply a


this view.
void setScaleType(ImageView.S
e)

Controls how the image should be


match the size of this ImageView.
void setSelected(boolean select
Changes the selection state of this
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
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.
boole setFrame(int l, int t, int r, int b)
an
boole verifyDrawable(Drawable dr)
an
If your view subclass is displaying its own Drawable objects, it should override this function and return true for any Draw

Inherited methods
From class [Link]
From class [Link]
From interface [Link]
From interface [Link]
From interface [Link]

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.

May be a boolean value, such as "true" or "false".

Related methods:

 setAdjustViewBounds(boolean)

android:baseline

The offset of the baseline within this view. See {see [Link]#getBaseline} for
details

May be a dimension value, which is a floating point number appended with a unit such
as "14.5sp". Available units are: px (pixels), dp (density-independent pixels), sp (scaled
pixels based on preferred font size), in (inches), and mm (millimeters).

Related methods:

 setBaseline(int)

android:baselineAlignBottom

If true, the image view will be baseline aligned with based on its bottom edge.

May be a boolean value, such as "true" or "false".

Related methods:

 setBaselineAlignBottom(boolean)

android:cropToPadding

If true, the image will be cropped to fit within its padding.

May be a boolean value, such as "true" or "false".

Related methods:

 setCropToPadding(boolean)
android:maxHeight

An optional argument to supply a maximum height for this view. See {see
[Link]#setMaxHeight} for details.

May be a dimension value, which is a floating point number appended with a unit such
as "14.5sp". Available units are: px (pixels), dp (density-independent pixels), sp (scaled
pixels based on preferred font size), in (inches), and mm (millimeters).

Related methods:

 setMaxHeight(int)

android:maxWidth

An optional argument to supply a maximum width for this view. See {see
[Link]#setMaxWidth} for details.

May be a dimension value, which is a floating point number appended with a unit such
as "14.5sp". Available units are: px (pixels), dp (density-independent pixels), sp (scaled
pixels based on preferred font size), in (inches), and mm (millimeters).

Related methods:

 setMaxWidth(int)

android:scaleType

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

Must be one of the following constant values.

Constant Value Description

center 5 Center the image in the view, but perform no scaling.


centerCrop 6 Scale the image uniformly (maintain the image's aspect ratio) so both dimensions (width and height) of the im
larger than the corresponding dimension of the view (minus padding). The image is then centered in the view.
centerInside 7 Scale the image uniformly (maintain the image's aspect ratio) so that both dimensions (width and height) of th
to or less than the corresponding dimension of the view (minus padding). The image is then centered in the vi
fitCenter 3 Scale the image using [Link].
fitEnd 4 Scale the image using [Link].
fitStart 2 Scale the image using [Link].
fitXY 1 Scale the image using [Link].
matrix 0 Scale using the image matrix when drawing. See [Link](Matrix).

Related methods:

 setScaleType([Link])

android:src

Sets a drawable as the content of this ImageView.

May be a reference to another resource, in the form "@[+][package:]type/name" or a


theme attribute in the form "?[package:]type/name".

May be a color value, in the form of "#rgb", "#argb", "#rrggbb", or "#aarrggbb".

Related methods:

 setImageResource(int)

android:tint

The tinting color for the image. By default, the tint will blend using SRC_ATOP mode.
Please note that for compatibility reasons, this is NOT consistent with the default
SRC_IN tint mode used by [Link](ColorStateList) and by similar
tint attributes on other views.

May be a color value, in the form of "#rgb", "#argb", "#rrggbb", or "#aarrggbb".

Related methods:

 setImageTintList(ColorStateList)

android:tintMode

Blending mode used to apply the image tint.

Must be one of the following constant values.


Constant Value Description

add 10 Combines the tint and drawable color and alpha channels, clamping the result to valid color values. Saturate(S +
multiply e Multiplies the color and alpha channels of the drawable with those of the tint. [Sa * Da, Sc * Dc]
screen f [Sa + Da - Sa * Da, Sc + Dc - Sc * Dc]
src_atop 9 The tint is drawn above the drawable, but with the drawable\u2019s alpha channel masking the result. [Da, Sc *
src_in 5 The tint is masked by the alpha channel of the drawable. The drawable\u2019s color channels are thrown out. [S
src_over 3 The tint is drawn on top of the drawable. [Sa + (1 - Sa)*Da, Rc = Sc + (1 - Sa)*Dc]

Related methods:

 setImageTintMode([Link])

Public constructors

ImageView
Added in API level 1
public ImageView (Context context)

Parameters

conte Context
xt

ImageView
Added in API level 1
public ImageView (Context context,
AttributeSet attrs)

Parameters

conte Context
xt
attrs AttributeSet: This value may be null.
ImageView
Added in API level 1
public ImageView (Context context,
AttributeSet attrs,
int defStyleAttr)

Parameters

context Context
attrs AttributeSet: This value may be null.
defStyleA int
ttr

ImageView
Added in API level 21
public ImageView (Context context,
AttributeSet attrs,
int defStyleAttr,
int defStyleRes)

Parameters

context Context
attrs AttributeSet: This value may be null.
defStyleA int
ttr
defStyleR int
es

Public methods

animateTransform
Added in API level 29
public void animateTransform (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 ImageView#setImageMatrix(Matrix) to use only
during animations as this matrix will be cleared after the next drawable update or view's
bounds change.

Parameters

matr Matrix: The transformation parameters in matrix form. This value may be null.
ix

clearColorFilter
Added in API level 1
public final void clearColorFilter ()

Removes the image's ColorFilter.

See also:

 setColorFilter(int)
 getColorFilter()

drawableHotspotChanged
Added in API level 21
public 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.

Dispatching to child views is handled by dispatchDrawableHotspotChanged(float,


float).

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


If you override this method you must call through to the superclass implementation.

Parameters

xfloat: hotspot x coordinate


yfloat: hotspot y coordinate

getAccessibilityClassName
Added in API level 23
public CharSequence getAccessibilityClassName ()

Return the class name of this object to be used for accessibility purposes. Subclasses
should only override this if they are implementing something that should be seen as a
completely new class of view when used by accessibility, unrelated to the class it is
deriving from. This is used to fill in [Link].

Returns

CharSeque
nce

getAdjustViewBounds
Added in API level 16
public boolean getAdjustViewBounds ()

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

Related XML Attributes:

 android:adjustViewBounds

Returns

boole whether to adjust the bounds of this view to preserve the original aspect ratio of the drawable
an

See also:

 setAdjustViewBounds(boolean)

getBaseline
Added in API level 1
public int getBaseline ()

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

Returns

in the offset of the baseline within the widget's bounds or -1 if baseline alignment is not supported.
t

getBaselineAlignBottom
Added in API level 11
public boolean getBaselineAlignBottom ()

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

Returns

boole True if the ImageView's baseline is considered the bottom of the view, false if otherwise.
an

See also:

 setBaselineAlignBottom(boolean)

getColorFilter
Added in API level 16
public ColorFilter getColorFilter ()

Returns the active color filter for this ImageView.

Returns

ColorFilt the active color filter for this ImageView


er

See also:

 setColorFilter([Link])
getCropToPadding
Added in API level 16
public boolean getCropToPadding ()

Return whether this ImageView crops to padding.

Related XML Attributes:

 android:cropToPadding

Returns

boole whether this ImageView crops to padding


an

See also:

 setCropToPadding(boolean)

getDrawable
Added in API level 1
public Drawable getDrawable ()

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

Returns

Drawab the view's drawable, or null if no drawable has been assigned.


le

getImageAlpha
Added in API level 16
public int getImageAlpha ()

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

Returns
in the alpha value that will be applied to the drawable of this ImageView (between 0 and 255 inclusive, with 0 being transparent
t

See also:

 setImageAlpha(int)

getImageMatrix
Added in API level 1
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().

Returns

Matri
x

getImageTintBlendMode
Added in API level 29
public BlendMode getImageTintBlendMode ()

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

Related XML Attributes:

 android:tintMode

Returns

BlendMo the blending mode used to apply the tint to the image Drawable This value may be null.
de

See also:

 setImageTintBlendMode(BlendMode)
getImageTintList
Added in API level 21
public ColorStateList getImageTintList ()

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

Related XML Attributes:

 android:tint

Returns

ColorStateL the tint applied to the image drawable


ist

See also:

 setImageTintList(ColorStateList)

getImageTintMode
Added in API level 21
public [Link] getImageTintMode ()

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

Related XML Attributes:

 android:tintMode

Returns

PorterDuff.M the blending mode used to apply the tint to the image Drawable This value may be null.
ode

See also:

 setImageTintMode([Link])

getMaxHeight
Added in API level 16
public int getMaxHeight ()

The maximum height of this view.

Related XML Attributes:

 android:maxHeight

Returns

in The maximum height of this view


t

See also:

 setMaxHeight(int)

getMaxWidth
Added in API level 16
public int getMaxWidth ()

The maximum width of this view.

Related XML Attributes:

 android:maxWidth

Returns

in The maximum width of this view


t

See also:

 setMaxWidth(int)

getScaleType
Added in API level 1
public [Link] getScaleType ()
Returns the current ScaleType that is used to scale the bounds of an image to the
bounds of the ImageView.

Related XML Attributes:

 android:scaleType

Returns

[Link] The ScaleType used to scale the image.


Type

See also:

 [Link]

hasOverlappingRendering
Added in API level 16
public boolean hasOverlappingRendering ()

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 forceHasOverlappingRendering(boolean) has been called on this view.

Returns

boole true if the content in this view might overlap, false otherwise.
an
invalidateDrawable
Added in API level 1
public void invalidateDrawable (Drawable dr)

Invalidates the specified Drawable.

Parameters

d Drawable: This value cannot be null.


r

isOpaque
Added in API level 7
public boolean isOpaque ()

Indicates whether this View is opaque. An opaque View guarantees that it will draw all
the pixels overlapping its bounds using a fully opaque color. Subclasses of View should
override this method whenever possible to indicate whether an instance is opaque.
Opaque Views are treated in a special way by the View hierarchy, possibly allowing it to
perform optimizations during invalidate/draw passes.

Returns

boole True if this View is guaranteed to be fully opaque, false otherwise.


an

jumpDrawablesToCurrentState
Added in API level 11
public void jumpDrawablesToCurrentState ()

Call [Link]() on all Drawable objects associated with this view.

Also calls StateListAnimator#jumpToCurrentState() if there is a StateListAnimator


attached to this view.
If you override this method you must call through to the superclass implementation.
onCreateDrawableState
Added in API level 1
public int[] onCreateDrawableState (int extraSpace)

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 getDrawableState().

Parameters

extraSp int: if non-zero, this is the number of extra entries you would like in the returned array in which you can place your o
ace

Returns

int[] Returns an array holding the current Drawable state of the view.

onRtlPropertiesChanged
Added in API level 17
public void onRtlPropertiesChanged (int layoutDirection)

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.

Parameters

layoutDirect int: the direction of the layout Value is View.LAYOUT_DIRECTION_LTR, or View.LAYOUT_DIRECTION


ion

onVisibilityAggregated
Added in API level 24
public 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.
If you override this method you must call through to the superclass implementation.
Parameters

isVisibl boolean: true if this view and all of its ancestors are [Link] and this view's window is also visible
e

setAdjustViewBounds
Added in API level 1
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.

Note: If the application targets API level 17 or lower, adjustViewBounds will allow the
drawable to shrink the view bounds, but not grow to fill available measured space in all
cases. This is for compatibility with legacy MeasureSpec and RelativeLayout behavior.

Related XML Attributes:

 android:adjustViewBounds

Parameters

adjustViewBou boolean: Whether to adjust the bounds of this view to preserve the original aspect ratio of the drawable.
nds

See also:

 getAdjustViewBounds()

setAlpha
Added in API level 1
Deprecated in API level 16
public void setAlpha (int alpha)

This method was deprecated in API level 16.


use #setImageAlpha(int) instead

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


Parameters

alph int: the alpha value that should be applied to the image
a

setBaseline
Added in API level 11
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.

Related XML Attributes:

 android:baseline

Parameters

baseli int: The baseline to use, or -1 if none is to be provided.


ne

See also:

 setBaseline(int)

setBaselineAlignBottom
Added in API level 11
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.

Related XML Attributes:

 android:baselineAlignBottom

Parameters

align boolean: If true, the image view will be baseline aligned by its bottom edge.
ed

setColorFilter
Added in API level 1
public final void setColorFilter (int color,
[Link] mode)

Sets a tinting option for the image.

Related XML Attributes:

 android:tint

Parameters

colo int: Color tint to apply.


r
mod [Link]: How to apply the color. The standard mode is [Link]#SRC_ATOP
e

setColorFilter
Added in API level 1
public void setColorFilter (ColorFilter cf)

Apply an arbitrary colorfilter to the image.

Parameters

c ColorFilter: the colorfilter to apply (may be null)


f

See also:

 getColorFilter()

setColorFilter
Added in API level 8
public final void setColorFilter (int color)

Set a tinting option for the image. Assumes [Link]#SRC_ATOP blending


mode.

Related XML Attributes:

 android:tint

Parameters

colo int: Color tint to apply.


r

setCropToPadding
Added in API level 16
public void setCropToPadding (boolean cropToPadding)

Sets whether this ImageView will crop to padding.

Related XML Attributes:

 android:cropToPadding

Parameters

cropToPadd boolean: whether this ImageView will crop to padding


ing

See also:

 getCropToPadding()

setImageAlpha
Added in API level 16
public void setImageAlpha (int alpha)

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


Parameters

alph int: the alpha value that should be applied to the image (between 0 and 255 inclusive, with 0 being transparent and 255 bei
a

See also:

 getImageAlpha()

setImageBitmap
Added in API level 1
public void setImageBitmap (Bitmap bm)

Sets a Bitmap as the content of this ImageView.

Parameters

b Bitmap: The bitmap to set


m

setImageDrawable
Added in API level 1
public void setImageDrawable (Drawable drawable)

Sets a drawable as the content of this ImageView.

Parameters

drawa Drawable: the Drawable to set, or null to clear the content


ble

setImageIcon
Added in API level 23
public void setImageIcon (Icon icon)
Sets the content of this ImageView to the specified Icon.

Depending on the Icon type, this may do Bitmap reading and decoding on the UI thread, which can
cause UI jank. If that's a concern, consider using Icon#loadDrawableAsync(Context,
[Link], Handler) and
then setImageDrawable([Link]) instead.

Parameters

ico Icon: an Icon holding the desired image, or null to clear the content
n

setImageLevel
Added in API level 1
public void setImageLevel (int level)

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

Parameters

lev int: The new level for the image.


el

setImageMatrix
Added in API level 1
public void setImageMatrix (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

matr Matrix: The transformation parameters in matrix form.


ix

setImageResource
Added in API level 1
public void setImageResource (int resId)

Sets a drawable as the content of this ImageView.

This does Bitmap reading and decoding on the UI thread, which can cause a latency hiccup. If that's
a concern, consider
using setImageDrawable([Link]) or setImageBitmap(and
[Link]) and BitmapFactory instead.

Related XML Attributes:

 android:src

Parameters

resI int: the resource identifier of the drawable


d

setImageState
Added in API level 1
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

stat int: the state to set for the StateListDrawable


e
mer boolean: if true, merges the state values for the state you specify into the current state
ge

setImageTintBlendMode
Added in API level 29
public void setImageTintBlendMode (BlendMode blendMode)
Specifies the blending mode used to apply the tint specified
by setImageTintList([Link]) } to the image drawable. The
default mode is BlendMode#SRC_IN.

Related XML Attributes:

 android:tintMode

Parameters

blendMo BlendMode: the blending mode used to apply the tint, may be null to clear tint
de

See also:

 getImageTintMode()
 [Link](BlendMode)

setImageTintList
Added in API level 21
public void setImageTintList (ColorStateList tint)

Applies a tint to the image drawable. Does not modify the current tint mode, which
is [Link]#SRC_IN by default.

Subsequent calls to setImageDrawable([Link]) will


automatically mutate the drawable and apply the specified tint and tint mode
using Drawable#setTintList(ColorStateList).

Note: The default tint mode used by this setter is NOT consistent with the default tint
mode used by the android:tint attribute. If the android:tint attribute is specified, the
default tint mode will be set to [Link]#SRC_ATOP to ensure consistency with
earlier versions of the platform.

Related XML Attributes:

 android:tint

Parameters

tin ColorStateList: the tint to apply, may be null to clear tint


t
See also:

 getImageTintList()
 [Link](ColorStateList)

setImageTintMode
Added in API level 21
public void setImageTintMode ([Link] tintMode)

Specifies the blending mode used to apply the tint specified


by setImageTintList([Link]) } to the image drawable. The
default mode is [Link]#SRC_IN.

Related XML Attributes:

 android:tintMode

Parameters

tintMo [Link]: the blending mode used to apply the tint, may be null to clear tint
de

See also:

 getImageTintMode()
 [Link]([Link])

setImageURI
Added in API level 1
public void setImageURI (Uri uri)

Sets the content of this ImageView to the specified Uri. Note that you use this method to
load images from a local Uri only.

To learn how to display images from a remote Uri see: Handling Bitmaps

This does Bitmap reading and decoding on the UI thread, which can cause a latency hiccup. If that's
a concern, consider
using setImageDrawable([Link]) or setImageBitmap(and
[Link]) and BitmapFactory instead.
On devices running SDK < 24, this method will fail to apply correct density scaling to images loaded
from content and file schemes. Applications running on devices with SDK >= 24 MUST specify
the targetSdkVersion in their manifest as 24 or above for density scaling to be applied to images
loaded from these schemes.

Parameters

ur Uri: the Uri of an image, or null to clear the content


i

setMaxHeight
Added in API level 1
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([Link]) to
determine how to fit the image within the bounds.

Related XML Attributes:

 android:maxHeight

Parameters

maxHei int: maximum height for this view


ght

See also:

 getMaxHeight()

setMaxWidth
Added in API level 1
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([Link]) to
determine how to fit the image within the bounds.

Related XML Attributes:

 android:maxWidth

Parameters

maxWid int: maximum width for this view


th

See also:

 getMaxWidth()

setScaleType
Added in API level 1
public void setScaleType ([Link] scaleType)

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

Related XML Attributes:

 android:scaleType

Parameters

scaleTy [Link]: The desired scaling mode.


pe
setSelected
Added in API level 1
public void setSelected (boolean selected)

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.

Parameters

select boolean: true if the view must be selected, false otherwise


ed

setVisibility
Added in API level 1
public void setVisibility (int visibility)

Set the visibility state of this view.

Parameters

visibilit int: One of [Link], [Link], or [Link]. Value is [Link], [Link], or Vi


y

Protected methods

drawableStateChanged
Added in API level 1
protected 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.

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.
If you override this method you must call through to the superclass implementation.

onAttachedToWindow
Added in API level 1
protected void onAttachedToWindow ()

This is called when the view is attached to a window. At this point it has a Surface and
will start drawing. Note that this function is guaranteed to be called
before onDraw([Link]), however it may be called any time before
the first onDraw -- including before or after onMeasure(int, int).
If you override this method you must call through to the superclass implementation.

onDetachedFromWindow
Added in API level 1
protected void onDetachedFromWindow ()

This is called when the view is detached from a window. At this point it no longer has a
surface for drawing.
If you override this method you must call through to the superclass implementation.

onDraw
Added in API level 1
protected void onDraw (Canvas canvas)

Implement this to do your drawing.

Parameters

canv Canvas: the canvas on which the background will be drawn This value cannot be null.
as

onMeasure
Added in API level 1
protected void onMeasure (int widthMeasureSpec,
int heightMeasureSpec)

Measure the view and its content to determine the measured width and the measured
height. This method is invoked by 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 setMeasuredDimension(int,


int) to store the measured width and height of this view. Failure to do so will trigger
an IllegalStateException, thrown by measure(int, int). Calling the
superclass' onMeasure(int, int) 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 onMeasure(int,
int) to provide better measurements of their content.

If this method is overridden, it is the subclass's responsibility to make sure the


measured height and width are at least the view's minimum height and width
(getSuggestedMinimumHeight() and getSuggestedMinimumWidth()).

Parameters

widthMeasureS int: horizontal space requirements as imposed by the parent. The requirements are encoded with [Link]
pec
heightMeasure int: vertical space requirements as imposed by the parent. The requirements are encoded with [Link]
Spec

setFrame
Added in API level 1
protected boolean setFrame (int l,
int t,
int r,
int b)

Parameters

l int
t int
r int
b int
Returns

boole
an

verifyDrawable
Added in API level 1
protected 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. This allows animations for
those drawables to be scheduled.

Be sure to call through to the super class when overriding this function.
If you override this method you must call through to the superclass implementation.

Parameters

dr Drawable: This value cannot be null.

Returns

boole boolean If true then the Drawable is being displayed in the view; else false and it is not allowed to animate.
an

You might also like