简体   繁体   中英

Java: How to overdraw existing drawable in ImageView with another drawable?

I have Activity where I have text view which looks like this: 1/9 (holeCounter/holeNm). I have 2 image views which manipulate holeCounter (add or reduce the number) in ImageView, arrow left image represents reducing and arrow right represent adding the number. Now when my holeCounter == holeNm (9/9) this arrow right image should turn into another image.

note: Those arrow images are created in that Activitys XML file like:

android:background="@drawable/ic_arrow_left"
android:background="@drawable/ic_arrow_right"

What I have right now:

private void setButtons() {
        mBackArrow = findViewById(R.id.previous);
        mForwardArrow = findViewById(R.id.next);

        mBackArrow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (holeCounter > 1) {
                    Intent intent = getIntent();
                    mHoleNm = findViewById(R.id.gameHoleNumber);

                    holeCounter--;
                    mHoleNm.setText(holeCounter + "/" + intent.getStringExtra("HOLESNM"));
                }
            }
        });

        mForwardArrow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = getIntent();
                int holesNm = Integer.parseInt(intent.getStringExtra("HOLESNM"));

                if (holeCounter == (holesNm - 1)) {
                    mForwardArrow.setImageResource(R.drawable.ic_finish);
                }

                if (holeCounter < holesNm) {
                    mHoleNm = findViewById(R.id.gameHoleNumber);
                    holeCounter++;
                    mHoleNm.setText(holeCounter + "/" + intent.getStringExtra("HOLESNM"));
                }
            }
        });
    }

Now it adds the new drawable on top of that arrow right, but ofc I want that arrow right to disappear underneath that new image. Also after that, if the user click left arrow and now holeCounter != holeNm that other image should turn back to Arrow right image.

You should had to use android:src instead android:background . replace below lines of code

android:background="@drawable/ic_arrow_left"
android:background="@drawable/ic_arrow_right"

TO

android:src="@drawable/ic_arrow_left"
android:src="@drawable/ic_arrow_right"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM