Replace the two button issues of the same picture twice Yaoan

Andrews stumbled upon this pit, two Button binding actiondown and actionup be replaced with a picture of Yao An ImageView twice for the job, the only solution is to set imageview display picture level

 

<level-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:maxLevel="1"
        android:drawable="@drawable/btn1" />
    <item
        android:maxLevel="2"
        android:drawable="@drawable/btn2" />
    <item
        android:maxLevel="3"
        android:drawable="@drawable/btn3" />
</level-list>

This file is built under the src drawable directory as imageview

 

public class MainActivity extends AppCompatActivity {
    ImageView imageView;
    Button button;
    Button button2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imageView = findViewById(R.id.image3);
        button = findViewById(R.id.button1);
        button2 = findViewById(R.id.button2);
//
        button.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (MotionEvent.ACTION_DOWN == event.getAction()) {

                    imageView.setImageLevel(2);
                    System.out.println("按下1");
                } else if (MotionEvent.ACTION_UP == event.getAction()) {
                    imageView.setImageLevel(1);


                }

                return true;
            }

        });
        button2.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {


                if (MotionEvent.ACTION_DOWN == event.getAction()) {

                    imageView.setImageLevel(3);
                    System.out.println("按下1");
                } else if (MotionEvent.ACTION_UP == event.getAction()) {
                    imageView.setImageLevel(1);


                }
                return true;


            }

        });

    }
}

 

Guess you like

Origin www.cnblogs.com/Ocean123123/p/11233718.html