以下是一个简单的Android绘图实战示例,使用Canvas、Paint和Path来绘制一个自定义的图形:

1. 创建一个新的CustomView类,继承自View:
public class CustomView extends View {
    private Paint paint;
    private Path path;

    public CustomView(Context context) {
        super(context);
        init();
    }

    public CustomView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        paint = new Paint();
        paint.setColor(Color.BLUE);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(8);

        path = new Path();
    }

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

        // 绘制路径
        canvas.drawPath(path, paint);
    }

    // 处理触摸事件
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float x = event.getX();
        float y = event.getY();

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                // 按下时移动到起始点
                path.moveTo(x, y);
                break;
            case MotionEvent.ACTION_MOVE:
                // 移动时连接到目标点
                path.lineTo(x, y);
                break;
            case MotionEvent.ACTION_UP:
                // 抬起时结束路径
                break;
        }

        // 触发重绘
        invalidate();
        return true;
    }

    // 清除绘图
    public void clearDrawing() {
        path.reset();
        invalidate();
    }
}

2. 在布局文件中使用该自定义View:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp">

    <com.example.yourpackage.CustomView
        android:id="@+id/customView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#ECECEC" />

    <Button
        android:id="@+id/btnClear"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Clear Drawing"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="16dp"/>
</RelativeLayout>

3. 在Activity中使用该自定义View:
public class MainActivity extends AppCompatActivity {

    private CustomView customView;
    private Button btnClear;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        customView = findViewById(R.id.customView);
        btnClear = findViewById(R.id.btnClear);

        btnClear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 清除绘图
                customView.clearDrawing();
            }
        });
    }
}

这个示例创建了一个自定义的CustomView,当用户在屏幕上滑动时,通过触摸事件(MotionEvent)绘制路径。用户可以点击"Clear Drawing"按钮来清除绘图。这是一个简单的涂鸦应用示例,演示了Canvas、Paint和Path的基本用法。在实际应用中,你可以根据需求进一步扩展和定制。


转载请注明出处:http://www.pingtaimeng.com/article/detail/15216/Android