如何在另一个圈子里面保持一个圆圈的android视图控件

我想在android中创建一个类似pad的视图.我得到一个跟随用户手势的圆圈,我使用距离来保持圆圈越过垫控制的主圆.
我的问题是我希望圈子继续跟随手势,但要留在主圈内.我使用公式来找到使用角度和半径的点,但它做了一些时髦的东西.

我正在翻译画布,因此主圆的中心位于0,0.
这是代码:

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.translate(this.mainRadius, this.mainRadius);

    canvas.drawCircle(0, 0, this.mainRadius, this.debugPaint);
    canvas.drawCircle(this.handleX, this.handleY, this.handleRadius, this.handlePaint);
}

private void translateHandle(MotionEvent event) {
    int x = (int) (event.getX() - this.mainRadius);
    int y = (int) (event.getY() - this.mainRadius);
    double distance = distanceFromCenter(x, y);
    if (distance <= this.maxDistance) {
        this.handleX = x;
        this.handleY = y;
    } else {
        float angle = (float) Math.toDegrees(Math.atan2(y, x));
        if (angle < 0)
            angle += 360;

        this.handleX = (int) ((this.mainRadius - this.handleRadius) * Math.cos(angle));
        this.handleY = (int) ((this.mainRadius - this.handleRadius) * Math.sin(angle));
    }

    //onTranslateHandle(distance);
}

这是gif图像中的时髦东西:

解决方法:

我无法在您的代码段中验证此更改,但希望它能让您知道如何进一步继续;

private void translateHandle(MotionEvent event) {
    float x = event.getX() - this.mainRadius;
    float y = event.getY() - this.mainRadius;
    double distance = distanceFromCenter(x, y);
    if (distance > this.maxDistance) {
        // If distance is i.e 2.0 and maxDistance is 1.0 ==> adjust is 0.5
        // which repositions x and y making distance 1.0 maintaining direction
        double adjust = this.maxDistance / distance;
        x = (float)(x * adjust);
        y = (float)(y * adjust);
    }
    this.handleX = (int)x;
    this.handleY = (int)y;
}

如果不能提供任何有用的结果,我可以根据需要更新答案.

上一篇:android – ListView项目不可点击.为什么?


下一篇:php – Zend_Auth:为什么验证对象名为adapter而不是策略?