如何绘制像椭圆和线条的形状,并在手势上计算面积

时间:2017-09-30 08:47:05

标签: android

Philips Lumify

在playstore android中参考Philips Lumify应用程序。请帮我生成这个形状。

1 个答案:

答案 0 :(得分:0)

创建custom view并覆盖onDraw(Canvas canvas)方法,你必须draw your shape内的方式:

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

   if(touched)
       canvas.drawCircle(centerX, centerY, radius, mPaint);
}

然后在onTouchEvent方法中处理手势:

@Override
public boolean onTouchEvent(MotionEvent e) {
    float x = e.getX();
    float y = e.getY();

    switch (e.getAction()) {
        case MotionEvent.ACTION_DOWN:
            touched = true;
            centerX = x;
            centerY = y;
            break;
        case MotionEvent.ACTION_MOVE:
            // calculate radius
            radius = Math.abs(centerX - x);
            break;
        case (MotionEvent.ACTION_UP) :
            touched = false;
            break;
    }
    // call next frame
    postInvalidate();
    return true;
}

另见canvas.drawOval方法。