- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在自定义 View 的 onDraw
方法中为我的 Canvas 设置了一个矩阵canvas.setMatrix(matrix);
然后我只使用预定义的颜料绘制一个网格:
canvas.drawRect(0,0,viewWidth,viewHeight, background);
for(int i=0; i <= nRows; i++){
canvas.drawLine(0,i*cellHeight, nCols*cellWidth,i*cellHeight,lines);
canvas.drawLine(i*cellWidth, 0, i*cellWidth, nRows*cellHeight, lines);
if(i != nRows)
canvas.drawText(""+i, i*cellWidth, (i+1)*cellHeight, text);
}
并且出于某种原因,整个 Canvas 偏移了单元格高度的大约 1.5 倍。知道为什么会发生这种情况或如何解决它吗?矩阵未初始化,根据文档,顺便说一句,因此应该是恒等式。
非常感谢!
最佳答案
我已将这种行为缩小到 View
的 Canvas 的原始 Matrix
已经被 View 的位置平移了。但是,如果您使用 Canvas.getMatrix()
或 View.getMatrix()
获取 Matrix
,这并不明显。您将从这些调用中获得单位矩阵。
您看到的 Canvas 偏移很可能与 View
从屏幕顶部(状态栏、标题栏等)的偏移高度完全相同。
在这个用例和大多数用例中,您使用 canvas.concat(matrix)
而不是 canvas.setMatrix(matrix)
是正确的。如果你真的需要原始矩阵,我在调试时做了,你必须通过 View
在它自己的 Window
中的翻译手动转换它:
int[] viewLocation = new int[2];
mView.getLocationInWindow(viewLocation);
mOriginalMatrix.setTranslate(viewLocation[0], viewLocation[1]);
编辑以回答评论中的附加问题:
要转换触摸坐标(或任何屏幕坐标)以匹配 Canvas
的坐标,只需将所有转换改为 Matrix
和 Canvas。 concat()
在绘制之前每帧使用该矩阵。 (或者您可以像现在一样直接对 Canvas
进行所有转换,并在每次绘制后使用 Canvas.getMatrix(mMyMatrix)
检索矩阵。它是已弃用,但它有效。)
矩阵随后可用于将原始网格边界转换为屏幕上绘制的网格边界。您实际上在做与 Canvas
在绘制网格时所做的完全相同的事情,将网格的角点转换为屏幕坐标。网格现在将与您的触摸事件处于相同的坐标系中:
private final Matrix mMyMatrix = new Matrix();
// Assumes that the grid covers the whole View.
private final float[] mOriginalGridCorners = new float[] {
0, 0, // top left (x, y)
getWidth(), getHeight() // bottom right (x, y)
};
private final float[] mTransformedGridCorners = new float[4];
@Override
public boolean onTouchEvent(MotionEvent event) {
if (/* User pans the screen */) {
mMyMatrix.postTranslate(deltaX, deltaY);
}
if (/* User zooms the screen */) {
mMyMatrix.postScale(deltaScale, deltaScale);
}
if (/* User taps the grid */) {
// Transform the original grid corners to where they
// are on the screen (panned and zoomed).
mMyMatrix.mapPoints(mTransformedGridCorners, mOriginalGridCorners);
float gridWidth = mTransformedGridCorners[2] - mTransformedGridCorners[0];
float gridHeight = mTransformedGridCorners[3] - mTransformedGridCorners[1];
// Get the x and y coordinate of the tap inside the
// grid, between 0 and 1.
float x = (event.getX() - mTransformedGridCorners[0]) / gridWidth;
float y = (event.getY() - mTransformedGridCorners[1]) / gridHeight;
// To get the tapped grid cell.
int column = (int)(x * nbrColumns);
int row = (int)(y * nbrRows);
// Or to get the tapped exact pixel in the original grid.
int pixelX = (int)(x * getWidth());
int pixelY = (int)(y * getHeight());
}
return true;
}
@Override
protected void onDraw(Canvas canvas) {
// Each frame, transform your canvas with the matrix.
canvas.save();
canvas.concat(mMyMatrix);
// Draw grid.
grid.draw(canvas);
canvas.restore();
}
或者获取矩阵的已弃用方法,它仍然有效并且可能需要更少的更改:
@Override
protected void onDraw(Canvas canvas) {
canvas.save();
// Transform canvas and draw the grid.
grid.draw(canvas);
// Get the matrix from canvas. Can be used to transform
// corners on the next touch event.
canvas.getMatrix(mMyMatrix);
canvas.restore();
}
关于java - 通过在自定义 View 的 onDraw 中设置单位矩阵来抵消 Canvas ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17100355/
我有 2 张 table ; item_in(item_id,unit) item_out(item_id,unit) 现在假设我想知道为每个项目插入了多少个单元,我只是查询 select sum(u
API 浏览器中有 3 个速率限制类别: 如果我使用 Youtube 数据 API(其中跟随 implicit OAuth grant flow)创建客户端 Web 应用程序,我是否仍被限制为总共 1
我正在使用一个 postgresql 表,该表包含许多带有 GEOMETRY(Point, 4326) 的行。使用 ST_SnapToGrid 函数和 DISTINCT 选择,我只根据显示的 map
我对 C++ 和 Cppunit 都很陌生。我正在尝试编译一个小的 cppunit 测试。但是,我没有成功。 qwerty@qwerty:~/chessgame/src$ g++ -Wall Coor
我注意到 REM 单位可用于元素的大小,而不仅仅是字体大小。并且对 HTML 字体大小属性非常有用。 html { font-size:1vw } @media all and (max-width:
我试图在 Shapely 中找到线串的长度(以米为单位),但似乎无法达到预期的结果。几乎可以肯定我在坐标系方面犯了一些错误,但我无法弄清楚。 这是单行的一些简化代码: from shapely.geo
对于大量的物种数据集,我试图计算给定月份集的圆形平均值,例如对于从 3 月到 7 月开花的物种,我想知道开花的平均月份(即 5 月),以及围绕平均值的方差。 给定月份是循环的,因此 12 月到 2 月
我还应该在单元测试中释放对象吗? 我注意到在Apple的“iPhoneUnitTests”示例项目中,设置方法中的对象是[[object alloc] init],但从未在单元测试中的任何地方发布?
我目前正在使用 OpenGL 进行开发,并使用米作为我自己的单位,即 20 厘米宽的三角形为 0.2。然而 OpenGL 似乎对这些数字进行了舍入,最终的形状并不完全符合我的意愿。这在 OpenGL
我的问题与对信号进行频谱分析或将信号放入 FFT 并使用合适的数值包解释结果的物理意义有关, 具体: 获取一个信号,例如时变电压 v(t) 将其放入 FFT 中(您将得到复数序列) 现在取模 (abs
在深入研究代码后,我意识到 Fabricjs Text 对象的 fontSize 是在 PIXELS 中测量的。在我的项目中,有时我需要使用点而不是像素。 当指定单位时,我只在代码中找到一个位置,此片
在我的应用程序中,我尝试使用,RentModel.find({prop_location : { $near : [msg.lat, msg.lng],$maxDistance : 500}},函数(
我正在开发我的第一个 LibGdx (Scene2d + Box2d) 游戏,这对我来说是一个全新的领域,但仍然对一些事情感到有点困惑,尤其是关于单位。已经看到了几种不同的处理方法,但仍然不确定哪种方
我正在寻找一个 MySQL 查询(子查询很好),它将以下列格式获取过去一年中每个订单的单位分布: units_per_order | number_of_orders |
我正在使用 Highcharts生成折线图。 我遇到了 numberFormat 的问题: var test = 15975000; numberFormat(test, 0,',','.'); 结果
我正在尝试创建一些用户定义的类型来表示单位,以便我可以强类型化函数参数。例如,长度为毫米,速度为毫米每秒,加速度为毫米每秒等。 到目前为止我已经这样做了: template struct Value
谁能解释一下最低精度的 ULP 单位?我有如下定义,但还是不清楚 “表示分数时的误差大小与存储的数字大小成正比。ULP 或最小精度单位定义了存储数字时可以获得的最大误差。存储的数字越大,ULP 越大”
我有一张卡片图像,我需要重复它 30 次,每次我想申请一张特定卡片的左侧位置时,它会与卡片重叠,然后再停留在一副牌的位置上。 问题是,当我将左侧位置应用于图像卡片时,它会将相同的左侧位置应用于所有卡片
有没有办法用php代码更改每个滚动条的大小。 说明:当我向下滚动时,它会下降x(50~)像素,我想将x改为20。 编辑:这是我的代码。。。 Excel "; $i=1; wh
我不知道下面的想法是否可行或不能概括它,但我想将每个计算值四舍五入到 100 单位四舍五入。 例子: double x; int x_final; ... if (x<400) x_final=400
我是一名优秀的程序员,十分优秀!