[英]Changing image to another image and backforth in Android using imageview
当用户单击图像并返回时更改图像。问题是,当图像A更改为图像B时,它可以工作...但是我无法将图像B转换为图像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);
}
您应该使用ViewSwitcher。 ViewSwitcher允许您在两个视图之间动态切换。 例如,您可以在ImageView和解释ImageView内容的TextView之间切换。 如果您需要更多信息,我已经写了一篇博客文章,介绍如何在Android中创建一个简单的ViewSwitcher: 一个简单的ANDROID VIEWSWITCHER示例
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.