画笔 --- 画一个波(贝赛尔曲线)

目的:画贝赛尔曲线

大致效果展示:

效果展示

代码:

WaveView类:
package swu.twj.a13_drawview;

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;

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

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

    @Override
    protected void onDraw(Canvas canvas) {
        //创建一个路径
        Path mPath = new Path();

        mPath.moveTo(50,500);
        //画贝赛尔曲线
        /*mPath.cubicTo(50,500,
                      200,300,
                      350,500);

        mPath.cubicTo(350,500,
                      500,700,
                      650,500);*/

        mPath.quadTo(200,300,350,500);
        mPath.quadTo(500,700,650,500);

        //画笔
        Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setStrokeWidth(10);
        mPaint.setColor(Color.BLACK);
        mPaint.setStyle(Paint.Style.STROKE);
        //绘制
        canvas.drawPath(mPath,mPaint);
    }
}
xml文件配置:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">

    <swu.twj.a13_drawview.WaveView
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。