5.10
所花时间(包括上课):1
打码量(行):200
博客量(篇):1
了解到知识点:学习绘制路径
package com.example.myapp;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.View;
import androidx.annotation.Nullable;
public class PathDrawingView extends View {
private Paint paint;
private Path path;
public PathDrawingView(Context context) {
super(context);
init();
}
public PathDrawingView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init();
}
public PathDrawingView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
paint = new Paint();
paint.setColor(Color.BLUE);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(5f);
path = new Path();
// 添加路径操作,例如绘制一个三角形
path.moveTo(100, 100);
path.lineTo(200, 200);
path.lineTo(300, 100);
path.close(); // 闭合路径
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// 绘制路径
canvas.drawPath(path, paint);
}
}
<?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">
<com.example.myapp.PathDrawingView
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
本文来自博客园,作者:赵千万,转载请注明原文链接:https://www.cnblogs.com/zhaoqianwan/p/18177839
千万千万赵千万