一个图像按钮中有许多点击器

问题描述 投票:0回答:1

我有这张照片(这是一张照片,而不是四张)。我想使其成为一个图像按钮。但是,我想将其分为四个部分,所以当我单击Paul时,它将为我打开有关Paul的新意向,对John,Ringo和George依此类推。我能做到吗?怎么样?enter image description here

android android-imagebutton
1个回答
0
投票

您可以使用View.OnTouchListener从发生的点击中获取坐标。

View.OnTouchListener onTouchListener = new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction() & MotionEvent.ACTION_MASK) {
                case MotionEvent.ACTION_UP:
                    float x = event.getX();

                    int width = v.getWidth();
                    int oneFourthWidth = width / 4;
                    if (x <= oneFourthWidth) {
                        // John Lennon
                    } else if (x > oneFourthWidth && x <= oneFourthWidth * 2) {
                        // Paul McCartney
                    } else if (x > oneFourthWidth * 2 && x <= oneFourthWidth * 3) {
                        // George Harrison
                    } else if (x > oneFourthWidth * 3) {
                        // Ringo Starr
                    }
                    break;
            }
            return true;
        }
    };
yourView.setOnTouchListener(onTouchListener);

每个图像的精确坐标的计算取决于具体情况。

© www.soinside.com 2019 - 2024. All rights reserved.