【发布时间】: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