アイコンのような任意の画像を表示できます。表示する画像は、リソースやコンテント プロバイダなどから読み込みます。
| XML属性 | 関連メソッド | 説明 |
|---|---|---|
| android:adjustViewBounds | setAdjustViewBounds | Set this to true if you want the ImageView to adjust its bounds to preserve the aspect ratio of its drawable. |
| android:baseline | setBaseline | The offset of the baseline within this view. |
| android:baselineAlignBottom | setBaselineAlignBottom | 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 | setMaxHeight | ビューの最大高さ (オプション) |
| android:maxWidth | setMaxWidth | ビューの最大幅 (オプション) |
| android:scaleType | setScaleType | Controls how the image should be resized or moved to match the size of this ImageView. |
| android:src | setImageResource | 表示画像をDrawableから設定する。 |
| android:tint | setColorFilter | Set a tinting color for the image. |
アクセシビリティを向上させるため、android:contentDescriptionを指定すべきです。
画像を表示するには、画像のソースの違いにより次の4つのメソッドがあります。
| メソッド | 説明 |
|---|---|
void setImageBitmap(
Bitmap bm)
|
表示画像をBitmapから設定する。 |
void setImageDrawable(
Drawable drawable)
|
表示画像をDrawableから設定する。 |
void setImageResource(
int resId)
|
表示画像をリソースから設定する。 |
void setImageURI(
Uri uri)
|
表示画像をURIから設定する。 ※指定できるのは、リソースかローカルのファイルのみ |
デバイス上のファイルを表示するには、Drawableを使用して次のように記述できます。
Drawable drawable = Drawable.createFromPath("/data/sample.bmp");
ImageView imageView = (ImageView) findViewById(R.id.imageView1);
imageView.setImageDrawable(drawable);
setImageURI()では、リモートのファイルからは読み込めません。読み込もうとすると「Unable to decode stream: java.io.FileNotFoundException: /http:/example.com/test.bmp: open failed: ENOENT (No such file or directory)」のようなエラーが発生します。よってInputStreamなどを介してDrawableから読み込む必要がありますが、リモートとの通信はバックグラウンドのスレッドで行わなければなりません。一方でViewのサブクラスであるImageViewはUIスレッドで操作しなければならないため、処理にはスレッドの考慮が必要です。このような煩雑な処理は、AsyncTaskクラスにより容易に実現できます。
| メソッド | 説明 |
|---|---|
Drawable getDrawable() |
Return the view's drawable, or null if no drawable has been assigned. |
void invalidateDrawable(
Drawable dr)
|
Invalidates the specified Drawable. |
| 区分 | メソッド | 説明 |
|---|---|---|
final void clearColorFilter() |
||
final void setColorFilter(
int color)
|
Set a tinting option for the image. | |
void setColorFilter(
ColorFilter cf)
|
Apply an arbitrary colorfilter to the image. | |
final void setColorFilter(
int color,
PorterDuff.Mode mode)
|
Set a tinting option for the image. | |
int getBaseline() |
Return the offset of the widget's text baseline from the widget's top boundary. | |
void setBaseline(
int baseline)
|
Set the offset of the widget's text baseline from the widget's top boundary. | |
boolean getBaselineAlignBottom() |
Return whether this view's baseline will be considered the bottom of the view. | |
void setBaselineAlignBottom(
boolean aligned)
|
Set whether to set the baseline of this view to the bottom of the view. | |
Matrix getImageMatrix() |
Return the view's optional matrix. | |
void setImageMatrix(
Matrix matrix)
|
||
ImageView.ScaleType getScaleType() |
Return the current scale type in use by this ImageView. | |
void setScaleType(
ImageView.ScaleType scaleType)
|
Controls how the image should be resized or moved to match the size of this ImageView. | |
int[] onCreateDrawableState(
int extraSpace)
|
Generate the new Drawable state for this view. | |
void setImageState(
int[] state,
boolean merge)
|
||
void jumpDrawablesToCurrentState() |
Call Drawable.jumpToCurrentState() on all Drawable objects associated with this view. | |
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 setMaxWidth(
int maxWidth)
|
An optional argument to supply a maximum width for this view. | |
void setMaxHeight(
int maxHeight)
|
An optional argument to supply a maximum height for this view. | |
void setAlpha(
int alpha)
|
||
void setImageLevel(
int level)
|
Sets the image level, when it is constructed from a LevelListDrawable. | |
void setSelected(
boolean selected)
|
Changes the selection state of this view. |