【问题标题】:Best way to draw on canvas Android在 Android 画布上绘制的最佳方式
【发布时间】:2014-04-02 08:53:10
【问题描述】:

在画布上绘制下图的最佳方式是,尾巴应该独立于其身体移动

下面是我尝试绘制的代码:

RectF rectf = new RectF(left, top, right, bottom);                            
path.arcTo(rectf, startAngle, sweepAngle);
path.lineTo(linex, liney);

解释:

我正在一个矩形内画一个圆。我没有画一个完整的圆,我从 0 度到 350 度开始,然后我从弧停止的地方画一条线。

图像变得完美。问题在于旋转,当我尝试旋转整个画布时,我只希望尾部旋转。

对于旋转,我使用以下代码:

//This line is being called inside onDraw()
canvas.rotate(rotation, (left+right)/2, (top+bottom)/2);

我基本上是在尝试旋转气泡 onTouch 事件

View.OnTouchListener touch = new View.OnTouchListener() {


    @Override
    public boolean onTouch(View v, MotionEvent e) {
        if(e.getAction() == MotionEvent.ACTION_MOVE){
            float x = e.getX();
            float y = e.getY();
            updateRotation(x,y);
            invalidate();

        }
        return true;
    }
};


public void updateRotation(float x, float y) {

        double r = Math.atan2(x - (getWidth()/2),  (getHeight()/2) - y);
        rotation = (int) Math.toDegrees(r);

   }

我完整的 onDraw() 方法如下:

    @Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    path.reset();

    left = 50;
    top = 50;
    right = getWidth()- 50;
    bottom = getHeight()-50;
    startAngle = 100;
    sweepAngle = 335;
    linex = (left + right)/2;
    liney = bottom + 50;

    canvas.rotate(rotation, (left+right)/2, (top+bottom)/2);

    RectF rectf = new RectF(left, top, right, bottom);
    path.arcTo(rectf, startAngle, sweepAngle);
    path.lineTo(linex, liney);

    canvas.clipPath(path);
    canvas.drawPath(path, paint);

    canvas.restore();

}

旋转前的图像:

旋转后的图像:

【问题讨论】:

  • 旋转后添加另一张图片
  • @pskink 我已经在我的问题中添加了图片
  • 所以这是一个带有枢轴的“正常”旋转,那有什么问题呢?
  • 您可以在图像中看到整个画布正在旋转。我只希望它的尾巴(v 形部分)旋转(尾巴应该沿着图形的边缘移动。我希望你理解我想要实现的目标)
  • 出于兴趣,您可以将它们用作两个单独的项目吗?气泡始终被绘制,并且取决于用户按下的时间/位置,您会绘制底部位吗?

标签: android canvas android-canvas


【解决方案1】:

我在这方面花了很多时间,但它对我的应用很有用。

我的想法是设置点并围绕它绘制,所以你所要做的就是改变 linex 和 liney

你还过得怎么样?

protected void onDraw(Canvas canvas) {
    // TODO Auto-generated method stub
    super.onDraw(canvas);
    paint.setColor(mContext.getResources().getColor(R.color.black));

    Path path = new Path();
    int left = 50;
    int top = 50;
    int right = getWidth()- 50;
    int bottom = getHeight()-50;
    int linex = getWidth()/2;
    int liney = getHeight();
    int openSize = 20;


    RectF rectf = new RectF(left, top, right, bottom);
    int angle = (int) angle(new Point(getWidth()/2, getHeight()/2), new Point(linex,liney)); 

    int startAngle = angle + (openSize/2);
    int sweepAngle = 360 - openSize;

    path.arcTo(rectf, startAngle, sweepAngle);
    path.lineTo(linex, liney);
    path.close();
    canvas.drawPath(path, paint);

}

private double angle(Point point1, Point point2) {
    double deltaX = (point1.x - point2.x);
    double deltaY = (point1.y - point2.y);
    double angle =  Math.toDegrees(Math.atan2(deltaY, deltaX));
    if (angle < 0) {
        angle += 360;
    }

    if (angle > 360) {
        angle -= 360;
    }
    // Compensate for 0
    angle -= 180;
    return angle;
}

【讨论】:

  • 你说的“你还怎么过?”是什么意思?
  • 按照您在上述部分和代码中的评论 - 当我根据我触摸的方式触摸圆圈时,您想画尾巴吗?
  • 我目前正在遵循@pskink 的建议,我能够在路径周围移动对象(尾部)。我正在尝试根据我的需要即兴创作。如果您正在寻找与我相同的功能,您可以参考此链接bitbucket.org/TedBeer/spritealongpath/src/…
  • 是的,只需更改 onTouch 上的 linex 和 liney,它就会计算出该角度并围绕该角度进行绘制。我尝试使用 pathmeasure,但是当我找到交叉点时,无法从路径上的距离获得角度。
  • 我运行了你的代码。很接近了。我将尝试完成它并在此处发布完整的解决方案。谢谢:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多