public class BezierGestureTrackView extends View {
private Bitmap mBufferBitmap;
private Canvas mBufferCanvas;
public BezierGestureTrackView(Context context) {
super(context);
}
PointF pointF;
Paint paint;
Paint paint2;
Path p,path ;
@ColorInt public static final int YELLOW = 0xFFFFFF00;
@ColorInt
public static final int CYAN = 0xFF00FFFF;
public static final int c1 = 0xEF8A97;
public static final int c2 = 0xFBD2A8;
public BezierGestureTrackView(Context context, AttributeSet attrs) {
super(context, attrs);
paint = new Paint();
paint.setStyle(Paint.Style.STROKE);//设置画笔的填充模式
paint.setStrokeJoin(Paint.Join.ROUND);//设置画笔图形接触时笔迹的形状
paint.setStrokeCap(Paint.Cap.ROUND);//设置画笔离开画板时笔迹的形状
paint.setStrokeWidth(8);//设置画笔粗细 6
paint.setAntiAlias(true);//设置抗锯齿
paint.setColor(Color.WHITE);
initPaint();
// BitmapFactory.decodeResource(getResources(), R.drawable.hbbg);
}
//新建两个SparseArray<Path>用来保存我们画出的Path对象,具体为什么之后会解释
//mActivePointers用来保存当前正在画的Path
SparseArray<Path> mActivePointers = new SparseArray<>();
//truePointers用来保存所有的Path,在onDraw()方法里面我们也是绘制这里的所有Path
SparseArray<Path> truePointers = new SparseArray<>();
//保存单个点
private int i=0;
private float[] a= new float[1000];
//重写onTouchEvent()方法
@Override
public boolean onTouchEvent(MotionEvent event) {
//获取当前DOWN或者UP的是手指的index
int curPointerIndex = event.getActionIndex();
//通过index获得当前手指的id
int curPointerId = event.getPointerId(curPointerIndex);
//获取当前正在发生的事件
int actionMasked = event.getActionMasked();
switch (actionMasked) {
//不管是第一个还是第N个手指落下,都执行以下方法:
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_POINTER_DOWN:
//新建一个PointF对象用来保存点的坐标
pointF = new PointF();
pointF.x = event.getX(curPointerIndex);
pointF.y = event.getY(curPointerIndex);
//每当有手指下落时,都新建一个Path,并移动到手指落下的点
Path p = new Path();
p.moveTo(pointF.x, pointF.y);
//将新建的path添加到mActivePointers中
mActivePointers.append(curPointerId, p);
//保存单个点位置
if (i<998){
a[i++]=event.getX(curPointerIndex);
a[i++]=event.getY(curPointerIndex);
}
break;
case MotionEvent.ACTION_MOVE:
//当有手指移动时,遍历mActivePointers,注意这里遍历的条件并不是mActivePointers.size(),而是event.getPointerCount()
//也就是只监控当前所有正在滑动的手指数,另外在MotionEvent.ACTION_UP事件中,抬起的手指已经从mActivePointers中删除了
for (int size = event.getPointerCount(), i = 0; i < size; i++) {
//获取mActivePointers中的每一个Path
path = mActivePointers.get(event.getPointerId(i));
if (path != null) {
//更新每一个Path将得到的点连线
path.lineTo(event.getX(i), event.getY(i));
}
}
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_POINTER_UP:
//当手指抬起时,代表已经绘制完毕,取得抬起的手指绘制图线实例,传入truePointers让Drow()绘制
path = mActivePointers.get(curPointerId);
truePointers.append(truePointers.size(), path);
//从mActivePointers中将抬起的手指删除
mActivePointers.remove(curPointerId);
break;
}
//重绘
invalidate();
return true;
}
private void initBuffer(){
mBufferBitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
mBufferCanvas = new Canvas(mBufferBitmap);
}
@Override
protected void onDraw(Canvas canvas) {
//canvas.drawColor(Color.BLACK);
// Bitmap srcBitmap= BitmapFactory.decodeResource(getResources(), R.drawable.hbbg);
// canvas.drawBitmap(srcBitmap,0f,0f,null);
//遍历mActivePointers,因为只有当手指抬起时Path才被加入truePointers,此时truePointers里面还没有保存当前正在绘制的图线
//如果没有遍历mActivePointers,则图线不会即时显示,只有在抬起手指的瞬间,图线才会一起显示
for (int size = mActivePointers.size(), i = 0; i < size; i++) {
Path path = mActivePointers.valueAt(i);
canvas.drawPath(path, paint);
}
//遍历truePointers,这里保存的是当前正在画的图线
for (int size = truePointers.size(), i = 0; i < size; i++) {
path = truePointers.valueAt(i);
//绘制truePointers里的所有Path
canvas.drawPath(path, paint);
}
if (pointF!=null){
//canvas.drawPoint(pointF.x, pointF.y,paint2);
canvas.drawPoints(a, paint2);
for (int j = 0; j < a.length; j++) {
Log.e("TAG","a:"+a[i]+"a的长度:"+a.length);
}
}
}
// public void clean(){
//// Canvas canvas = sh.lockCanvas();
////
////
//// if(canvas!=null){
//// Paint paint = new Paint();
//// paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
//// canvas.drawPaint(paint);
//////canvas.drawColor(Color.BLUE);
//// }
// Log.e("BezierGestureTrackView","CLEAN");
// }
public void clear() {
if (mBufferBitmap != null) {
if (mActivePointers!=null){
mActivePointers.clear();
}
if (truePointers != null) {
truePointers.clear();
}
mBufferBitmap.eraseColor(Color.TRANSPARENT);
invalidate();
}
}
private void initPaint() {
paint2 = new Paint();
//LinearGradient lg=new LinearGradient(0,0,100,100,Color.RED,Color.BLUE, Shader.TileMode.MIRROR); //参数一为渐变起初点坐标x位
LinearGradient lg=new LinearGradient(0,0,100,100,CYAN,YELLOW, Shader.TileMode.MIRROR); //参数一为渐变起初点坐标x位
paint2.setStyle(Paint.Style.STROKE);//设置画笔的填充模式
paint2.setStrokeJoin(Paint.Join.ROUND);//设置画笔图形接触时笔迹的形状
paint2.setStrokeCap(Paint.Cap.ROUND);//设置画笔离开画板时笔迹的形状
paint2.setStrokeWidth(45);//设置画笔粗细 50
paint2.setAntiAlias(true);//设置抗锯齿
paint2.setColor(Color.WHITE);//设置抗锯齿
paint2.setShader(lg);
}
}