How to highlight ImageView when focused or clicked?

前端 未结 10 1382
余生分开走
余生分开走 2020-12-02 08:34

A good example of this is either on the Twitter launch screen (the screen with the large icons that is seen when the application is first launch) or even just look at the ap

相关标签:
10条回答
  • 2020-12-02 09:32

    Only to complete Josh Clemm answer. You can also maintain the same image defined by src, but change or highlight only the background. This would more or less like this:

    logo_box.xml

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_pressed="true" android:drawable="@drawable/background_normal"/>
        <item android:state_pressed="false" android:drawable="@drawable/background_pressed"/>
    </selector>
    

    And then defining the background of your button as logo_box:

    <ImageView
        android:contentDescription="@string/description_logo"
        android:src="@drawable/logo"
        android:background="@drawable/logo_box" />
    

    Where background_normal and background_pressed can be as complex as you want, or as simple as a @color :)

    0 讨论(0)
  • 2020-12-02 09:36

    Use selectableItemBackground as a background:

    android:background="?android:attr/selectableItemBackground"
    
    0 讨论(0)
  • 2020-12-02 09:38

    If you don't have another drawable for the pressed state you can use setColorFilterto achieve a simple tint effect.

    It behaves just like pressed state selector so when the image is pressed it changes the background to light grey color.

    final ImageView image = (ImageView) findViewById(R.id.my_image);
    image.setOnTouchListener(new View.OnTouchListener() {
            private Rect rect;
    
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if(event.getAction() == MotionEvent.ACTION_DOWN){
                    image.setColorFilter(Color.argb(50, 0, 0, 0));
                    rect = new Rect(v.getLeft(), v.getTop(), v.getRight(), v.getBottom());
                }
                if(event.getAction() == MotionEvent.ACTION_UP){
                    image.setColorFilter(Color.argb(0, 0, 0, 0));
                }
                if(event.getAction() == MotionEvent.ACTION_MOVE){
                    if(!rect.contains(v.getLeft() + (int) event.getX(), v.getTop() + (int) event.getY())){
                        image.setColorFilter(Color.argb(0, 0, 0, 0));
                    } 
                }
                return false;
            }
        });
    

    It handles moving finger outside the view boundaries, thus if it occurs, it restores a default background.

    It's important to return false from onTouch method when you want to support onClickListner too.

    0 讨论(0)
  • 2020-12-02 09:38

    I noticed that a drawable xml is not enough:

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

    An ImageView doesn't press. You should also assign an OnClickListener for an ImageView. Then it will press as a button.

    0 讨论(0)
提交回复
热议问题