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>

posted @ 2024-05-10 20:20  赵千万  阅读(3)  评论(0编辑  收藏  举报