android自定义View——仿九宫格解锁,android高级开发面试题以及答案

本文详细介绍了如何在Android中实现自定义九宫格解锁视图,包括绘制连接线、箭头以及处理触摸事件。通过示例代码展示了如何监听并处理用户的触摸操作,验证输入的密码是否正确,并提供了错误处理机制。此外,还探讨了箭头绘制的原理和引用对象赋值的特性。
摘要由CSDN通过智能技术生成

float d = (float) twoPointDistace(lastPoint.x, lastPoint.y, curPoint.x, curPoint.y);

float cos_a = (curPoint.x - lastPoint.x) * 1.0f / d;

float sin_a = (curPoint.y - lastPoint.y) * 1.0f / d;

canvas.drawLine(lastPoint.x + inCircleR * cos_a, lastPoint.y + inCircleR * sin_a,

curPoint.x - inCircleR * cos_a, curPoint.y - inCircleR * sin_a, linePressPaint);

//绘制箭头

drawArrow(lastPoint.x, lastPoint.y, d, cos_a, sin_a, canvas);

lastPoint = curPoint;

}

if (isPressFlag){

//绘制射线

float d = (float) twoPointDistace(lastPoint.x, lastPoint.y, downPointX, downPointY);

float cos_a = (downPointX - lastPoint.x) * 1.0f / d;

float sin_a = (downPointY - lastPoint.y) * 1.0f / d;

canvas.drawLine(lastPoint.x + inCircleR * cos_a, lastPoint.y + inCircleR * sin_a,

downPointX, downPointY, linePressPaint);

}

}

}

private void drawArrow(float startX, float startY, float d, float cos_a, float sin_a, Canvas canvas) {

float l = (float) (arrowHeight * (Math.tan(Math.toRadians(angle))));

float x0 = startX + (d - outCircleR - arrowHeight) * cos_a;

float y0 = startY + (d - outCircleR - arrowHeight) * sin_a;

float x1 = startX + (d - outCircleR) * cos_a;

float y1 = startY + (d - outCircleR) * sin_a;

float x2 = x0 + l * sin_a;

float y2 = y0 - l * cos_a;

float x3 = x0 - l * sin_a;

float y3 = y0 + l * cos_a;

Path path = new Path();

path.moveTo(x1, y1);

path.lineTo(x2, y2);

path.lineTo(x3, y3);

path.close();

linePressPaint.setStyle(Paint.Style.FILL);

canvas.drawPath(path, linePressPaint);

}

private double twoPointDistace(float startX, float startY, float endX, float endY) {

return Math.sqrt(Math.pow(startX - endX, 2) + Math.pow(startY - endY, 2));

}

@Override

public boolean onTouchEvent(MotionEvent event) {

downPointX = event.getX();

downPointY = event.getY();

System.out.println(event.getAction());

switch (event.getAction()) {

case Moti

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值