Change source image for image view when pressed

前端 未结 16 517
闹比i
闹比i 2020-12-08 19:53

I have a scroll view with lots of image buttons. I want to change the image for an image button when it\'s pressed. The thing is that I want the image to remain until anothe

相关标签:
16条回答
  • 2020-12-08 20:48
    float alpha_first = 0.2f;
    float alpha_second = 1.0f;
        AlphaAnimation alphadp = new AlphaAnimation(alpha_second, alpha_first);
            switch (v.getId()) {
            case R.id.disable_deactivate_pic:
            ImageButton disable_button =(ImageButton)findViewById(R.id.disable_deactivate_pic);
                    if (!flag) {
                        disable_button.setImageResource(R.drawable.enable_active);
    
                        linearLayout_for_picture = (LinearLayout) findViewById(R.id.linearlayout_imageView_pic);
    
                    alphadp.setFillAfter(true);
                    linearLayout_for_picture.startAnimation(alphadp);
                    flag=true;
                    }
            else {
                        disable_button.setImageResource(R.drawable.disable_active);
                        alphadp.setFillAfter(false);
                        linearLayout_for_picture.startAnimation(alphadp);
                        flag=false;
                    }
    
                break;
    
    0 讨论(0)
  • 2020-12-08 20:48

    You can use a StateListDrawable to achieve this. This method also works for ImageButtons. I prefer it to setting additional listeners.

    I've also compiled a class of additional helpers: http://alexanderwong.me/post/40799636705/android-change-background-image-drawable-on-press

        public static StateListDrawable makeStateDrawable(Drawable drawable, Drawable pressedDrawable, Drawable disabledDrawable) {
        boolean set = false;
        StateListDrawable stateDrawable = new StateListDrawable();
        if (disabledDrawable != null) {
            set = true;
            stateDrawable.addState(new int[] { -android.R.attr.state_enabled }, disabledDrawable);
        }
        if (pressedDrawable != null) {
            set = true;
            stateDrawable.addState(new int[] { android.R.attr.state_pressed }, pressedDrawable);
        }
        if (drawable != null) {
            set = true;
            stateDrawable.addState(new int[0], drawable);
        }
        return set ? stateDrawable : null;
    } 
    
    0 讨论(0)
  • 2020-12-08 20:51

    the OnTouchListener is much better for what you have to do:

    myImageButton.setOnTouchListener(new OnTouchListener(){
    
                    public boolean onTouch(View v, MotionEvent event) {
                        switch(event.getAction())
                        {
                        case MotionEvent.ACTION_DOWN :
    myImageButton.setImageResource(R.drawable.image_when_pressed);
                            break;
                        case MotionEvent.ACTION_UP :
    myImageButton.setImageResource(R.drawable.image_when_released);
                            break;
                        }
                        return false;
                    }
    
                });
    
    0 讨论(0)
  • 2020-12-08 20:51

    state_pressed or state_activated did not work for me.

    However, I succeeded with state_enabled

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_enabled="true" android:drawable="@drawable/checkbox_on" />
        <item android:state_enabled="false" android:drawable="@drawable/checkbox_off" />
    </selector>
    

    Your xml should declare it as src:

     <ImageView
            android:id="@+id/img_checkmark"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/checkbox_selector_filter"/>
    

    Then in code, ensure you enable or disable it based on event/state:

    imageCheckBox.isEnabled = true

    This was easiest and cleanest for me

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