简体   繁体   English

如何在android中的imageview中设置图像?

[英]How to set image in imageview in android?

I want to show an image in imageview so I made a folder - drawable in res and put my image there.我想在 imageview 中显示一个图像,所以我创建了一个文件夹 - 在res drawable并将我的图像放在那里。 Something like apple.png .类似apple.png东西。 Then I use this code:然后我使用这个代码:

ImageView iv= (ImageView)findViewById(R.id.img_selected_image);
String path = getApplication().getFilesDir().getAbsolutePath();
InputStream is = new FileInputStream(path + "/apple.png");
Drawable icon = new BitmapDrawable(is);
Log.i("Fnord", "width="+icon.getIntrinsicWidth()+
     " height="+icon.getIntrinsicHeight());
iv.setImageDrawable(icon);

But when i run it, it said there isn't any image by apple.png name in data/data/package-name/files .但是当我运行它时,它说data/data/package-name/files没有apple.png名称的任何图像。 I think i put my image in inappropriate place.我想我把我的形象放在了不合适的地方。 Where should i put my image ?我应该把我的形象放在哪里?

you can directly give the Image name in your setimage as iv.setImageResource(R.drawable.apple); 你可以直接在你的setimage中给出图像名称iv.setImageResource(R.drawable.apple); that should be it. 那应该是它。

使用以下代码,

    iv.setImageResource(getResources().getIdentifier("apple", "drawable", getPackageName()));

Instead of setting drawable resource through code in your activity class you can also set in XML layout: 您可以在XML布局中设置:而不是通过活动类中的代码设置可绘制资源:

Code is as follows: 代码如下:

<ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/apple" />
// take variable of imageview

private ImageView mImageView;

//bind imageview with your xml's id

mImageView = (ImageView)findViewById(R.id.mImageView);

//set resource for imageview

mImageView.setImageResource(R.drawable.your_image_name);

The images your put into res/drawable are handled by Android. 放入res / drawable的图像由Android处理。 There is no need for you to get the image the way you did. 您无需按照自己的方式获取图像。 in your case you could simply call iv.setImageRessource(R.drawable.apple) 在你的情况下你可以简单地调用iv.setImageRessource(R.drawable.apple)

to just get the image (and not adding it to the ImageView directly), you can call Context.getRessources().getDrawable(R.drawable.apple) to get the image 要获取图像(而不是直接将其添加到ImageView),可以调用Context.getRessources().getDrawable(R.drawable.apple)来获取图像

        ImageView iv= (ImageView)findViewById(R.id.img_selected_image);
        public static int getDrawable(Context context, String name)//method to get id
        {
         Assert.assertNotNull(context);
         Assert.assertNotNull(name);
        return context.getResources().getIdentifier(name,    //return id
            "your drawable", context.getPackageName());
        }
        image.setImageResource(int Id);//set id using this method

If you created ImageView from Java Class 如果您从Java类创建了ImageView

ImageView img = new ImageView(this);

//Here we are setting the image in image view
img.setImageResource(R.drawable.my_image);

if u want to set a bitmap object to image view here is simple two line` 如果你想设置一个位图对象到图像视图这里是简单的两行`

Bitmap bitmap=BitmapFactory.decodeResource(getResources(),R.drawable.sample_drawable_image);
image.setImageBitmap(bitmap);

1> You can add image from layout itself: 1>您可以从布局本身添加图像:

<ImageView
                android:id="@+id/iv_your_image"
                android:layout_width="wrap_content"
                android:layout_height="25dp"
                android:background="@mipmap/your_image"
                android:padding="2dp" />

OR 要么

2> Programmatically in java class: 2>在java类中以编程方式:

ImageView ivYouImage= (ImageView)findViewById(R.id.iv_your_image);
        ivYouImage.setImageResource(R.mipmap.ic_changeImage);

OR for fragments : 片段

View rowView= inflater.inflate(R.layout.your_layout, null, true);

ImageView ivYouImage= (ImageView) rowView.findViewById(R.id.iv_your_image);
        ivYouImage.setImageResource(R.mipmap.ic_changeImage);

在imageview中设置图像

imageView.setImageResource(R.drawable.myImage);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM