简体   繁体   English

有人向我解释了来自 android 开发者网站的代码

[英]somebody explain to me code from android developers site

Im following this training tutorial on this site http://developer.android.com/training/graphics/opengl/touch.html#listener .我在这个网站http://developer.android.com/training/graphics/opengl/touch.html#listener上关注这个培训教程。

The link shows an example of how to respond to touch events and rotate a triangle.该链接显示了如何响应触摸事件和旋转三角形的示例。 I have the code all setup and working i just don't understand this portion of code:我有所有设置和工作的代码我只是不明白这部分代码:

private final float TOUCH_SCALE_FACTOR = 180.0f / 320;
private float mPreviousX;
private float mPreviousY;

@Override
public boolean onTouchEvent(MotionEvent e) {
// MotionEvent reports input details from the touch screen
// and other input controls. In this case, you are only
// interested in events where the touch position changed.

float x = e.getX();
float y = e.getY();

switch (e.getAction()) {
    case MotionEvent.ACTION_MOVE:

        float dx = x - mPreviousX;
        float dy = y - mPreviousY;

        // reverse direction of rotation above the mid-line
        if (y > getHeight() / 2) {
          dx = dx * -1 ;
        }

        // reverse direction of rotation to left of the mid-line
        if (x < getWidth() / 2) {
          dy = dy * -1 ;
        }

        mRenderer.setAngle(
                mRenderer.getAngle() +
                ((dx + dy) * TOUCH_SCALE_FACTOR));
        requestRender();
}

mPreviousX = x;
mPreviousY = y;
return true;
}

getAngle and setAngle are sort of utility methods in mRenderer object as can be further seen in the example shown in the link. getAngle 和 setAngle 是 mRenderer 对象中的实用方法,可以在链接中显示的示例中进一步看到。

I need an explanation of how this code calculates the angle exactly, how can it take the increments dx and dy and some them up and use that as an angle and what is happening with getHeight and getWidth arent these the height and width of the whole screen?我需要解释一下这段代码是如何准确计算角度的,它如何将增量 dx 和 dy 以及一些增量用作角度,以及 getHeight 和 getWidth 发生的事情不是整个屏幕的高度和宽度? i hope I've been clear enough, thanks.我希望我已经足够清楚了,谢谢。

Although this question is massively down-voted it is unfair to leave out a very common openGL developer problem which is that most (almost) all examples and tutorials are extremely poorly designed which misleads many developers in early stage or even makes them lose hope of ever understanding how this works.尽管这个问题被大量否决,但忽略一个非常常见的 openGL 开发人员问题是不公平的,即大多数(几乎)所有示例和教程的设计都非常糟糕,这在早期误导了许多开发人员,甚至使他们失去希望了解这是如何工作的。

The reason for that is that a basic openGL setup requires a lot of knowledge and is massive in code size.原因是基本的 openGL 设置需要大量知识并且代码量很大。 A proficient developer will break the code to multiple classes such as shader, context, buffer, objects, matrices... while more or less all the examples or tutorials will show everything in the same class/file simply because it is easier to navigate for a fresh developer.熟练的开发人员会将代码分解为多个类,例如着色器、上下文、缓冲区、对象、矩阵……而所有示例或教程或多或少都会在同一类/文件中显示所有内容,只是因为它更易于导航一个新的开发者。 What this leads to is that authors will try to find a smallest (number of lines) solution to a problem which is not directly bound to the article you are reading.这导致作者将尝试为与您正在阅读的文章没有直接关系的问题找到最小(行数)的解决方案。

As for this example you are using: I do not know the whole example or what it looks like but just seeing the method you posted it would suggest you have some shape in the middle (a triangle as mentioned) which is rotated by dragging through the screen.至于你正在使用的这个例子:我不知道整个例子或它的样子,但只要看到你发布的方法就会建议你在中间有一些形状(提到的三角形),它通过拖动旋转屏幕。

What you would normally do is get the vector from the center to the finger (x-getWidth(), y-getHeight()) which can then be used to compute the angle via methods such as atan2f and the angle is then set to the object.您通常要做的是获取从中心到手指的向量(x-getWidth(), y-getHeight()) ,然后可以使用它通过atan2f等方法计算角度,然后将角度设置为目的。 This is actually a way shorter way as well but it would require some knowledge of math and definitely some explanation on the code itself.这实际上也是一种更短的方法,但它需要一些数学知识,并且肯定需要对代码本身进行一些解释。

So instead of that the author used a very simple procedure which says by dragging vertically or horizontally the object will rotate by some angle and the rotation direction depends on what part of the screen you are dragging on.因此,作者使用了一个非常简单的程序,即通过垂直或水平拖动对象将旋转某个角度,旋转方向取决于您拖动的屏幕部分。 Further the author actually added both vertical and horizontal rotation which in the end is simply a sum of 2 values.此外,作者实际上添加了垂直和水平旋转,最终只是 2 个值的总和。

I agree this may be extremely confusing to understand for developers with little experience in such coding.我同意这对于在此类编码方面几乎没有经验的开发人员来说可能非常难以理解。 If nothing else this answer should be something to consider for all of you possible future authors of tutorials and examples.如果不出意外,这个答案应该是你们所有未来可能的教程和示例作者都应该考虑的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM