How can I give an imageview click effect like a button on Android?

前端 未结 30 2080
情歌与酒
情歌与酒 2020-11-28 01:50

I have imageview in my Android app that I am using like a button with the onClick event given, but as you might guess it is not giving imageview a clickable effect when clic

相关标签:
30条回答
  • 2020-11-28 02:07

    EDIT: Although the original answer below works and is easy to set up, refer to this post by an Android Developer Advocate at Google if you want / need a more efficient implementation. Also note that the android:foreground attribute is coming to all Views, including ImageView, by default in Android M.


    The problem with using a selector for an ImageView is that you can only set it as the view's background - as long as your image is opaque, you will not see the selector's effect behind it.

    The trick is to wrap your ImageView in a FrameLayout with the attribute android:foreground which allows us to define an overlay for its content. If we set android:foregroundto a selector (e.g.?android:attr/selectableItemBackground for API level 11+) and attach the OnClickListener to the FrameLayout instead of the ImageView, the image will be overlaid with our selector's drawable - the click effect we desire!

    Behold:

    <FrameLayout
        android:id="@+id/imageButton"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:foreground="?android:attr/selectableItemBackground" >
    
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:src="@drawable/yourImageFile" />
    
    </FrameLayout>
    

    (Note this should be placed within your parent layout.)

    final View imageButton = findViewById(R.id.imageButton);
    imageButton.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View view) {
            // do whatever we wish!
        }
    });
    
    0 讨论(0)
  • 2020-11-28 02:07

    I do some similar things See suitable for you or not

    View Press Effect Helper:

    • usage : do some simple press effect like iOS

      Simple Usage:

    • ImageView img = (ImageView) findViewById(R.id.img);

    • ViewPressEffectHelper.attach(img)

    https://gist.github.com/extralam/7489370

    0 讨论(0)
  • 2020-11-28 02:09

    OR:

    You can use this form, with Image Button.

    Create file res/drawable/btn_video.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:drawable="@drawable/image"
            android:state_pressed="true" />
        <item android:drawable="@drawable/ico2"
            android:state_focused="true" />
        <item android:drawable="@drawable/ico2" />
    </selector>
    

    And res/layout/activity_main.xml:

    <ImageButton
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/imageButton"
        android:layout_gravity="center_horizontal"
        android:onClick="eventImageBtn"
        android:background="@drawable/btn_video"
        android:adjustViewBounds="true"
        android:scaleType="fitXY"
    />
    

    Your image change with a click, and you can adjust with a linear layout:

    <?xml version="1.0" encoding="utf-8"?>
    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:background="@color/menu_item_background">
    
            <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"
                          android:paddingLeft="@dimen/main_screen_side_padding" android:paddingRight="@dimen/main_screen_side_padding" android:paddingTop="@dimen/main_screen_side_padding" android:paddingBottom="@dimen/main_screen_side_padding"
                          android:background="#ffb3ff13" android:weightSum="10.00">
    
    
                <LinearLayout android:layout_weight="2.50" android:background="#ff56cfcd" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="0dp" >
    
                    <ImageButton
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:id="@+id/imageButton"
                        android:layout_gravity="center_horizontal"
                        android:onClick="eventImageBtn"
                        android:background="@drawable/btn_video"
                        android:adjustViewBounds="true"
                        android:scaleType="fitXY"
                    />
                </LinearLayout>
    
                <LinearLayout android:layout_weight="0.50" android:layout_height="0dp" android:background="#ffffffff" android:orientation="vertical" android:layout_width="fill_parent" >
                </LinearLayout>
    
                <LinearLayout android:layout_weight="4.50" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="0dp" android:background="#ff8aa5ff">
                </LinearLayout>
    
                <LinearLayout android:layout_weight="0.50" android:layout_height="0dp" android:background="#ffffffff" android:orientation="vertical" android:layout_width="fill_parent" >
                </LinearLayout>
    
                <LinearLayout android:layout_weight="2.00" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="0dp" android:background="#ffff7d1a" >
                </LinearLayout>
    
            </LinearLayout>
        </LinearLayout>
    </ScrollView>
    
    0 讨论(0)
  • 2020-11-28 02:11

    Use style="?android:borderlessButtonStyle" in the XML file. It will show the Android default click effect.

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" 
        style="?android:borderlessButtonStyle"
    />
    
    0 讨论(0)
  • 2020-11-28 02:12

    I think ImageButton is a better solution

    <ImageButton
        android:layout_width="96dp"
        android:layout_height="56dp"
        android:src="@mipmap/ic_launcher"
        android:adjustViewBounds="true"
        android:background="@android:color/transparent"
        android:foreground="@drawable/selector" />
    
    0 讨论(0)
  • 2020-11-28 02:13

    This is the best solution I ever seen. Its more generic.

    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android"
         android:fillAfter="true" >
    
        <alpha
            android:duration="100"
            android:fromAlpha="0.0"
            android:interpolator="@android:anim/accelerate_interpolator"
            android:toAlpha="1.0" />
    </set>
    
    0 讨论(0)
提交回复
热议问题