This question already has an answer here:
这个问题已经有了答案:
- Changing ImageView source 7 answers
- 更改ImageView源7的答案
I have just started learning android. And i don't know How can I change the image of an ImageView
? ie it has some Image which was set in the layout but i want to change that image through coding how should i do it ?
我刚开始学android。我不知道如何改变ImageView的图像?ie它有一些在布局中设置的图像但是我想通过编码来改变图像我该怎么做呢?
Here is the xml file
这是xml文件。
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#cc8181"
>
<ImageView
android:id="@+id/image"
android:layout_width="50dip"
android:layout_height="fill_parent"
android:src="@drawable/icon"
android:layout_marginLeft="3dip"
android:scaleType="center"/>
thanks for replying.
谢谢你的回复。
4 个解决方案
#1
218
If you created imageview using xml file then follow the steps.
如果您使用xml文件创建了imageview,请遵循以下步骤。
Solution 1:
解决方案1:
Step 1: Create an XML file
步骤1:创建一个XML文件
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#cc8181"
>
<ImageView
android:id="@+id/image"
android:layout_width="50dip"
android:layout_height="fill_parent"
android:src="@drawable/icon"
android:layout_marginLeft="3dip"
android:scaleType="center"/>
</LinearLayout>
Step 2: create an Activity
步骤2:创建一个活动
ImageView img= (ImageView) findViewById(R.id.image);
img.setImageResource(R.drawable.my_image);
Solution 2:
解决方案2:
If you created imageview from Java Class
如果您从Java类中创建了imageview
ImageView img = new ImageView(this);
img.setImageResource(R.drawable.my_image);
#2
34
Have a look at the ImageView API. There are several setImage*
methods. Which one to use depends on the image you provide. If you have the image as resource (e.g. file res/drawable/my_image.png)
看看ImageView API。有几种setImage*方法。使用哪一个取决于您提供的映像。如果您将映像作为资源(例如文件res/drawable/my_image.png)
ImageView img = new ImageView(this); // or (ImageView) findViewById(R.id.myImageView);
img.setImageResource(R.drawable.my_image);
#3
13
Just to go a little bit further in the matter, you can also set a bitmap directly, like this:
在这个问题上更进一步,你也可以直接设置一个位图,像这样:
ImageView imageView = new ImageView(this);
Bitmap bImage = BitmapFactory.decodeResource(this.getResources(), R.drawable.my_image);
imageView.setImageBitmap(bImage);
Of course, this technique is only useful if you need to change the image.
当然,如果您需要更改映像,此技术仅是有用的。
#4
3
if (android.os.Build.VERSION.SDK_INT >= 21) {
storeViewHolder.storeNameTextView.setImageDrawable(context.getResources().getDrawable(array[position], context.getTheme()));
} else {
storeViewHolder.storeNameTextView.setImageDrawable(context.getResources().getDrawable(array[position]));
}
#1
218
If you created imageview using xml file then follow the steps.
如果您使用xml文件创建了imageview,请遵循以下步骤。
Solution 1:
解决方案1:
Step 1: Create an XML file
步骤1:创建一个XML文件
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#cc8181"
>
<ImageView
android:id="@+id/image"
android:layout_width="50dip"
android:layout_height="fill_parent"
android:src="@drawable/icon"
android:layout_marginLeft="3dip"
android:scaleType="center"/>
</LinearLayout>
Step 2: create an Activity
步骤2:创建一个活动
ImageView img= (ImageView) findViewById(R.id.image);
img.setImageResource(R.drawable.my_image);
Solution 2:
解决方案2:
If you created imageview from Java Class
如果您从Java类中创建了imageview
ImageView img = new ImageView(this);
img.setImageResource(R.drawable.my_image);
#2
34
Have a look at the ImageView API. There are several setImage*
methods. Which one to use depends on the image you provide. If you have the image as resource (e.g. file res/drawable/my_image.png)
看看ImageView API。有几种setImage*方法。使用哪一个取决于您提供的映像。如果您将映像作为资源(例如文件res/drawable/my_image.png)
ImageView img = new ImageView(this); // or (ImageView) findViewById(R.id.myImageView);
img.setImageResource(R.drawable.my_image);
#3
13
Just to go a little bit further in the matter, you can also set a bitmap directly, like this:
在这个问题上更进一步,你也可以直接设置一个位图,像这样:
ImageView imageView = new ImageView(this);
Bitmap bImage = BitmapFactory.decodeResource(this.getResources(), R.drawable.my_image);
imageView.setImageBitmap(bImage);
Of course, this technique is only useful if you need to change the image.
当然,如果您需要更改映像,此技术仅是有用的。
#4
3
if (android.os.Build.VERSION.SDK_INT >= 21) {
storeViewHolder.storeNameTextView.setImageDrawable(context.getResources().getDrawable(array[position], context.getTheme()));
} else {
storeViewHolder.storeNameTextView.setImageDrawable(context.getResources().getDrawable(array[position]));
}