sunwengang blog

developer | android display/graphics

  1. 1. 代码分析
  2. 2. 问题案例
    1. 2.1. 问题描述
    2. 2.2. 分析
    3. 2.3. 修改

打开开发者选项中的“指针位置”,界面上方状态栏处会显示当前触屏的X/Y坐标,那么这个坐标值是怎么打印出来的呢?

代码分析

在frameworks/base/core/java/com/android/internal/widget/PointerLocationView.java的onDraw方法中,在触屏的时候会实时打印底层读取的X/Y值。

如下代码(Android Q AOSP源码),ps.mCoords.xps.mCoords.y是底层传递读取的坐标值,float类型。

最后会显示成类似X:500.5 Y:200.6

PointerLocationView.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
@Override
protected void onDraw(Canvas canvas) {
final int w = getWidth();
final int itemW = w/7;
final int base = -mTextMetrics.ascent+1;
final int bottom = mHeaderBottom;

final int NP = mPointers.size();

// Labels
if (mActivePointerId >= 0) {
final PointerState ps = mPointers.get(mActivePointerId);

canvas.drawRect(0, 0, itemW-1, bottom,mTextBackgroundPaint);
canvas.drawText(mText.clear()
.append("P: ").append(mCurNumPointers)
.append(" / ").append(mMaxNumPointers)
.toString(), 1, base, mTextPaint);

final int N = ps.mTraceCount;
if ((mCurDown && ps.mCurDown) || N == 0) {
canvas.drawRect(itemW, 0, (itemW * 2) - 1, bottom, mTextBackgroundPaint);
canvas.drawText(mText.clear()
.append("X: ").append(ps.mCoords.x, 1)
.toString(), 1 + itemW, base, mTextPaint);
canvas.drawRect(itemW * 2, 0, (itemW * 3) - 1, bottom, mTextBackgroundPaint);
canvas.drawText(mText.clear()
.append("Y: ").append(ps.mCoords.y, 1)
.toString(), 1 + itemW * 2, base, mTextPaint);
}
......

问题案例

问题描述

如果此时设备的分辨率是1080x2340,“指针位置”坐标值边缘滑动需要显示到1079x2339。而现在出现了问题:在竖屏的时候只能显示到1078x2338,横屏(两种横屏情况)只能显示到1079x23381078xz2339?此处如何进行修改?

分析

首先要对此处读取的ps.mCoords.xps.mCoords.y值打印,发现在滑动到边缘的时候,应该显示1079,打印的值大约是1078.0001;应该显示2339的时候,打印的值大约是2038.0001

所以在此处需要判断,在大于1078或2339的时候,使用进一法,将其作加一操作。

同时还要考虑到横屏和竖屏两种状态。

修改

因为PointerLocationView.java继承view.java,可以使用getResources().getConfiguration();来获取设备参数,从而获取到当前横竖屏的状态。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
//如果是true,则对坐标加一操作
private boolean mRealX = false;
private boolean mRealY = false;
//获取当前设备横竖屏状态
private int mOrientation;

@Override
protected void onDraw(Canvas canvas) {
......
//获取横屏、竖屏状态并判断
Configuration mConfiguration = getResources().getConfiguration();
mOrientation = mConfiguration.orientation;
if (mOrientation == mConfiguration.ORIENTATION_PORTRAIT) {
mRealX = ps.mCoords.x > 972.0f && ps.mCoords.x < 1079.0f;
mRealY = ps.mCoords.y > 2106.0f && ps.mCoords.y < 2339.0f;
} else if (mOrientation == mConfiguration.ORIENTATION_LANDSCAPE) {
mRealX = ps.mCoords.x > 2106.0f && ps.mCoords.x < 2339.0f;
mRealY = ps.mCoords.y > 972.0f && ps.mCoords.y < 1079.0f;
}

//绘制的时候判断是否加一
canvas.drawText(mText.clear()
.append("X: ").append(mRealX ? ps.mCoords.x + 1.0f : ps.mCoords.x, 1)
.toString(), 1 + itemW, base, mTextPaint);
canvas.drawRect(itemW * 2, mHeaderPaddingTop, (itemW * 3) - 1, bottom,
mTextBackgroundPaint);
canvas.drawText(mText.clear()
.append("Y: ").append(mRealY ? ps.mCoords.y + 1.0f : ps.mCoords.y, 1)
.toString(), 1 + itemW * 2, base, mTextPaint);
mRealX = false;
mRealY = false;
} else {
......

本文作者 : sunwengang
本文使用 署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0) 协议
本文链接 : https://alonealive.github.io/Blog/2020/06/10/2020/200610_android_pointerDraw/

本文最后更新于 天前,文中所描述的信息可能已发生改变