The selector Effect of ImageView.
During normal development, if we add selector to the Button to present the pressed and normal effects, it will greatly improve the user experience, however, when we use ImageView as "one by one" Button ", we find that setting selector directly does not work. Of course, our application is dimmed. Then we can only find a solution to this situation.
First define a selector file:
<selector xmlns:android="http://schemas.android.com/apk/res/android" > <item android:state_pressed="true"> <shape android:shape="rectangle"> <corners android:radius="5dp" /> <solid android:color="#50000000"/> </shape> </item> <item > <shape android:shape="rectangle"> <corners android:radius="5dp" /> <solid android:color="#00000000"/> </shape> </item></selector>
Step 2: Set the selector for src of ImageView.
<ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:contentDescription="@null" android:scaleType="centerCrop" android:src="@drawable/share_image_selector" />
Then, the image resource to be presented on our ImageVIew is
mImageView.setBackgroundResource(R.drawable.icon);
That is to say, we set backgroundResource for imageview, and then set selector for src. Visually, our selector is displayed above ImageView. Of course, when we click ImageView, the selector is triggered, at this time, there will be a press effect.
Therefore, a good app can be easily used by users in terms of experience, because I forgot which Androider blog saw it, because I found this effect record while checking the code, I would like to share with you, thanks to the Androider.