The selector Effect of ImageView.

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.