在Android的圆形按钮..避免presses"之外"按钮? [英] Round button in android.. avoid presses "outside" the button?

查看:123
本文介绍了在Android的圆形按钮..避免presses"之外"按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建/试图通过ImageButton的小工具来创建一个圆形按钮为Android。 但是,由于这种类型的按钮将被视为一个正方形和我的PNG图像也被视为带有​​透明背景的正方形,那我怎么避免用户从beeing能够preSS的圆形按钮外?..原因作为现在..他们可以preSS在按钮的角,并且将仍然触发点击事件.. 是否有任何speciall映射层,可以在Photoshop或任何方式进行更改图像按钮的半径,使其适合于我的形象的圆......或任何想法?

I have created/tried to create, a round button for android by using the ImageButton "widget". But since that type of button is treated as a square and my png-image also is treated as a square with a transparent background, then how do I avoid the user from beeing able to press outside the round button?.. cause as for now.. they can press in the "corners" of the button and that will still trigger the click-event.. Is there any speciall mapping-layer that can be done in photoshop or any way to change the radius of the image button so it fits to the "roundness" of my image.. or any ideas?

在此先感谢!..和抱歉的英文不好。

Thanks in advance!.. and sorry for poor english..

推荐答案

尝试勾股定理和onTouch,简单易用的方式来做到这一点。

Try Pythagorean theorem and onTouch, simple and easy way to do it.

public boolean inCircle(MotionEvent e, int radius, int x, int y) {
    int dx = e.x - x;
    int dy = e.y - y;
    double d = Math.sqrt((dx * dx) + (dy * dy));
    if(d < radius)
        return true;
    return false;
}

在x,y是圆的posision,半径为半径,而e是的TouchEvent你有

the x, y is the posision of the circle, the radius is the radius, and the e is the TouchEvent you have.

@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
    if(arg1.getAction() == MotionEvent.ACTION_DOWN){
           if(inCircle(arg1, radius, xCircle, yCircle){
                  //do whatever you wanna do here
                  }
            }
    return false;
}

这篇关于在Android的圆形按钮..避免presses&QUOT;之外&QUOT;按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆