smallCircleX = bigCircleX; smallCircleY = bigCircleY;
} return true; }
弧度计算
通过 event.getX(), event.getY()获得当前的触摸点,与圆点进行计算,获取弧度
/***
- 得到两点之间的弧度 */ public float getRad(float px1, float py1, float px2, float py2) { float x = px2 - px1;
float y = py1 - py2; //斜边的长 float z = (float) Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2)); float cosAngle = x / z; float rad = (float) Math.acos(cosAngle);
if (py2 < py1) { rad = -rad; } return rad; }
图形绘制
通过 canvas.drawCircle()和 canvas.drawBitmap()分别进行遥感按钮和遥感背景的绘制,注意对遥感背景的保存,如果在绘制的时候每次BitmapFactory.decodeResource()会增加耗时,因此只需在surfaceCreated()中进行bitmap的生成即可。
public void draw() { try { canvas = sfh.lockCanvas(); canvas.drawColor(getResources().getColor(R.color.ghostwhite));
// 指定图片绘制区域(左上角的四分之一) Rect src = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
// 指定图片在屏幕上显示的区域 Rect dst = new Rect(bigCircleX - bigCircleR, bigCircleY - bigCircleR, bigCircleX + bigCircleR, bigCircleY + bigCircleR); // 绘制图片 remoteViewBg.draw(canvas, paint, src, dst); paint.setColor(0x70ff0000); //绘制摇杆 canvas.drawCircle(smallCircleX, smallCircleY, smallCircleR, paint); } catch (Exception e) { // TODO: handle exception } finally { try { if (canvas != null) sfh.unlockCanvasAndPost(canvas); } catch (Exception e2) { e2.printStackTrace(); } } }
使用
在activity中动态添加
RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.dance_relative_layout); remoteSurfaceView = new RemoteSurfaceView(this); params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT); remoteSurfaceView.setLayoutParams(params); relativeLayout.addView(remoteSurfaceView);
关键代码
public RemoteSurfaceView(Context context) { super(context); sfh = this.getHolder(); sfh.addCallback(this); paint = new Paint(); paint.setAntiAlias(true); setFocusable(true); setFocusableInTouchMode(true); setZOrderOnTop(true); getHolder().setFormat(PixelFormat.TRANSPARENT);
}
public void surfaceCreated(SurfaceHolder holder) { int width = getWidth(); int height = getHeight(); bigCircleX = width / 2; bigCircleY = height / 2; bigCircleR = width / 4; smallCircleX = width / 2; smallCircleY = height / 2; smallCircleR = width / 8; bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.fangxiang); remoteViewBg = new RemoteViewBg(bitmap); th = new Thread(this); flag = true; th.start();
}
/***
- 得到两点之间的弧度 */ public float getRad(float px1, float py1, float px2, float py2) { float x = px2 - px1;
float y = py1 - py2; //斜边的长 float z = (float) Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2)); float cosAngle = x / z; float rad = (float) Math.acos(cosAngle);
if (py2 < py1) { rad = -rad; } return rad; }
@Override public boolean onTouchEvent(MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_DOWN || event.getAction() == MotionEvent.ACTION_MOVE) { // 范围外触摸 if (Math.sqrt(Math.pow((bigCircleX - (int) event.getX()), 2) + Math.pow((bigCircleY - (int) event.getY()), 2)) >= bigCircleR) {
double tempRad = getRad(bigCircleX, bigCircleY, event.getX(), event.getY());
getXY(bigCircleX, bigCircleY, bigCircleR, tempRad); } else {//范围内触摸 smallCircleX = (int) event.getX(); smallCircleY = (int) event.getY(); } } else if (event.getAction() == MotionEvent.ACTION_UP) { smallCircleX = bigCircleX; smallCircleY = bigCircleY;
} return true; }
结尾
好了,今天的分享就到这里,如果你对在面试中遇到的问题,或者刚毕业及工作几年迷茫不知道该如何准备面试并突破现状提升自己,对于自己的未来还不够了解不知道给如何规划,可以来看看同行们都是如何突破现状,怎么学习的,来吸收他们的面试以及工作经验完善自己的之后的面试计划及职业规划。
这里放上一部分我工作以来以及参与过的大大小小的面试收集总结出来的一套进阶学习的视频及面试专题资料包,在这里免费分享给大家,主要还是希望大家在如今大环境不好的情况下面试能够顺利一点,希望可以帮助到大家~