[英]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.