- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 Android 的新手,我在使用这个 tutorial 时遇到了一些麻烦.我得到的错误是 getHeight/Width/requestRender 对于 HelloOpenGLES20Activity 的类型是未定义的。包含错误的代码部分需要在教程末尾添加。我搜索了大约一个小时,但处理 getHeight 的大部分错误都是关于它返回 0。
这是类 (HelloOpenGLES20Activity):
package com.example.HelloOpenGLES20;
import android.app.Activity;
import android.content.Context;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.view.Display;
import android.view.MotionEvent;
public class HelloOpenGLES20Activity extends Activity {
private GLSurfaceView mGLView;
private final float TOUCH_SCALE_FACTOR = 180.0f / 320;
private HelloOpenGLES20Renderer mRenderer;
private float mPreviousX;
private float mPreviousY;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Create a GLSurfaceView instance and set it
// as the ContentView for this Activity
mGLView = new HelloOpenGLES20SurfaceView(this);
setContentView(mGLView);
}
@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.mAngle += (dx + dy) * TOUCH_SCALE_FACTOR;
requestRender();
}
mPreviousX = x;
mPreviousY = y;
return true;
}
@Override
protected void onPause() {
super.onPause();
// The following call pauses the rendering thread.
// If your OpenGL application is memory intensive,
// you should consider de-allocating objects that
// consume significant memory here.
mGLView.onPause();
}
@Override
protected void onResume() {
super.onResume();
// The following call resumes a paused rendering thread.
// If you de-allocated graphic objects for onPause()
// this is a good place to re-allocate them.
mGLView.onResume();
}
class HelloOpenGLES20SurfaceView extends GLSurfaceView {
private HelloOpenGLES20Renderer mRenderer;
public HelloOpenGLES20SurfaceView(Context context){
super(context);
// Create an OpenGL ES 2.0 context.
setEGLContextClientVersion(2);
// set the mRenderer member
mRenderer = new HelloOpenGLES20Renderer();
setRenderer(mRenderer);
// Render the view only when there is a change
setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
}
}
}
这是 HelloOpenGLES20Renderer 类:
package com.example.HelloOpenGLES20;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import android.opengl.GLES20;
import android.opengl.GLSurfaceView;
import android.opengl.Matrix;
public class HelloOpenGLES20Renderer implements GLSurfaceView.Renderer {
public void onSurfaceCreated(GL10 unused, EGLConfig config) {
// Set the background frame color
GLES20.glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
// initialize the triangle vertex array
initShapes();
int vertexShader = loadShader(GLES20.GL_VERTEX_SHADER, vertexShaderCode);
int fragmentShader = loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentShaderCode);
mProgram = GLES20.glCreateProgram(); // create empty OpenGL Program
GLES20.glAttachShader(mProgram, vertexShader); // add the vertex shader to program
GLES20.glAttachShader(mProgram, fragmentShader); // add the fragment shader to program
GLES20.glLinkProgram(mProgram); // creates OpenGL program executables
// get handle to the vertex shader's vPosition member
maPositionHandle = GLES20.glGetAttribLocation(mProgram, "vPosition");
}
public void onDrawFrame(GL10 unused) {
// Redraw background color
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
//Add program to OpenGL environment
GLES20.glUseProgram(mProgram);
//Prepare the triangle data
GLES20.glVertexAttribPointer(maPositionHandle, 3, GLES20.GL_FLOAT, false, 12, triangleVB);
GLES20.glEnableVertexAttribArray(maPositionHandle);
//Create a rotation for the triangle (Boring! Comment this out:)
//long time = SystemClock.uptimeMillis() % 4000L;
//float angle = 0.090f * ((int) time);
//Use the mAngle member as the rotation value
Matrix.setRotateM(mMMatrix, 0, mAngle, 0, 0, 1.0f);
Matrix.multiplyMM(mMVPMatrix, 0, mVMatrix, 0, mMMatrix, 0);
Matrix.multiplyMM(mMVPMatrix, 0, mProjMatrix, 0, mMVPMatrix, 0);
//Apply a ModelView Projection transformation
GLES20.glUniformMatrix4fv(muMVPMatrixHandle, 1, false, mMVPMatrix, 0);
//Draw the triangle
GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, 3);
}
public void onSurfaceChanged(GL10 unused, int width, int height) {
GLES20.glViewport(0, 0, width, height);
float ratio = (float) width / height;
// this projection matrix is applied to object coodinates
// in the onDrawFrame() method
Matrix.frustumM(mProjMatrix, 0, -ratio, ratio, -1, 1, 3, 7);
muMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");
Matrix.setLookAtM(mVMatrix, 0, 0, 0, -3, 0f, 0f, 0f, 0f, 1.0f, 0.0f);
}
private FloatBuffer triangleVB;
private int mProgram;
private int maPositionHandle;
private int muMVPMatrixHandle;
private float[] mMVPMatrix = new float[16];
private float[] mMMatrix = new float[16];
private float[] mVMatrix = new float[16];
private float[] mProjMatrix = new float[16];
public float mAngle;
private void initShapes(){
float triangleCoords[] = {
// X, Y, Z
-0.5f, -0.25f, 0,
0.5f, -0.25f, 0,
0.0f, 0.559016994f, 0
};
// initialize vertex Buffer for triangle
ByteBuffer vbb = ByteBuffer.allocateDirect(
// (# of coordinate values * 4 bytes per float)
triangleCoords.length * 4);
vbb.order(ByteOrder.nativeOrder());// use the device hardware's native byte order
triangleVB = vbb.asFloatBuffer(); // create a floating point buffer from the ByteBuffer
triangleVB.put(triangleCoords); // add the coordinates to the FloatBuffer
triangleVB.position(0); // set the buffer to read the first coordinate
}
private final String vertexShaderCode =
//This matrix member variable provides a hook to manipulate
//the coordinates of the objects that use this vertex shader
"uniform mat4 uMVPMatrix; \n" +
"attribute vec4 vPosition; \n" +
"void main(){ \n" +
//the matrix must be included as a modifier of gl_Position
" gl_Position = uMVPMatrix * vPosition; \n" +
"} \n";
private final String fragmentShaderCode =
"precision mediump float; \n" +
"void main(){ \n" +
" gl_FragColor = vec4 (0.63671875, 0.76953125, 0.22265625, 1.0); \n" +
"} \n";
private int loadShader(int type, String shaderCode){
// create a vertex shader type (GLES20.GL_VERTEX_SHADER)
// or a fragment shader type (GLES20.GL_FRAGMENT_SHADER)
int shader = GLES20.glCreateShader(type);
// add the source code to the shader and compile it
GLES20.glShaderSource(shader, shaderCode);
GLES20.glCompileShader(shader);
return shader;
}
}
提前致谢。
最佳答案
尝试使用以下代码获取屏幕的宽度:
WindowManager w = getWindowManager();
Display d = w.getDefaultDisplay();
int width = d.getWidth();
int height = d.getHeight();
再试一遍,ontouchevent 放错了
希望对您有所帮助
关于android - getHeight 的类型未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9457610/
我有两个使用相同 JavaScript 文件的网站。一个是 Joomla 站点,另一个是该站点布局的副本。 JavaScript 包含以下两行: var header = document.getEl
我是 Android 的新手,我在使用这个 tutorial 时遇到了一些麻烦.我得到的错误是 getHeight/Width/requestRender 对于 HelloOpenGLES20Acti
当我运行计算二叉搜索树高度的方法的代码时,它会导致堆栈溢出错误,但仅限于具有多个节点的树(我的程序中的 BSTElements)。我已了解到这是由于错误的递归调用造成的,但无法识别我的代码中的问题。
这是代码: import acm.program.*; public class test extends GraphicsProgram{ public test()
我正在 xml 文件中创建一个 imageLyout,使用这段代码这段代码: 当我在 drawbale-hdpi 属性中输入图像时,我看到图像尺寸为:515 * 348 像素。 当我使用
我是 libgdx 的新手,一直在练习使用 ShapeRenderer。我试图在屏幕底部放置一个矩形,但 Gdx.graphics.getHeight() 函数返回的值未覆盖整个长度。如果我将矩形的坐
我正在制作一个围绕框架移动的弹跳立方体。为了检测框架的边界,我使用了 JFrame 中 JComponent 的 getHeight() 和 getWidth() 并计算可能的边界,以便立方体可以反弹
我正在 relativeLayout 中动态创建一些 ImageView,但我需要根据屏幕的高度和宽度改变大小。在我设置高度时,尚未创建 View 和布局,这意味着 getHeight() 和 get
这些方法在动态检索时返回正确的值,例如。在 onCreate() 中,但唯一的异常(exception)是当维度在 xml 布局文件中指定为 undefined 时,像 'wrap content'
本文整理了Java中android.graphics.YuvImage.getHeight()方法的一些代码示例,展示了YuvImage.getHeight()的具体用法。这些代码示例主要来源于Git
本文整理了Java中am.widget.zxingscanview.ZxingScanView.getHeight()方法的一些代码示例,展示了ZxingScanView.getHeight()的具体
本文整理了Java中am.widget.zxingscanview.ZxingForegroundView.getHeight()方法的一些代码示例,展示了ZxingForegroundView.ge
我是 Android Studio 的新手,在与 BitmapFactory 相关的任务中,我尤其感到困惑。 我遇到的问题如下。当我在可绘制文件夹下插入一个尺寸为 (578x496) 的 jpg 文件
关于Scriptaculous的Effect.Move演示可以参见 here : a#move_demo { background:#fa0000; color:#fff; padding:5p
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.background); 我的 background.png
我正在使用 Actionbarsherlock,我想在操作栏的正下方放置一个 PopupWindow。使用 showAtLocation() 获取 x 和 y 偏移量,因此理想情况下 y 偏移量将是操
我想以编程方式获取 TextView 的高度或至少行数,但日志显示为 0。有什么问题吗?这是我的代码: public void onCreate(Bundle savedInstanceState)
我已经在代码中初始化了 TextView(即,没有从布局中初始化它) TextView v = new TextView(getApplicationContext()); 然后我设置文
您好,我是 Android 的新手,我正面临这个问题:如何获取放置在相对布局中的 View 的高度/宽度?我尝试了 getHeight() 和 getWidth() 并且都返回了 0。 我需要知道这个
我在尝试为状态栏添加颜色时发现了影响相对布局高度的问题。 adding Flag影响相对布局 rootView 高度的计算。 private void changeStatusBarColor(){
我是一名优秀的程序员,十分优秀!