Change source image for image view when pressed

前端 未结 16 515
闹比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:29

    if u have already a button in ur app that will do the swap between the two pics u have when its clicked then there is a simple Code :D

    // to add Images in android we simply " Copy them from their location and past in (res --> drawable)"

    // then drag and drop "ImageView" and select the image u want to display

    // u can adjust the scale throw "scaleType , layout_Width & layout_Height"

    public boolean swap = true;
    
    public  void change(View view)
    {
        ImageView i = (ImageView) findViewById(R.id.img);
        if (swap)
        {
            i.setImageResource(R.drawable.images);
            swap=false;
        }
        else
        {
            swap=true;
            i.setImageResource(R.drawable.couple);
        }
    
    }
    
    0 讨论(0)
  • 2020-12-08 20:34

    Better solution, use the following xml as source of the image:

       <?xml version="1.0" encoding="utf-8"?>
        <selector xmlns:android="http://schemas.android.com/apk/res/android" >
            <item android:state_activated="true">
                <bitmap android:src="@drawable/image_selected"/>
            </item>
            <item>
                <bitmap android:src="@drawable/image_not_selected"/>
            </item>
        </selector>
    
        @Override
         public void onClick(View v) {
            v.setActivated(!v.isActivated());
         }
    
    0 讨论(0)
  • 2020-12-08 20:34

    If you save the selection on click and then reload the image scroll gallery (or scroll menu) and then reload and scroll to the selection with a replaced image, then you might be able to do it. As far as I know none of the inbuilt gallery or menu functions have the ability to replace images once they are loaded and displayed.

    0 讨论(0)
  • 2020-12-08 20:35

    What worked for me was:

    I created a new drawable xml file, for eg, image_state.xml

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

    And in my layout file, I set the src of the imageView as:

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/image_state"/>
    
    0 讨论(0)
  • 2020-12-08 20:37

    don't forget to create the field "fun"

    Try this for changing image.When imageview is pressed

    Like_btn.setOnClickListener(new OnClickListener()
            {
    
                **private boolean fun = true;**
    
    
                public void onClick(View v)
                {
                    if(fun)
                    {
                    Like_btn.setImageResource(R.drawable.unlike);
                    fun=false;
                    }
                    else
                    {
                        fun=true;       
                        Like_btn.setImageResource(R.drawable.like);
                        Toast.makeText(getApplicationContext(), "Changed", Toast.LENGTH_LONG).show();
                    }
                }
            });
    
    0 讨论(0)
  • 2020-12-08 20:40

    You want to do this.

    ImageButton Demo_button = (ImageButton)findViewById(R.id.firstimage);
    
    // when you click this demo button
    Demo_button.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            Demo_button.setImageResource(R.drawable.secondimage);
        }
    }
    

    Try this. (updated setset to set)

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