两个按钮更换同一张图片要按两次的问题

偶然发现了安卓的这个坑,使用两个Button绑定actiondown和actionup更换同一个ImageView的图片要按两次才行,唯一的解决办法是设置imageview的显示图片水平

 

<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>

drawable目录下建这个文件作为imageview的src

 

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;


            }

        });

    }
}

 

上一篇:android – catch“RuntimeException:Canvas:试图画得太大……”


下一篇:Objective-C ,ios,iphone开发基础:快速实现一个简单的图片查看器