简体   繁体   中英

Change ImageView image in Java on Android

Following steps:

  1. I create dynamically an ImageView and set it to a bitmap that I have downloaded from server previously. The image is display small as a thumbnail by scaling it down using setLayoutParams without changing its real size. I have a horizontal LinearLayout in my main XML layout file on top of the display that gets populated with such thumbnails. Just imagine a row of thumbnails on top of the display. This works!

  2. In the main XML layout file I have a bigger ImageView below the LinearLayout (as described above) that should show the big version of the thumbnail that I click. So somehow I must manage to assign the bitmap in the thumbnail ImageView to the bigger ImageView. It is not possible to simply to this: bigImageView = imageViewArray[i];

Can you please let me know how this assignment can be done?

Here is the main XML layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".GalleryWithBaseadapter"
    android:background="#000000">

    <HorizontalScrollView
        android:id="@+id/horizontalContainer"
        android:background="#000000"
        android:layout_width="wrap_content"
        android:layout_height="120dp">

        <LinearLayout
            android:orientation="horizontal"
            android:id="@+id/imgLinearLayout"
            android:background="#000000"
            android:layout_gravity="left"
            android:layout_width="wrap_content"
            android:layout_height="match_parent">

        </LinearLayout>

    </HorizontalScrollView>

    <ProgressBar
        android:id="@+id/myProgressBar"
        android:visibility="visible"
        style="?android:attr/progressBarStyleHorizontal"
        android:max="100"
        android:layout_gravity="center_horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:text="Click Me"
        android:onClick="onClickButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <ImageView
        android:id="@+id/bigImage"
        android:padding="4dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

Thank you for advice!

Try this:

bigImageView.setImageDrawable(imageViewArray[i].getDrawable());

By the way you should set the large image from the file source and load small scaled versions of the images into your thumbnail list to avoid memory problems.

Try this:

ImageView bigImage = ...
ImageView[] smallImages = ...


public void setSmallToBig(int positionInSmallArray){
    Drawable d = smallImages[positionInSmallArray].getDrawable();
    bigImage.setDrawableImage(d);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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