How to highlight ImageView when focused or clicked?

前端 未结 10 1423
余生分开走
余生分开走 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:19

    My solution, custom attribute for ImageView :
    https://github.com/henrychuangtw/Android-ImageView-hover

    Step 1 : declare-styleable

    
        
    
    


    Step 2 : custom ImageView

    public class MyImageView extends ImageView {
    
    int resID, resID_hover;
    
    public MyImageView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }
    public MyImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.MyImageViewAttr);
        resID_hover = array.getResourceId(R.styleable.MyImageViewAttr_hover_res, -1);
        if(resID_hover != -1){
            int[] attrsArray = new int[] {
                    android.R.attr.src 
                };
    
            TypedArray ta = context.obtainStyledAttributes(attrs, attrsArray);
            resID = ta.getResourceId(0 , View.NO_ID);           
            ta.recycle();
    
            setOnTouchListener(listener_onTouch);
        }
    
        array.recycle();
    
    }
    public MyImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.MyImageViewAttr);
        resID_hover = array.getResourceId(R.styleable.MyImageViewAttr_hover_res, -1);
        if(resID_hover != -1){
            int[] attrsArray = new int[] {
                    android.R.attr.src 
                };
    
            TypedArray ta = context.obtainStyledAttributes(attrs, attrsArray);
            resID = ta.getResourceId(0 , View.NO_ID);           
            ta.recycle();
    
            setOnTouchListener(listener_onTouch);
        }
    
        array.recycle();
    }
    
    
    
    OnTouchListener listener_onTouch = new OnTouchListener() {
    
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
    
            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                setImageResource(resID_hover);
                break;
    
            case MotionEvent.ACTION_MOVE:
    
                break;
    
            case MotionEvent.ACTION_UP:
                setImageResource(resID);
                break;
    
            default:
                break;
            }
    
    
            return false;
        }
    };
    

    }


    Step 3 : declare myattr : xmlns:myattr="http://schemas.android.com/apk/res-auto" in layout xml

    
    


    Step 4 : set myattr:hover_res for MyImageView

    
    


提交回复
热议问题