有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在Android中与GLSurfaceView通信?

在我的应用程序中,我实现了一个名为BatteryBroadcastReceiver的定制广播接收器,其目的是通知我电池的充电状态(“充电器已连接”、“充电器已断开”)。我在我的主要活动中注册了这款BatteryBroadcastReceiver,它与祝酒器配合使用效果很好。另一方面,我有一个带有渲染器的GLSurfaceView,它也很好用

现在我需要GLSurfaceView来监听BatteryBroadcastReceiver,以便通过GLSurfaceView调用我的自定义渲染器方法(Renderer.setMotion(“somestring”))

这是BatteryBroadcastReceiver课程:

public class BatteryBroadcastReceiver extends BroadcastReceiver {

    public BatteryBroadcastReceiver() {
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        int batteryLevel = intent.getIntExtra(
                BatteryManager.EXTRA_LEVEL, 0);
        int maxLevel = intent
                .getIntExtra(BatteryManager.EXTRA_SCALE, 0);

        float batteryPercentage = ((float) batteryLevel / (float) maxLevel) * 100;

        if (intent.getAction().equals(Intent.ACTION_POWER_CONNECTED)) {
            Toast.makeText(context, "Conectaste el cargador", Toast.LENGTH_SHORT).show();
        } else {
            intent.getAction().equals(Intent.ACTION_POWER_DISCONNECTED);
            Toast.makeText(context, "Desconectaste el cargador", Toast.LENGTH_SHORT).show();
        }
    }
}

这是主要的活动:

public class MainActivity extends AppCompatActivity {

    private GLSurfaceView glsv_main;
    private BatteryBroadcastReceiver receiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        glsv_main = new SampleGLSurfaceView(this);

        Live2D.init();
        setContentView(glsv_main);

    }

    @Override
    protected void onResume() {
        super.onResume();

        receiver = new BatteryBroadcastReceiver();

        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(Intent.ACTION_POWER_CONNECTED);
        intentFilter.addAction(Intent.ACTION_POWER_DISCONNECTED);
        registerReceiver(receiver, intentFilter);
    }

    @Override
    protected void onPause() {
        super.onPause();

        unregisterReceiver(receiver);
    }
}

这是GLSurfaceView类:

public class SampleGLSurfaceView extends GLSurfaceView
{
    private SampleGLRenderer renderer ;

    public SampleGLSurfaceView(Context context)
    {
        super(context);

        renderer = new SampleGLRenderer() ;
        setRenderer(renderer) ;
    }

    @Override
    public boolean onTouchEvent(MotionEvent e) {

        switch (e.getAction()) {
            case MotionEvent.ACTION_UP:
                renderer.setMotion("asuna/common/motions/surprise/0.mtn");
        }
        return true;
    }

    class SampleGLRenderer implements Renderer
    {
        private Live2DModelAndroid live2DModel;
        private Live2DMotion live2DMotion;
        private MotionQueueManager motionManager = new MotionQueueManager();

        private final String MODEL_PATH = "asuna/07/model.moc" ;
        private final String TEXTURE_PATHS[] =
                {
                        "asuna/common/textures/0.png",
                        "asuna/common/textures/1.png",
                        "asuna/07/textures/2.png",
                        "asuna/07/textures/3.png",
                } ;
        private final String PHYSICS_PATH = "asuna/07/physics.json";
        //private final String POSE_PATH = "haru/haru.pose.json";
        private final String MOTION_IDLE[] =
                {
                        "asuna/common/motions/idle/0.mtn",
                        "asuna/common/motions/idle/1.mtn",
                        "asuna/common/motions/idle/2.mtn",
                } ;
        private final String EXPRESSION_PATHS[] =
                {
                        "asuna/common/expressions/ANGRY.json",
                        "asuna/common/expressions/FUN.json",
                        "asuna/common/expressions/FUN_HANIKAMI.json",
                        "asuna/common/expressions/FUN_MAX.json",
                        "asuna/common/expressions/FUN_SMILE.json",
                        "asuna/common/expressions/FUN_WARM.json",
                        "asuna/common/expressions/NOMAL.json",
                        "asuna/common/expressions/SAD.json",
                        "asuna/common/expressions/SLEEP.json",
                        "asuna/common/expressions/SURPRISE.json",
                } ;
        //private final String SOUND_PATHS[] = {};

        @Override
        public void onDrawFrame(GL10 gl)
        {
            gl.glMatrixMode(GL10.GL_MODELVIEW ) ;
            gl.glLoadIdentity() ;
            gl.glClear( GL10.GL_COLOR_BUFFER_BIT ) ;
            gl.glEnable( GL10.GL_BLEND ) ;
            gl.glBlendFunc( GL10.GL_ONE , GL10.GL_ONE_MINUS_SRC_ALPHA ) ;
            gl.glDisable( GL10.GL_DEPTH_TEST ) ;
            gl.glDisable( GL10.GL_CULL_FACE ) ;

            live2DModel.loadParam();

            if(motionManager.isFinished())
            {
                setRamdomIdle();
                //If you have only 1 motion
                motionManager.startMotion(live2DMotion, false); // Play the motion inside live2Dmotion variable
            }
            else
            {
                motionManager.updateParam(live2DModel); // Asocia el motion que esta corriendo actualmente con el modelo
            }
            motionManager.updateParam(live2DModel);

            live2DModel.saveParam();

            live2DModel.setGL( gl ) ;
            live2DModel.update() ;
            live2DModel.draw() ;

        }

        @Override
        public void onSurfaceChanged(GL10 gl, int width, int height)
        {
            gl.glViewport( 0 , 0 , width , height ) ;

            gl.glMatrixMode( GL10.GL_PROJECTION ) ;
            gl.glLoadIdentity() ;

            float modelWidth = live2DModel.getCanvasWidth();
            float visibleWidth = modelWidth * (3.0f/4.0f);
            float margin = 0.5f * ( modelWidth/4.0f ) ;

            gl.glOrthof(margin, margin+visibleWidth, visibleWidth*height/width, 0, 0.5f, -0.5f);
        }

        @Override
        public void onSurfaceCreated(GL10 gl, EGLConfig config)
        {
            try
            {
                InputStream in = getContext().getAssets().open(MODEL_PATH);
                live2DModel = Live2DModelAndroid.loadModel(in);
                in.close() ;
                // live2DModel configuration ARM_L_B_001
                //live2DModel.setPartsOpacity("PARTS_01_ARM_L_A_001", (float) 0.0);
                //live2DModel.setPartsOpacity("PARTS_01_ARM_R_A_001", (float) 0.0);
                // --------------------------

                // live2DMotion configuration
                // --------------------------

                for (int i = 0; i < TEXTURE_PATHS.length; i++ )
                {
                    InputStream tin = getContext().getAssets().open( TEXTURE_PATHS[i] ) ;
                    int texNo = UtOpenGL.loadTexture(gl , tin , true );
                    live2DModel.setTexture( i , texNo );

                    //InputStream min = getContext().getAssets().open( MOTION_PATHS[i] ) ;
                    //int motNo = UtOpenGL.loadTexture(gl , tin , true ) ;
                    //live2DMotion.loadMotion(min) ;

                }

            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }

        public void setMotion(String mtn) {
            try {
                InputStream min = getContext().getAssets().open(mtn);
                live2DMotion = Live2DMotion.loadMotion(min);
                min.close();
                motionManager.startMotion(live2DMotion, false);
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }

        public void setRamdomIdle() {
            Random rand = new Random();
            int  n = rand.nextInt(3);
            try {
                InputStream min = getContext().getAssets().open(MOTION_IDLE[n]);
                live2DMotion = Live2DMotion.loadMotion(min);
                min.close();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
    }
}

共 (0) 个答案