Android 绘制直线围绕一个点旋转

在 Android 的开发中,绘制图形是一个常见的需求。特别是绘制直线并让它绕某个点旋转,能够让我们在游戏开发、动画效果等领域中提供丰富的视觉体验。本文将为大家介绍如何在 Android 中实现这一功能,并提供相关代码示例。

绘制直线的基本思路

在实现直线围绕某个点旋转的过程中,我们需要:

  1. 创建一个自定义视图。
  2. 在视图中重写 onDraw 方法以绘制直线。
  3. 调用 Canvas 的旋转方法来实现旋转效果。

我们可以利用 Matrix 类来简化旋转的计算。

自定义 View 的实现

首先,我们创建一个名为 RotatingLineView 的自定义视图类,重写其 onDraw 方法来绘制直线。

package com.example.rotatingline;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

public class RotatingLineView extends View {

    private Paint paint;
    private float angle = 0; // 当前角度
    private float centerX; // 中心点X坐标
    private float centerY; // 中心点Y坐标
    private float lineLength = 300; // 直线长度

    public RotatingLineView(Context context, AttributeSet attrs) {
        super(context, attrs);
        paint = new Paint();
        paint.setColor(0xFF000000); // 直线颜色黑色
        paint.setStrokeWidth(8);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        centerX = getWidth() / 2;
        centerY = getHeight() / 2;

        // 保存当前状态
        canvas.save();
        // 以中心点旋转画布
        canvas.rotate(angle, centerX, centerY);
        // 绘制直线
        canvas.drawLine(centerX, centerY, centerX + lineLength, centerY, paint);
        // 恢复画布状态
        canvas.restore();
    }

    public void setAngle(float angle) {
        this.angle = angle;
        invalidate(); // 重绘
    }
}

旋转效果的实现

我们需要在 Activity 中更新角度来不断旋转直线。可以使用一个 Handler 来定时更新角度,并调用 invalidate() 刷新视图。

import android.os.Bundle;
import android.os.Handler;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private RotatingLineView rotatingLineView;
    private Handler handler = new Handler();
    private Runnable runnable;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        rotatingLineView = new RotatingLineView(this, null);
        setContentView(rotatingLineView);

        runnable = new Runnable() {
            @Override
            public void run() {
                // 更新角度
                rotatingLineView.setAngle(rotatingLineView.angle + 5);
                handler.postDelayed(this, 100);
            }
        };
        handler.post(runnable);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        handler.removeCallbacks(runnable); // 清除回调
    }
}

类图

下图展示了 RotatingLineViewMainActivity 之间的关系:

classDiagram
    class RotatingLineView {
        - Paint paint
        - float angle
        - float centerX
        - float centerY
        - float lineLength
        + void onDraw(Canvas canvas)
        + void setAngle(float angle)
    }

    class MainActivity {
        - RotatingLineView rotatingLineView
        - Handler handler
        - Runnable runnable
        + void onCreate(Bundle savedInstanceState)
        + void onDestroy()
    }
    
    MainActivity --> RotatingLineView

项目进度甘特图

以下是本文实现这个功能的甘特图,你可以参考这个进度安排:

gantt
    title Rotating Line Project Timeline
    dateFormat  YYYY-MM-DD
    section Initial Setup
    Create Project            :done, 2023-10-01, 1d
    section Class Implementation
    Design RotatingLineView   :active, 2023-10-02, 2d
    Implement onDraw Method   : 2023-10-04, 2d
    section Main Activity
    Initialize MainActivity    : 2023-10-06, 2d
    Implement Rotation Logic    : 2023-10-08, 1d

结论

通过以上步骤,我们在 Android 中实现了一个简单的直线围绕某个点旋转的效果。这个功能不仅展示了自定义视图绘制的基本技巧,还强化了对 Canvas 操作和 Android 应用生命周期的理解。希望本文能够帮助你更深入地理解 Android 图形绘制。你可以进一步扩展这个功能,比如添加用户控制的旋转速度、改变线条的颜色等。