Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2112416
  • 博文数量: 438
  • 博客积分: 3871
  • 博客等级: 中校
  • 技术积分: 6075
  • 用 户 组: 普通用户
  • 注册时间: 2011-09-10 00:11
个人简介

邮箱: wangcong02345@163.com

文章分类

全部博文(438)

文章存档

2017年(15)

2016年(119)

2015年(91)

2014年(62)

2013年(56)

2012年(79)

2011年(16)

分类: Android平台

2017-03-28 11:31:08

参考,这是一位德国人Martin写的文章,年代比较久远了.但是入门还是比较好的
1. 工程的建立
eclipse --> 新建一项目  com.example.gldemo
SDK 都选API 19: android 4.4 (KitKat)  
2. 代码分析
MainActivity.java
  1. package com.example.gldemo;

  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.view.Menu;
  5. import android.view.MenuItem;

  6. public class MainActivity extends Activity {
  7.     private VortexView _vortexView;
  8.     @Override
  9.     protected void onCreate(Bundle savedInstanceState) {
  10.         super.onCreate(savedInstanceState);
  11.         _vortexView = new VortexView(this);    -->使用opengl的view
  12.         setContentView(_vortexView);
  13.         //setContentView(R.layout.activity_main);
  14.     }

  15.     @Override
  16.     public boolean onCreateOptionsMenu(Menu menu) {
  17.         // Inflate the menu; this adds items to the action bar if it is present.
  18.         getMenuInflater().inflate(R.menu.main, menu);
  19.         return true;
  20.     }

  21.     @Override
  22.     public boolean onOptionsItemSelected(MenuItem item) {
  23.         // Handle action bar item clicks here. The action bar will
  24.         // automatically handle clicks on the Home/Up button, so long
  25.         // as you specify a parent activity in AndroidManifest.xml.
  26.         int id = item.getItemId();
  27.         if (id == R.id.action_settings) {
  28.             return true;
  29.         }
  30.         return super.onOptionsItemSelected(item);
  31.     }
  32. }
VortexView.java
  1. package com.example.gldemo;

  2. import android.content.Context;
  3. import android.opengl.GLSurfaceView;
  4. import android.view.MotionEvent;

  5. public class VortexView extends GLSurfaceView{    -->VortexView继承了GLSurfaceView
  6.     private static final String LOG_TAG="VortexView";
  7.     private VortexRenderer _renderer;
  8.     public VortexView (Context context){
  9.         super(context);
  10.         _renderer = new VortexRenderer();    -->使用opengl的view
  11.         setRenderer(_renderer);              -->Renderer负责OpenGL call来render一个帧
  12.     }

  13.     public boolean onTouchEvent(final MotionEvent event){
  14.         queueEvent(new Runnable(){
  15.             public void run(){
  16.                 _renderer.setColor(event.getX()/getWidth(),
  17.                                  event.getY()/getHeight(), 1.0f);    
  18.             }
  19.         });
  20.         return true;
  21.     }
  22. }
VortexRenderer.java
  1. package com.example.gldemo;

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

  4. import android.opengl.GLSurfaceView;

  5. public class VortexRenderer implements GLSurfaceView.Renderer {
  6.     private static final String LOG_TAG="VortexRenderer";
  7.     private float _red = 0.9f;
  8.     private float _green = 0.9f;
  9.     private float _blue = 0.9f;
  10.     
  11.     @Override
  12.     public void onSurfaceCreated(GL10 gl, EGLConfig config) {
  13.         // TODO Auto-generated method stub    
  14.         
  15.     }
  16.     
  17.     @Override
  18.     public void onSurfaceChanged(GL10 gl, int w, int h){
  19.         gl.glViewport(0, 0, w, h);
  20.     }
  21.     
  22.     @Override
  23.     public void onDrawFrame(GL10 gl ){
  24.         gl.glClearColor(_red, _green, _blue, 1.0f);     -->用定义的rgb来设设置背景色
  25.         gl.glClear(GL10.GL_COLOR_BUFFER_BIT);           -->清空buffer
  26.     }
  27.     public void setColor(float r, float g, float b) {   -->自己写的一个方法,来改变rgb颜色
  28.         _red = r;                                       -->这样在onDrawFreme调用后就会在屏幕上显示出来
  29.         _green = g;
  30.         _blue = b;
  31.     }
  32.     
  33. }
在render中实现3个方法:
onSurfaceCreated()  --> 在surface创建后调用
onSurfaceChanged() --> 在surface发生改变后调用,如从竖屏切换到横屏时
onDrawFrame()        --> 当调用一个画图方法的时候调用
3. 运行代码后,在屏幕的不同位置点击,屏幕会显示不同的颜色
glDemo.rar(下载后不用改名)
阅读(1309) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~