Define the layout perimeters manually,means i want

2020-07-23 04:47发布

I am new to android. I want to create my own custom buttons in android;The methods which I have used to create the custom buttons are; 1. Draw a simple image whatever you want in paint and then go to drawable resources then paste that image.From there, we can use those images as buttons. Suppose my image is like this,enter image description here

I made the rest of the portion of this image excluding green TRANSPARENT so that i will make only the green area touchable,for this I am using this code,

 Bitmap TheBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.green);
        int eventPadTouch = event.getAction();
        int iX = (int) event.getX();
        int iY = (int) event.getY();

        int[] location = new int[2];
        v.getLocationOnScreen(location);

        int viewX = location[0];
        int viewY = location[1];


        switch (eventPadTouch) {
            case MotionEvent.ACTION_DOWN:
                if (iX>=viewX & iY>=viewY & iX<=(viewX+TheBitmap.getWidth()) & iY<=(viewY+TheBitmap.getHeight())) {                 
                    if (TheBitmap.getPixel(iX,iY)!=0) {

                        Intent intent = new Intent(this, NewActivity.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(intent);

                        showPressedState();
                        return false; 
                    } 
                }
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                showNormalState();
                break;
        }           
        return true;
    }

But when I am making this image like this, enter image description here I am facing problem that THE below the curve portion in the image it is touchable again but i made it non touchable. and when I move to the next problem that if I want to create this kind of buttonenter image description here I want to make all the portions to be used as buttons so that i can perform different activities on that,is it possible?

I found that this all is happening because android provides only limited types of layouts like LINEAR LAYOUT,RELATIVE LAYOUT,WEBVIEW. So ,is there any way to define our own layout means if i will be able to give the boudries of the layout.

1条回答
神经病院院长
2楼-- · 2020-07-23 05:23

My idea is, that you manually check what is the color of pixel where you pressed, and based on that, you can switch different touch events (different color cases). I don't think there is easier way to achieve what you want to achieve.

But what if your image consist of gradient, or same colors in different clickable areas? Consider the following example:

enter image description here

As you can see there is a picture with 4 different color regions, but there is also a lot of white pixels in each region right? If a button background is not a solid color, but for example consist of gradients etc, then you could make each of them having different transparency value (255,254,253 etc.) which wouldn't be noticeable by the user but which you could easily use to distinguish between area where you pressed.

So inside onClick method:

//Not sure about below line
alpha = ((bitmap.getPixel((int)event.getX(), (int)event.getY())) >>> 24);

switch(alpha) 
{
    case 255:
        //your code
    break;
    case 254:
        //your code
    break;
    default:
        //your code
    break;
}

Also, you might consider reading this:

查看更多
登录 后发表回答