我正在寻找将图片src分配给ImageView控件的方法。我读了一些例子,他们说了一些关于src="@drawable\image"
的内容,但我没有理解。同时,我也想在运行时通过Java代码指定图片src,并且在XML中应用默认图片。
src="@drawable/image"
。我正在寻找将图片src分配给ImageView控件的方法。我读了一些例子,他们说了一些关于src="@drawable\image"
的内容,但我没有理解。同时,我也想在运行时通过Java代码指定图片src,并且在XML中应用默认图片。
src="@drawable/image"
。如果您想在手机上显示图片文件,可以按照以下步骤进行:
private ImageView mImageView;
mImageView = (ImageView) findViewById(R.id.imageViewId);
mImageView.setImageBitmap(BitmapFactory.decodeFile("pathToImageFile"));
如果你想要显示来自drawable资源的图片,可以这样做:
private ImageView mImageView;
mImageView = (ImageView) findViewById(R.id.imageViewId);
mImageView.setImageResource(R.drawable.imageFileId);
在项目的res
文件夹中可以找到drawable
文件夹(们)。您可以将图像文件放在那里。
setImageResource(R.drawable.black_x)
。 - Michell Bak您可以在XML文件中设置ImageView,就像这样:
<ImageView
android:id="@+id/image1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/imagep1" />
你可以在 Android 的 Java 文件中定义图片视图,例如:
ImageView imageView = (ImageView) findViewById(R.id.imageViewId);
并使用可绘制的图像来设置图像,例如:
imageView.setImageResource(R.drawable.imageFileId);
并使用您的内存文件夹设置图像,例如:
File file = new File(SupportedClass.getString("pbg"));
if (file.exists()) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap selectDrawable = BitmapFactory.decodeFile(file.getAbsolutePath(), options);
imageView.setImageBitmap(selectDrawable);
}
else
{
Toast.makeText(getApplicationContext(), "File not Exist", Toast.LENGTH_SHORT).show();
}
在res文件夹中选择您想要查看图像的XML文件,
<ImageView
android:id="@+id/image1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/imagep1" />
将图像从硬盘拖到项目的Drawable文件夹中,然后在代码中像这样使用它:
ImageView image;
image = (ImageView) findViewById(R.id.yourimageviewid);
image.setImageResource(R.drawable.imagename);