Jak Mogę zmienić obraz ImageView? [duplikat]

To pytanie ma już odpowiedź tutaj:

Właśnie zacząłem uczyć się Androida. I nie wiem jak Mogę zmienić obraz ImageView? ie ma jakiś obraz, który został ustawiony w układzie, ale chcę zmienić ten obraz poprzez kodowanie jak mam to zrobić ?

Oto XML plik

<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"/>

Dzięki za odpowiedź.
Author: BBdev, 2011-02-23

4 answers

Jeśli program imageview został utworzony za pomocą pliku xml, postępuj zgodnie z instrukcjami.

Rozwiązanie 1:

Krok 1: Utwórz plik 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>

Krok 2: Utwórz aktywność

ImageView img= (ImageView) findViewById(R.id.image);
img.setImageResource(R.drawable.my_image);

Rozwiązanie 2:

Jeśli stworzyłeś imageview z klasy Java

ImageView img = new ImageView(this);
img.setImageResource(R.drawable.my_image);
 231
Author: Sankar Ganesh,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-01-27 05:30:20

Spójrz na ImageView API . Istnieje kilka metod setImage*. Który z nich należy użyć, zależy od dostarczonego obrazu. Jeśli masz obraz jako zasób (np. plik res / drawable / my_image.png)

ImageView img = new ImageView(this);  // or (ImageView) findViewById(R.id.myImageView);
img.setImageResource(R.drawable.my_image);
 36
Author: FrVaBe,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-02-23 10:01:42

Aby pójść nieco dalej w tej sprawie, Możesz również ustawić bitmapę bezpośrednio, Tak:

ImageView imageView = new ImageView(this);  
Bitmap bImage = BitmapFactory.decodeResource(this.getResources(), R.drawable.my_image);
imageView.setImageBitmap(bImage);

Oczywiście ta technika jest przydatna tylko wtedy, gdy trzeba zmienić obraz.

 13
Author: rotterick,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-12-05 19:50:58
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]));
}
 3
Author: Rohit Mandiwal,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-08-09 15:35:43