简体   繁体   中英

Changing image to another image and backforth in Android using imageview

change image when user click on the image and backforth .The problem is it works when image A change to image B ...But i cant get image B to image A

   <ImageView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/imageView"
    android:layout_alignParentTop="true"
    android:layout_alignParentStart="true"
    android:src="@drawable/img1"
    android:onClick="fade" />

<ImageView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/imageView2hi"
    android:layout_alignParentTop="true"
    android:layout_alignParentStart="true"
    android:alpha="0"
    android:src="@drawable/img2hello"
    android:onClick="fade2" />


public class MainActivity extends AppCompatActivity {

public void fade(View view)
{
    ImageView img= (ImageView)findViewById(R.id.imageView);
    img.animate().alpha(0f).setDuration(2000);

    ImageView img1=(ImageView)findViewById(R.id.imageView2hi);
    img1.animate().alpha(1f).setDuration(2000);
}
public  void fade2(View view)
{
    ImageView img1=(ImageView)findViewById(R.id.imageView2hi);
    img1.animate().alpha(0f).setDuration(2000);
    ImageView img= (ImageView)findViewById(R.id.imageView);
    img.animate().alpha(1f).setDuration(2000);
}

You should use a ViewSwitcher. A ViewSwitcher allows you to switch between two view dynamically. For example, you could switch between a ImageView and a TextView that explains the content of the ImageView. If you want more information, I've written a blog post on creating a simple ViewSwitcher in Android: A SIMPLE ANDROID VIEWSWITCHER EXAMPLE

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