安卓系统上,通过SurfaceView绘制动态的贝塞尔曲线的demo示例,请任意用.
SurfaceView代码如下:
package com.example.test.widget;
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.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class BezierView extends SurfaceView implements SurfaceHolder.Callback, Runnable {
private Path path = new Path();
Paint paint = new Paint();
public BezierView(Context context) {
super(context);
init();
}
public BezierView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public BezierView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
public BezierView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init();
}
private void init() {
paint.setColor(Color.BLUE);
paint.setStyle(Paint.Style.STROKE);
// line width.
paint.setStrokeWidth(150);
paint.setAntiAlias(true);
mHolder = getHolder();
mHolder.addCallback(this);
}
private boolean mIsDrawing = false;
private Thread mThread;
int startX = 0;
int startY;
int endX;
int endY;
int middleX;
int middleY = 0;
int count = 1;
int mWidth;
int mHeight;
int mHeightIncrease;
@Override
public void surfaceCreated(SurfaceHolder holder) {
Log.d("test111", "surfaceCreated");
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
Log.d("test111", "surfaceChanged, width:" + width + ", height:" + height);
mWidth = width;
mHeight = height;
mHeightIncrease = mHeight / 120; // change this to may bezier line move faster or slower.
initPosition();
mIsDrawing = true;
mThread = new Thread(this);
mThread.start();
}
private void initPosition() {
mHolder = getHolder();
//mCanvas = mHolder.lockCanvas();
startX = 0;
startY = mHeight / 2;
middleX = mWidth / 2;
middleY = 100;
endX = mWidth + 150;
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
Log.d("test111", "surfaceDestroyed");
}
@Override
public void run() {
while (mIsDrawing) {
//Log.d("test111", "draw start");
draw();
}
}
private SurfaceHolder mHolder;
private Canvas mCanvas;
int flag = 1;
private void caculateMiddlePosition() {
// change endY value on each draw action.
count = count + mHeightIncrease * flag;
if (count >= mHeight) {
flag = -1;
}
if (count <= 0) {
flag = 1;
}
endY = count;
// 清除上次的quadTo对象。
path.rewind();
path.moveTo(startX, startY); // 起点
path.quadTo(middleX, middleY, endX, endY); // 中间点、终点
}
private void draw() {
try {
caculateMiddlePosition();
mCanvas = mHolder.lockCanvas();
mCanvas.drawColor(Color.WHITE);
// 绘制贝塞尔曲线
mCanvas.drawPath(path, paint);
mHolder.unlockCanvasAndPost(mCanvas);
} catch (Exception e) {
Log.w("test111", "draw( error", e);
}
}
}
layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:background="@null"
android:focusableInTouchMode="true"
android:orientation="vertical"
tools:context=".MainActivity">
<com.example.test.widget.BezierView
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>