4

我在整个互联网上搜索了几天以找到解决方案,但一无所获。

我想获取有关 Android 设备(如 RENDERER、VENDOR 和 VERSION)上 GPU 的主要信息,并能够在定义的 XML 布局上的 textview 上打印它。我尝试了很多方法,但对我没有任何帮助。每个人都说要使用这个:

Log.d("GL", "GL_RENDERER = " + gl.glGetString(GL10.GL_RENDERER));
Log.d("GL", "GL_VENDOR = " + gl.glGetString(GL10.GL_VENDOR));
Log.d("GL", "GL_VERSION = " + gl.glGetString(GL10.GL_VERSION));
Log.i("GL", "GL_EXTENSIONS = " + gl.glGetString(GL10.GL_EXTENSIONS));

我实现了下一个类:

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mGLView = new GLSurfaceView(this);
    mGLView.setRenderer(new ClearRenderer());
    setContentView(mGLView);

}


private GLSurfaceView mGLView;
static class ClearRenderer implements GLSurfaceView.Renderer {

    public final static String renderer = null;
    Random aleatorio = new Random();

    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        float r = aleatorio.nextFloat();
        float g = aleatorio.nextFloat();
        float b = aleatorio.nextFloat();
        gl.glClearColor(r, g, b, 1.0f);

        Log.d("GL", "GL_RENDERER = " + gl.glGetString(GL10.GL_RENDERER));
        Log.d("GL", "GL_VENDOR = " + gl.glGetString(GL10.GL_VENDOR));
        Log.d("GL", "GL_VERSION = " + gl.glGetString(GL10.GL_VERSION));
        Log.i("GL", "GL_EXTENSIONS = " + gl.glGetString(GL10.GL_EXTENSIONS));

    }

    public void onSurfaceChanged(GL10 gl, int w, int h) {           
    }

    public void onDrawFrame(GL10 gl) {
        gl.glClear(gl.GL_COLOR_BUFFER_BIT);         
    }
}
}

效果很好,但我不知道如何将这些日志放入文本视图。将 ContentView 设置为我的 GLSurfaceView 我不知道如何在其中使用 TextView。

我也尝试过使用:

String renderer = gl.glGetString(GL10.GL_VENDOR);
Log.d("GL", renderer);

在以前的日志所在的同一个地方,这也很好,我可以在 LogCat 中看到供应商的正确价值。

但是,我仍然不知道如何将此值传递给 textview(例如 tv.setText(renderer))并在正常布局上使用它。

如果有人能用一个简单的例子帮助我解决这个问题,我将不胜感激。考虑到我从未使用过 OpenGL,我只想获取有关它的信息。如果您告诉我另一种方式(如果可能,更容易:D)获取该信息,我也接受。

提前致谢。

4

3 回答 3

4

这有点棘手,你不需要GlSurfaceView作为 root ,否则你不会TextView在布局中预先定义。

解决方案:

这就是我设法做到的,我已经添加GlSurfaceView了我的预定义布局,然后在我完成信息后将其删除。

您的xml文件将与预定义类似TextView

gpu_info.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/rlRoot"
android:layout_width="match_parent"
android:layout_height="match_parent" >


<TextView 
    android:id="@+id/tvVendor"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#FF0000"
    />

您的Activity文件将如下所示:

public class GpuInfoActivity extends Activity{

private RelativeLayout rlRoot;
private TextView tvVendor;
private String mVendor;
private GLSurfaceView mGlSurfaceView;
private GLSurfaceView.Renderer mGlRenderer = new Renderer() {

    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {// TODO Auto-generated method stub
        Log.d(TAG, "gl renderer: "+gl.glGetString(GL10.GL_RENDERER));
        Log.d(TAG, "gl vendor: "+gl.glGetString(GL10.GL_VENDOR));
        Log.d(TAG, "gl version: "+gl.glGetString(GL10.GL_VERSION));
        Log.d(TAG, "gl extensions: "+gl.glGetString(GL10.GL_EXTENSIONS));

        mVendor = gl.glGetString(GL10.GL_VENDOR);
        runOnUiThread(new Runnable() {

            @Override
            public void run() {
                tvVendor.setText(mVendor);
                rlRoot.removeView(mGlSurfaceView);

            }
        });}

    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onDrawFrame(GL10 gl) {
        // TODO Auto-generated method stub

    }
};


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.gpu_info);


    rlRoot = (RelativeLayout)findViewById(R.id.rlRoot);
    tvVendor = (TextView)findViewById(R.id.tvVendor);

    mGlSurfaceView = new GLSurfaceView(this);
    mGlSurfaceView.setRenderer(mGlRenderer);

    rlRoot.addView(mGlSurfaceView);

    }
}

我希望它会有所帮助!

于 2013-12-10T13:07:51.913 回答
2

最后,在解决了这个问题之后,我找到了一个可能的解决方案。也许它不是最好的,但我没有其他的,它奏效了。

我使用共享偏好来存储信息。我使用已实现的 GLSurfaceView 创建了一个启动器活动,延迟为 3 秒(足以存储所有字符串),在此延迟之后,我需要的活动开始。

启动器活动如下所示:

package system.info.to.txt;

import java.util.Random;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.util.Log;

public class RendererLoader extends Activity {

private static SharedPreferences prefs;
private static SharedPreferences.Editor editor;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    prefs = this.getSharedPreferences("GPUinfo", Context.MODE_PRIVATE);
    mGLView = new GLSurfaceView(this);
    mGLView.setRenderer(new ClearRenderer());
    setContentView(mGLView);    

    final Intent intent = new Intent(this, Info.class);
    new CountDownTimer(3000, 9999)
         {
           public void onTick(long millisUntilFinished) {
                     // Not used
           }
           public void onFinish() {             
           startActivity(intent);
           finish();
           }
        }.start();  
}


private GLSurfaceView mGLView;
class ClearRenderer implements GLSurfaceView.Renderer {

    Random aleatorio = new Random();

    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        float r = aleatorio.nextFloat();
        float g = aleatorio.nextFloat();
        float b = aleatorio.nextFloat();
        gl.glClearColor(r, g, b, 1.0f);

        editor = prefs.edit();
        editor.putString("RENDERER", gl.glGetString(GL10.GL_RENDERER));
        editor.putString("VENDOR", gl.glGetString(GL10.GL_VENDOR));
        editor.putString("VERSION", gl.glGetString(GL10.GL_VERSION));
        editor.putString("EXTENSIONS", gl.glGetString(GL10.GL_EXTENSIONS));
        editor.commit();

    }

    public void onSurfaceChanged(GL10 gl, int w, int h) {

    }

    public void onDrawFrame(GL10 gl) {
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

    }
  }
}

之后,您可以使用以下命令在应用程序中的任意位置检索存储的字符串:

    SharedPreferences prefs =getSharedPreferences("GPUinfo",Context.MODE_PRIVATE);
    String vendor = prefs.getString("VENDOR", null);
    String renderer = prefs.getString("RENDERER", null);
    String version = prefs.getString("VERSION", null);
    String extensions = prefs.getString("EXTENSIONS", null);

我希望这个答案对有同样问题的人有用。任何其他解决方案也将有所帮助。

于 2013-08-27T10:35:56.250 回答
1

我会这样做:

package com.paget96.lspeed.activities;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.opengl.GLSurfaceView;
import android.os.Bundle;

import java.util.Random;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

public class GetGpuInfo extends Activity {

    // Variables
    private static SharedPreferences sharedPreferences;
    private static SharedPreferences.Editor editor;
    private GLSurfaceView mGLView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        sharedPreferences = this.getSharedPreferences("device_info", Context.MODE_PRIVATE);
        mGLView = new GLSurfaceView(this);
        mGLView.setRenderer(new ClearRenderer());
        setContentView(mGLView);

        final Intent intent = new Intent(this, MainActivity.class);
    }


    class ClearRenderer implements GLSurfaceView.Renderer {

        Random random = new Random();

        @Override
        public void onSurfaceCreated(GL10 gl10, EGLConfig eglConfig) {
            float r = random.nextFloat();
            float g = random.nextFloat();
            float b = random.nextFloat();
            gl10.glClearColor(r, g, b, 1.0f);

            editor = sharedPreferences.edit();
            editor.putString("gpu_vendor", gl10.glGetString(GL10.GL_VENDOR));
            editor.putString("gpu_renderer", gl10.glGetString(GL10.GL_RENDERER));
            editor.putString("gpu_version", gl10.glGetString(GL10.GL_VERSION));
            //editor.putString("gpu_extensions", gl10.glGetString(GL10.GL_EXTENSIONS));
            editor.apply();

            finish();
        }

        public void onSurfaceChanged(GL10 gl, int w, int h) {

        }

        public void onDrawFrame(GL10 gl) {
            gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

        }
    }
}

然后通过 Intent 调用相同的活动(一旦传递onSurfaceCreated(...)

final Intent intent = new Intent(this, GetGpuInfo.class);
startActivity(intent);

现在活动 GetGpuInfo 活动已经将 GPU 数据收集到 sharedPreferences 中,因此您可以将它们访问到任何片段等。像这样:

vendor = sharedPreferences.getString("gpu_vendor", getActivity().getString(R.string.unknown));
        renderer = sharedPreferences.getString("gpu_renderer", getActivity().getString(R.string.unknown));
        version = sharedPreferences.getString("gpu_version", getActivity().getString(R.string.unknown));
        extensions = sharedPreferences.getString("gpu_extensions", getActivity().getString(R.string.unknown));

        gpuInfo.setText(String.format("%s\n%s\n%s",
                getString(R.string.vendor, vendor),
                getString(R.string.renderer, renderer),
                getString(R.string.version, version)
                /*,getString(R.string.extensions, extensions)*/));

将 sharedPreferences 的 getter 初始化为 String,然后您可以像我在这里所做的那样将它们放入 TextView。我使用 String.format 是因为在没有字符串集中警告的情况下使用新行更容易。

就是这样,加油

于 2019-08-28T13:30:06.333 回答