继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

粘性控件

皈依舞
关注TA
已关注
手记 99
粉丝 13
获赞 37

自定义一个GooView

// 初始化要使用到的资源和对象

private void initView() {

paint = new Paint();

paint.setAntiAlias(true);//边缘更加清晰

paint.setColor(Color.RED);

}

对画笔进行初始化

@Override

protected void onDraw(Canvas canvas) {

super.onDraw(canvas);

if (isDisappear) {

return;

}

mStickRadius = fixStickRadius();

// 连接组建数据

float offsetY = mStickCenter.y - mDragCenter.y;

float offsetX = mStickCenter.x - mDragCenter.x;

Double lineK = 0d;

if (offsetX!=0) {

lineK = (double) (offsetY / offsetX);

}

PointF[] mStickPoints = GeometryUtil.getIntersectionPoints(mStickCenter, mStickRadius, lineK);

PointF[] mDragPoints = GeometryUtil.getIntersectionPoints(mDragCenter, mDragRadius, lineK);

PointF mControlorPoint = GeometryUtil.getMiddlePoint(mStickCenter, mDragCenter);

// 绘制拖拽圆

canvas.drawCircle(mDragCenter.x, mDragCenter.y, mDragRadius, paint);

// 如果没有超出最大距离,才绘制固定圆和连接组件

if (!isBrokenConnect) {

// 绘制固定圆

canvas.drawCircle(mStickCenter.x, mStickCenter.y, mStickRadius, paint);

// 绘制连接组件

Path path = new Path();

path.moveTo(mDragPoints[0].x, mDragPoints[0].y);// 移动到起始点

path.quadTo(mControlorPoint.x, mControlorPoint.y, mStickPoints[0].x, mStickPoints[0].y);// 绘制曲线,x1、y1 是控制点,x2、y2是结束点

path.lineTo(mStickPoints[1].x, mStickPoints[1].y);// 绘制直线

path.quadTo(mControlorPoint.x, mControlorPoint.y, mDragPoints[1].x, mDragPoints[1].y);

path.close();// 闭合路径

canvas.drawPath(path, paint);

}

// 绘制参考内容

paint.setStyle(Style.STROKE);// 设置为只绘制边线

paint.setColor(Color.BLUE);// 修改填充颜色

canvas.drawCircle(mStickCenter.x, mStickCenter.y, MAX_DISTANCE, paint);

canvas.drawCircle(mDragPoints[0].x, mDragPoints[0].y, 2, paint);

canvas.drawCircle(mDragPoints[1].x, mDragPoints[1].y, 2, paint);

canvas.drawCircle(mStickPoints[0].x, mStickPoints[0].y, 2, paint);

canvas.drawCircle(mStickPoints[1].x, mStickPoints[1].y, 2, paint);

paint.setStyle(Style.FILL);

paint.setColor(Color.RED);

}

无效的时候就不绘图,

private float fixStickRadius() {

float distance = GeometryUtil.getDistanceBetween2Points(mStickCenter, mDragCenter);

float fraction = distance / MAX_DISTANCE;// 百分比 = 当前的圆心距离 / 最大距离

if (fraction > 1) {

fraction = 1;

}

return evaluate(fraction , mDragRadius, mDragRadius/2);

}

返回一个浮点数,就是拖拽棍子的长度。

mDragCenter这个被不断刷新

// 如果没有超出最大距离,才绘制固定圆和连接组件

if (!isBrokenConnect) {

// 绘制固定圆

canvas.drawCircle(mStickCenter.x, mStickCenter.y, mStickRadius, paint);

// 绘制连接组件

Path path = new Path();

path.moveTo(mDragPoints[0].x, mDragPoints[0].y);// 移动到起始点

path.quadTo(mControlorPoint.x, mControlorPoint.y, mStickPoints[0].x, mStickPoints[0].y);// 绘制曲线,x1、y1 是控制点,x2、y2是结束点

path.lineTo(mStickPoints[1].x, mStickPoints[1].y);// 绘制直线

path.quadTo(mControlorPoint.x, mControlorPoint.y, mDragPoints[1].x, mDragPoints[1].y);

path.close();// 闭合路径

canvas.drawPath(path, paint);

}

如果没有被中断话棍子,path.close();// 闭合路径

case MotionEvent.ACTION_MOVE:

// 移动拖拽圆的位置

float x = event.getX();

float y = event.getY();

updateDragCenter(x, y);

// 如果现在两个圆的距离超过了最大限制,则标识不再绘制连接组件和固定圆

float distance = GeometryUtil.getDistanceBetween2Points(mStickCenter, mDragCenter);

if (distance > MAX_DISTANCE) {

isBrokenConnect = true;

}

invalidate();

更新棍子的长度。重绘

// 松手时,如果没有断开连接,则显示回弹效果

final PointF startPointF = new PointF(mDragCenter.x, mDragCenter.y);

ValueAnimator animator = ValueAnimator.ofFloat(1);

animator.addUpdateListener(new AnimatorUpdateListener() {

@Override

public void onAnimationUpdate(ValueAnimator value) {

System.out.println("time="+value.getCurrentPlayTime()+";percent="+value.getAnimatedFraction());

float percent = value.getAnimatedFraction();

PointF newPoint = GeometryUtil.getPointByPercent(startPointF, mStickCenter, percent);

updateDragCenter(newPoint.x, newPoint.y);

}

});

animator.addListener(new AnimatorListener() {

@Override

public void onAnimationStart(Animator arg0) {

}

@Override

public void onAnimationRepeat(Animator arg0) {

}

@Override

public void onAnimationEnd(Animator arg0) {

// 动画播放结束之后,调用监听

if (onDragEventListener!=null) {

onDragEventListener.onReset(false);

}

}

@Override

public void onAnimationCancel(Animator arg0) {

}

});

animator.setDuration(200);

animator.setInterpolator(new OvershootInterpolator(8));

animator.start();

}

invalidate();

break;

没有中断的时候。刷新值

原文链接:http://www.apkbus.com/blog-880881-63458.html

打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP