目的:畫貝賽爾曲線
大致效果展示:
代碼:
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) {
//創(chuàng)建一個路徑
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>
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者