【问题标题】:add a circle at the edge of arc android?在弧形android的边缘添加一个圆圈?
【发布时间】:2015-04-08 13:32:22
【问题描述】:

如何在圆弧边缘添加小圆。
并且它也应该在时钟方向上以弧形边缘移动。
现在我通过改变扫角成功地为弧设置动画。
并留下黑点。

下面是getView和动画类的代码

--- init method and implement constructor ----

 mRectF = new RectF(mWidth / 2 - 360, mHeight / 2 - 360, mWidth / 2 + 360, mHeight / 2 + 360);

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    //draw circle background
    mPaint.setColor(getResources().getColor(R.color.timer_background_color));
    canvas.drawCircle(mWidth / 2, mHeight / 2, 360, mPaint);

    mPaint.setColor(getResources().getColor(R.color.actionbar_back_color));
    canvas.drawArc(mRectF, mStartAnagle, mSweepAngle, false, mPaint);
}

public class TimerAnimation extends Animation{

    public TimerAnimation (float startAngle, float sweepAngle, long duration) {
        mStartAnagle = startAngle;
        mSweepAngle = sweepAngle;
        setDuration(duration);
        setRepeatCount(Animation.INFINITE);
        setInterpolator(new LinearInterpolator());
    }

    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {
        if (!isComplete) {
            mSweepAngle = mSweepAngle + 6;
            if (mSweepAngle >= 360) {
                isComplete = true;
                mSweepAngle = 360;
            }
        } else {
            mStartAnagle = mStartAnagle + 6;
            mSweepAngle = mSweepAngle - 6;
            if (mStartAnagle >= 360)
                mStartAnagle = 0;
            if (mStartAnagle == 270 || mSweepAngle <= 0) {
                isComplete = false;
                mSweepAngle = 0;
            }
        }
        invalidate();
    }
}

【问题讨论】:

  • 你能把你是如何实现的,完整的代码发给我吗
  • 我想看到 3 个弧线(就像三个生长阶段),当我在弧线逆时针上滑动时,弧线的颜色应该会改变,我应该能够从一个弧线跳到另一个弧线。有什么解决办法吗?

标签: android math view android-custom-view geometry


【解决方案1】:

也许你应该使用Path

Path path = new Path();
// Set the starting position of the path to (0,0).
path.moveTo(0, 0);
path.arcTo(...); //draw your arc here
path.circleTo(); //draw a small circle here at the end of arc

也许你应该calc the arc's end position 并用作小圆圈的中心。

【讨论】:

    【解决方案2】:

    第一步:计算黑点的位置

    建议中心位置为(centerX,centerY),黑点位置为(x,y),则,

    x = radius * cos(mStartAnagle+mSweepAngle) + centerX;
    y = radius * sin(mStartAnagle+mSweepAngle) + centerY;
    

    第 2 步:画黑点

    建议点图为R.drawable.dot

    Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.dot) 
                                 .copy(Bitmap.Config.ARGB_8888, true);
    canvas.drawBitmap(bitmap, x, y, null);
    

    【讨论】:

      猜你喜欢
      • 2023-03-29
      • 2015-12-18
      • 1970-01-01
      • 1970-01-01
      • 2017-09-06
      • 1970-01-01
      • 2018-12-15
      • 1970-01-01
      • 2013-06-07
      相关资源
      最近更新 更多