如何在Android中使用ImageView显示图像

Bre*_*ead 28 android imageview

我正在寻找将图像src分配给图像视图控件的方法.我读了几个例子,他们说了些什么,src="@drawable\image"但是不明白这一点,我想在运行时通过java代码分配图像src也想在XML中应用默认图像.

Mic*_*Bak 63

如果要在手机上显示图像文件,可以执行以下操作:

private ImageView mImageView;
mImageView = (ImageView) findViewById(R.id.imageViewId);
mImageView.setImageBitmap(BitmapFactory.decodeFile("pathToImageFile"));
Run Code Online (Sandbox Code Playgroud)

如果要显示可绘制资源中的图像,请执行以下操作:

private ImageView mImageView;
mImageView = (ImageView) findViewById(R.id.imageViewId);
mImageView.setImageResource(R.drawable.imageFileId);
Run Code Online (Sandbox Code Playgroud)

您将drawable在项目res文件夹中找到该文件夹.您可以将图像文件放在那里.


Pan*_*iya 13

您可以在XML文件中设置imageview,如下所示:

<ImageView
    android:id="@+id/image1"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    android:src="@drawable/imagep1" />
Run Code Online (Sandbox Code Playgroud)

你可以在android java文件中定义图像视图,如:

ImageView imageView = (ImageView) findViewById(R.id.imageViewId);
Run Code Online (Sandbox Code Playgroud)

并使用drawable设置Image:

imageView.setImageResource(R.drawable.imageFileId);
Run Code Online (Sandbox Code Playgroud)

并使用您的内存文件夹设置图像,如:

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();
}
Run Code Online (Sandbox Code Playgroud)