概述:
沒什麼好說的。
Demo
新建一個自定義View
public class MyPathView extends View {
private int width;
private int height;
private Paint mPaint;
private Path mPath;
private Paint mPaintPoint;
private Paint mPaintText;
private boolean isAdd = true;
public MyPathView(Context context) {
super(context);
}
public MyPathView(Context context, AttributeSet attrs) {
super(context, attrs);
mPaint = new Paint();
mPaint.setColor(Color.GREEN);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setTextSize(50);
mPaint.setAntiAlias(true);
mPaintPoint = new Paint();
mPaintPoint.setAntiAlias(true);
mPaintPoint.setColor(Color.RED);
mPaintPoint.setStyle(Paint.Style.STROKE);
//mPaintPoint.setStrokeWidth(10);
mPaintText = new Paint();
mPaintText.setAntiAlias(true);
mPaintText.setColor(Color.BLACK);
mPaintText.setTextAlign(Paint.Align.CENTER);
mPaintText.setTextSize(20);
mPath = new Path();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
width = getDefaultSize(getSuggestedMinimumWidth(),widthMeasureSpec);
height = getDefaultSize(getSuggestedMinimumHeight(),heightMeasureSpec);
setMeasuredDimension(width,height);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//畫一個三角形
mPath.moveTo(100, 100);//起始位置
mPath.lineTo(0, 200);//從起始位置移動到此位置
mPath.lineTo(200, 200);//從上一位置移動到下一位置
mPath.close();//前面只畫了兩條邊,調用close()可以讓三角形自動閉合
canvas.drawPath(mPath, mPaint);//把path經歷的軌跡畫出來
}
}
畫一個圓,沿着圓寫一段文字,修改onDraw()方法:
/*畫一個圓,並沿着圓寫一段文字*/
//參數依次爲:圓心x座標,圓心y座標,圓的半徑,CW順時針(CCW逆時針)方向
mPath.addCircle(width / 2, height / 2, 300, Path.Direction.CW);
canvas.drawPath(mPath, mPaint);
//沿着path的軌跡寫一段文字
canvas.drawTextOnPath("我要沿着圓形寫文字", mPath, 0, 0, mPaint);
canvas.drawPath(mPath, mPaint);
演示結果:
貝塞爾曲線:
貝塞爾曲線也可以叫做貝濟埃曲線或者貝茲曲線,它由線段與節點組成,節點是可拖動的支點,線段像可伸縮的皮筋。一般的矢量圖形軟件常利用貝塞爾曲線來精確畫出曲線。
Android提供的貝塞爾曲線繪製接口
在Android開發中,要實現貝塞爾曲線其實還是很簡單的,因爲Android已經給我們提供了相關接口,但此接口方法,藏於Path類中(android.graphics.Path)。此方法如下:
quadTo(float x1, float y1, float x2, float y2)
Since: API Level 1
參數說明:
x1:操作點的x座標
y1:操作點的y座標
x2:結束點的x座標
y2:結束點的y座標
mPath.moveTo(100, 100);
//畫一條貝塞爾曲線,參數依次爲:控制點的x座標,控制點的y座標,結束點的x座標,結束點的y座標
mPath.quadTo(100, 400, 300, 300);
canvas.drawPath(mPath, mPaint);
canvas.drawPoint(100, 100, mPaintPoint);
canvas.drawPoint(100, 400, mPaintPoint);
canvas.drawPoint(300, 300, mPaintPoint);
//會出以上貝塞爾曲線的起始點、控制點、結束點
canvas.drawText("起始點", 100, 100, mPaintText);
canvas.drawText("控制點", 100, 400, mPaintText);
canvas.drawText("結束點", 300, 300, mPaintText);
演示結果:
用path繪製動態貝塞爾曲線——模擬水面:
public class MyPathView extends View {
private int width;
private int height;
private Paint mPaint;
private Path mPath;
private Paint mPaintPoint;
private Paint mPaintText;
private int size = 0;
private int count;
private boolean isAdd = true;
private static final int START_WAVE = 0x21;
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what){
case START_WAVE:
count+=5;
if(count>=80){
count=0;
}
if (isAdd){
size++;
if(size>10){
isAdd = false;
}
}else{
size--;
if(size<=-10){
isAdd =true;
}
}
invalidate();
sendEmptyMessageDelayed(START_WAVE,100);
break;
}
}
};
public MyPathView(Context context) {
super(context);
}
public MyPathView(Context context, AttributeSet attrs) {
super(context, attrs);
mPaint = new Paint();
mPaint.setColor(Color.GREEN);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setTextSize(50);
mPaint.setAntiAlias(true);
mPaintPoint = new Paint();
mPaintPoint.setAntiAlias(true);
mPaintPoint.setColor(Color.RED);
mPaintPoint.setStyle(Paint.Style.STROKE);
//mPaintPoint.setStrokeWidth(10);
mPaintText = new Paint();
mPaintText.setAntiAlias(true);
mPaintText.setColor(Color.BLACK);
mPaintText.setTextAlign(Paint.Align.CENTER);
mPaintText.setTextSize(20);
mPath = new Path();
handler.sendEmptyMessageDelayed(START_WAVE,1000);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
width = getDefaultSize(getSuggestedMinimumWidth(),widthMeasureSpec);
height = getDefaultSize(getSuggestedMinimumHeight(),heightMeasureSpec);
setMeasuredDimension(width,height);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
/*
畫一條個模擬流動的波浪
*/
mPath.reset();
//當count增大時,重繪會顯示向前流動效果
mPath.moveTo(count, 200);
//size的從大到小從小到大變化,重繪時會產生波浪起伏效果
for(int i=0;i<10;i++) {
/*
rQuadTo()方法每次都會自動移動到下一位置,參數依次爲水平幅度,
垂直幅度,水平位移,處置位移
*/
mPath.rQuadTo(20, size, 40, 0);
mPath.rQuadTo(20, -size, 40, 0);
}
canvas.drawPath(mPath,mPaintPoint);
canvas.drawCircle(width / 2, 200, 100, mPaint);
}
}
結果演示:
我們猿類工作壓力大,很需要有自己的樂趣,於是乎,我開通了音樂人賬號,以後的作品將會上傳到我的音樂人小站上。如果這篇博客幫助到您,希望您能多關注,支持,鼓勵我將創作進行下去,同時也祝你能在工作和生活樂趣兩發麪都能出彩!