【问题标题】:Round button in android.. avoid presses "outside" the button?android中的圆形按钮..避免按下按钮“外部”?
【发布时间】:2012-02-21 10:05:35
【问题描述】:

我已经创建/尝试创建一个圆形按钮,使用 ImageButton“小部件”。 但是由于这种类型的按钮被视为正方形,而我的 png-image 也被视为具有透明背景的正方形,那么如何避免用户在圆形按钮外按下?...原因就目前而言..他们可以按下按钮的“角落”,仍然会触发点击事件.. 是否有任何特殊的映射层可以在 Photoshop 中完成,或者以任何方式更改图像按钮的半径,使其适合我的图像的“圆度”......或任何想法?

提前谢谢!..抱歉英语不好..

【问题讨论】:

    标签: android


    【解决方案1】:

    我使用 ImageView 作为我的圆形按钮,我需要对 @Daniel 的代码进行一些更改以使其按我想要的方式工作。这是我的代码:

    private boolean mStillDown = false;
    
    public boolean inCircle(MotionEvent e, float radius, float x, float y) {
        float dx = e.getX() - x;
        float dy = e.getY() - y;
        double d = Math.sqrt((dx * dx) + (dy * dy));
        if(d < radius)
            return true;
        return false;
    }
    
    @Override
    public boolean onTouchEvent(MotionEvent event) {
    
        int action = event.getAction();
        boolean inCircle = inCircle(event, getWidth()/2.0f, getWidth()/2.0f, getHeight()/2.0f);
    
        if(inCircle){
            if(action == MotionEvent.ACTION_DOWN || action == MotionEvent.ACTION_POINTER_DOWN){
                this.setPressed(true);
                mStillDown = true;
            }else if(action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_POINTER_UP){
                if(this.isPressed()){
                    this.performClick();
                    this.setPressed(false);
                    mStillDown = false;
                }
            }else if(action == MotionEvent.ACTION_MOVE && mStillDown){
                this.setPressed(true);
            }
        }else{
            if(action == MotionEvent.ACTION_MOVE){
                this.setPressed(false);
            }else if(action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_POINTER_UP || action == MotionEvent.ACTION_OUTSIDE){
                mStillDown = false;
            }
        }
    
        return true;
    }
    

    希望这对某人有用。

    【讨论】:

      【解决方案2】:

      试试勾股定理和 onTouch,简单易行的方法。

      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是圆的位置,radius是半径,e是你有的TouchEvent。

      @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;
      }
      

      【讨论】:

      • 是使用dp还是像素的单位?..按位置..是使用圆的中间位置..还是左上角的位置?...
      • 试过这个..但它似乎是从左上角开始计数,并且不会移动要计数的圆圈的“枢轴”..
      • 从中间数,如果你有正方形,这样做:
      • InCircle(arg1, imageaView.getWidth(), imageview.getWidh()/2, imageWiew.getHeight()/2.
      • 是的,非常感谢 :)..最后我可能会用到我的数学知识.. 一直在做很多 ASP.net.. 好吧.. 真的没有多少数学.. 哪个有点好..但偶尔自己思考一下也很有趣:)..非常感谢您的帮助:)
      猜你喜欢
      • 1970-01-01
      • 2012-03-09
      • 2019-11-13
      • 2012-02-28
      • 2012-08-28
      • 1970-01-01
      相关资源
      最近更新 更多