2017-06-15 84 views

回答

0

我確定有更好的方法,但這是最基本的方法。這裏的MainActivity:

public class MainActivity extends AppCompatActivity { 

    private boolean mMaleImgClicked, mFemaleImgClicked; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     final ImageView maleImg = (ImageView) findViewById(R.id.male_imageview); 
     final ImageView femaleImg = (ImageView) findViewById(R.id.female_imageview); 

     maleImg.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       if (!mMaleImgClicked) { 
        maleImg.setImageResource(R.drawable.female_image); 

        mMaleImgClicked = true; 
       } else { 
        maleImg.setImageResource(R.drawable.male_image); 

        mMaleImgClicked = false; 
       } 
      } 
     }); 
     femaleImg.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       if (!mFemaleImgClicked) { 
        femaleImg.setImageResource(R.drawable.male_image); 

        mFemaleImgClicked = true; 
       } else { 
        femaleImg.setImageResource(R.drawable.female_image); 

        mFemaleImgClicked = false; 
       } 
      } 
     }); 
    } 
} 

...和activity_main:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <ImageView 
     android:id="@+id/male_imageview" 
     android:src="@drawable/male_image" 
     android:layout_gravity="center_vertical" 
     android:layout_weight="1" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" /> 

    <ImageView 
     android:id="@+id/female_imageview" 
     android:src="@drawable/female_image" 
     android:layout_gravity="center_vertical" 
     android:layout_weight="1" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" /> 

</LinearLayout> 
+1

謝謝,它已成功>> –

0

您可以使用setColorFilter每個imageview的,當你點擊,只需添加到ImageView的onClick

imageview.setColorFilter(Color.argb(50, 0, 0, 0));