本文介绍了手动定义布局周边,意味着我想用我的价值观来定义布局的边界值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的机器人。我想创建自己的自定义按钮,Android的,这是我用于创建自定义按钮的方法;1.画出任何你想要的油漆一个简单的图像,然后去绘制资源,然后粘贴image.From那里,我们可以使用这些图片的按钮。假设我的形象是这样的,

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,

我做了这个不计形象的部分,其余的绿色透明的,因此,我将只作绿化面积触摸,对于这个我用这code,

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,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 buttonI want to make all the portions to be used as buttons so that i can perform different activities on that,is it possible?

我发现,这一切正在发生的事情,因为Android的只能提供有限的类型布局像线性布局,相对布局,web视图。那么,有没有什么办法可以定义自己的布局意味着,如果我将能够给布局的boudries。

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.

推荐答案

我的想法是,您手动检查什么是像素,在那里你pressed,并基于该颜色,你可以切换不同的触摸事件(不同颜色的情况下)。我不认为有更简单的方法来达到你想要达到什么目的。

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:

正如你可以看到有4种不同的颜色区域的图像,但也有大量的白色像素,每个地区对不对?如果按钮背景不是纯色,但例如由梯度等,那么你可以让他们每个人具有不同透明度值(255254253等),就不再T为用户显着的,但你可以很容易地使用区,让您pressed区别开来。

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.

那么的onClick方法中:

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:

这篇关于手动定义布局周边,意味着我想用我的价值观来定义布局的边界值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 11:32