天天看點

android 畫日出日落圓弧曲線

    private PointF mPointF1;

    private PointF mControl;

    private PointF touchPoint;

    private float startAngle = 180;

    private float sweepAngle = 60;

    private float radius = 100;

    private float LEFT = 100;

    private float TOP = LEFT + radius;

    private float RIGHT = 300;

    private float BOTTOM = 400;

    private RectF mRectF;

    private void init() {

        mPaint = new Paint();

        mPaint.setAntiAlias(true);

        mPaint.setStyle(Paint.Style.FILL);

        mPaint.setColor(Color.BLACK);

        mPath = new Path();

        mPointF1 = new PointF(LEFT, RIGHT);

        touchPoint = new PointF(0,0);

        mControl = new PointF(0,0);

        mRectF = new RectF(LEFT,TOP,RIGHT,BOTTOM);

    }

    @Override

    public boolean onTouchEvent(MotionEvent event) {

        switch (event.getAction()) {

            case MotionEvent.ACTION_MOVE:

            case MotionEvent.ACTION_DOWN:

                touchPoint.x = event.getX();

                touchPoint.y = event.getY();

                float d = BOTTOM - radius - touchPoint.y;

                float l = touchPoint.x - LEFT - radius;

                Log.e("jyg","onTouchEvent,d="+d);

                Log.e("jyg","onTouchEvent,l="+l);

                float angle = (float)Math.toDegrees(Math.atan (d/l));

                Log.e("jyg","onTouchEvent,angle="+angle);

                if(angle < 0){

                    sweepAngle = Math.abs(angle);

                } else if(angle < 180){

                    sweepAngle = 180 - angle;

                }

                Log.e("jyg","onTouchEvent,touchPoint="+touchPoint);

                Log.e("jyg","onTouchEvent,sweepAngle="+sweepAngle);

                invalidate();

                break;

        }

        return true;

    }

    private Paint mPaint;

    private Path mPath;

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)

    @Override

    protected void onDraw(Canvas canvas) {

        super.onDraw(canvas);

        mPath.reset();

        draw1(canvas);

    }

    private void draw1(Canvas canvas) {

        mPath.moveTo(mPointF1.x, mPointF1.y);

        mPath.arcTo(mRectF,startAngle,sweepAngle);

        float xx,yy;

        xx = LEFT+radius - (float)(Math.cos(Math.toRadians(sweepAngle)) * radius);

        yy = BOTTOM - radius - (float) Math.sin(Math.toRadians(sweepAngle)) * radius;

        Log.e("jyg","xx="+xx);

        Log.e("jyg","yy="+yy);

        mControl.x = xx;

        mControl.y = yy;

        mPath.moveTo(mControl.x, mControl.y);

        mPath.lineTo(mControl.x,BOTTOM-radius);

        mPath.lineTo(mPointF1.x,mPointF1.y);

        canvas.drawPath(mPath, mPaint);

    }