安卓自定义View绘制多边形

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

public class MyView extends View {
    public MyView(Context context) {
        super(context);
    }

    public MyView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    //重写绘图方法
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Paint paint=new Paint();//创建画笔
        paint.setColor(Color.RED);//为画笔设置颜色
        paint.setStrokeWidth(10);//为画笔设置粗细
        paint.setStyle(Paint.Style.STROKE);//设置空心
        canvas.drawColor(Color.GREEN);//为画布设置颜色
        //设置等腰三角形的三点坐标
        Path path=new Path();//绘制多边形的类
        path.moveTo(100,100);//起始点
        path.lineTo(150,150);//右下角
        path.lineTo(50,150);//左下角
        path.close();//闭合图形
        //绘制三角形
        canvas.drawPath(path,paint);
    }
}

 创建一个MyView类继承View,并重写onDrow()方法。

在布局文件中使用该控件即可

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <com.example.myapplication8.MyView
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

 

发布了15 篇原创文章 · 获赞 2 · 访问量 5240

猜你喜欢

转载自blog.csdn.net/qq_41985689/article/details/81501501