当前位置: 编程技术>移动开发
本页文章导读:
▪OpenGL学习一 OpenGL学习1
1.创建一个Activity
import android.app.Activity;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
public class OpenGLActivity extends Activity {
/** Called when the activity is first created. */
@Override
pu.........
▪ Animations(1) Animations(一)
Animation是个抽象类,他有五个子类。分别为:AlphaAnimation, AnimationSet, RotateAnimation, ScaleAnimation, TranslateAnimation 。 1. AlphaAnimation 淡入淡出效果 2. RotateAnimation 旋转效果 3. .........
▪ OpenGL学习三 OpenGL学习3
颜色和旋转
1.平滑着色
glColorPointer (int size, int type, int stride, Buffer pointer)
给每个点定义颜色值,各点之间平滑着色
需要gl.glEnableClientState(GL10.GL_COLOR_ARRAY);启动该功能,使用完需要gl.g.........
[1]OpenGL学习一
来源: 互联网 发布时间: 2014-02-18
OpenGL学习1
1.创建一个Activity
import android.app.Activity; import android.opengl.GLSurfaceView; import android.os.Bundle; public class OpenGLActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); GLSurfaceView glView = new GLSurfaceView(this); glView.setRenderer(new OpenGLRender()); setContentView(glView); } }
2.实现Renderer接口
import javax.microedition.khronos.egl.EGLConfig; import javax.microedition.khronos.opengles.GL10; import android.opengl.GLSurfaceView.Renderer public class OpenGLRender implements Renderer{ /** * 调用此方法绘制当前窗口 */ @Override public void onDrawFrame(GL10 gl) { gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); //清楚屏幕和深度缓存 } /** * 窗口被创建或者窗口大小改变时被调用 */ @Override public void onSurfaceChanged(GL10 gl, int width, int height) { gl.glViewport(0, 0, width, height); float ratio = (float) width / height; gl.glMatrixMode(GL10.GL_PROJECTION); //设置投影矩阵 gl.glLoadIdentity(); //重置投影矩阵 gl.glFrustumf(-ratio, ratio, -1, 1, 1, 10); //设置视口大小 } /** * 窗口被创建或者被重新创建时调用,做初始化工作 */ @Override public void onSurfaceCreated(GL10 gl, EGLConfig config) { gl.glClearColor(0, 0, 1.0f, 0.5f); //设置背景色,蓝色 } }
3.效果如下所示
[2] Animations(1)
来源: 互联网 发布时间: 2014-02-18
Animations(一)
Animation是个抽象类,他有五个子类。分别为:AlphaAnimation, AnimationSet, RotateAnimation, ScaleAnimation, TranslateAnimation 。
1. AlphaAnimation 淡入淡出效果
2. RotateAnimation 旋转效果
3. ScaleAnimation 缩放效果
4. TranslateAnimation 移动效果
5. AnimationSet animation集合,里面可以放多个animation。
下面直接实例,相关参数说明都在代码中:
Animation是个抽象类,他有五个子类。分别为:AlphaAnimation, AnimationSet, RotateAnimation, ScaleAnimation, TranslateAnimation 。
1. AlphaAnimation 淡入淡出效果
2. RotateAnimation 旋转效果
3. ScaleAnimation 缩放效果
4. TranslateAnimation 移动效果
5. AnimationSet animation集合,里面可以放多个animation。
下面直接实例,相关参数说明都在代码中:
package com.kevin.animations; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.view.animation.AlphaAnimation; import android.view.animation.Animation; import android.view.animation.AnimationSet; import android.view.animation.RotateAnimation; import android.view.animation.ScaleAnimation; import android.view.animation.TranslateAnimation; import android.widget.Button; import android.widget.ImageView; public class AnimationsDemo extends Activity { private ImageView img; private Button btn_alpha,btn_scale,btn_traslate,btn_rotate; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); img = (ImageView) findViewById(R.id.imageView1); btn_alpha = (Button) findViewById(R.id.btn_alpha); btn_rotate = (Button) findViewById(R.id.btn_rotate); btn_scale = (Button) findViewById(R.id.btn_scale); btn_traslate = (Button) findViewById(R.id.btn_translate); btn_alpha.setOnClickListener(new AlphaButtonListener()); btn_rotate.setOnClickListener(new RotateButtonListener()); btn_scale.setOnClickListener(new ScaleButtonListener()); btn_traslate.setOnClickListener(new TranslateButtonListener()); } // 淡入淡出效果 class AlphaButtonListener implements OnClickListener{ @Override public void onClick(View v) { // 创建AnimationSet对象 AnimationSet animationSet = new AnimationSet(true); // 创建AlphaAnimation对象 AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0); // 设置动画持续时间 alphaAnimation.setDuration(2000); // 将AlphaAnimation对象添加到AnimationSet当中 animationSet.addAnimation(alphaAnimation); // 使用ImageView的startAnimation方法开始执行动画 img.startAnimation(animationSet); } } // 旋转效果 class RotateButtonListener implements OnClickListener{ @Override public void onClick(View v) { AnimationSet animationSet = new AnimationSet(true); /* * fromDegrees 为0,起始旋转的角度 * toDegrees 为360,最终旋转到的角度 * pivotXType 为动画在X轴相对于物件位置类型(三种1.absolute 绝对坐标, 2. RELATIVE_TO_PARENT相对父控件 3.RELATIVE_TO_SELF相对于自身) * pivotXValue 1f(百分比)代表整个x轴的值,0f代表X轴的原点 * pivotYType,pivotYValue与上面类似 * pivotXType,pivotXValue,pivotYType,pivotYValue四个参数确定旋转的圆心 */ RotateAnimation rotateAnimation = new RotateAnimation(0, 320, Animation.RELATIVE_TO_PARENT, 1f, Animation.RELATIVE_TO_PARENT,0f); rotateAnimation.setDuration(5000); animationSet.setFillAfter(true); animationSet.setFillBefore(false); animationSet.addAnimation(rotateAnimation); img.startAnimation(animationSet); } } // 缩放效果 class ScaleButtonListener implements OnClickListener{ @Override public void onClick(View v) { AnimationSet animationSet = new AnimationSet(true); /* * fromX为X大小 * toX为缩放后的X大小(>1放大,<1缩小,=1维持不变) * fromY为Y轴大小 * toY为缩放后的Y大小 * pivotXType为动画在X轴相对于物件位置类型(三种1.absolute 绝对坐标, 2. RELATIVE_TO_PARENT相对父控件 3.RELATIVE_TO_SELF相对于自身) * pivotXValue 1f代表整个x轴的值,0f代表X轴的原点 * pivotYType,pivotYValue与上面类似 * pivotXType,pivotXValue,pivotYType,pivotYValue四个参数确定轴心 */ ScaleAnimation scaleAnimation = new ScaleAnimation(1, 2.0f, 1, 2.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); scaleAnimation.setDuration(2000); animationSet.addAnimation(scaleAnimation); // 让img停留在最终的位置,而不恢复到初始位置 animationSet.setFillAfter(true); animationSet.setFillBefore(false); img.startAnimation(animationSet); } } // 移动效果 class TranslateButtonListener implements OnClickListener{ @Override public void onClick(View v) { AnimationSet animationSet = new AnimationSet(true); /* * 第一个参数为动画在X轴相对于物件位置类型 * fromXValue为动画起始时 X坐标上的移动位置 * toXValue为动画结束时 X坐标上的移动位置 (最终的X的位置) * 下面的Y类似 */ TranslateAnimation translateAnimation = new TranslateAnimation( Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, -1f); translateAnimation.setDuration(5000); animationSet.addAnimation(translateAnimation); img.startAnimation(animationSet); System.out.println(img.getWidth() + " " + img.getHeight()); } } }
[3] OpenGL学习三
来源: 互联网 发布时间: 2014-02-18
OpenGL学习3
颜色和旋转
1.平滑着色
glColorPointer (int size, int type, int stride, Buffer pointer)
给每个点定义颜色值,各点之间平滑着色
需要gl.glEnableClientState(GL10.GL_COLOR_ARRAY);启动该功能,使用完需要gl.glDisableClientState(GL10.GL_COLOR_ARRAY);关闭平滑着色功能
2.单调着色
glColor4f (float red, float green, float blue, float alpha) 参数值在0.0-1.0之间
glColor4x (int red, int green, int blue, int alpha) int的高16位代表整数,低16为代表小数,因此,(0x10000,0,0,0x10000)表示红色不透明
3.旋转
glRotatex (int angle, int x, int y, int z)
第一个参数表示旋转的角度,(x,y,z)和(0,0,0)组成的直线代表旋转的轴
4.demo
package com.lanhuidong.opengl; import java.nio.FloatBuffer; import java.nio.IntBuffer; import javax.microedition.khronos.egl.EGLConfig; import javax.microedition.khronos.opengles.GL10; import android.opengl.GLSurfaceView.Renderer; import com.lanhuidong.opengl.util.BufferToNativeOrder; /** * @author lan * @since 2011-6-27 * @version */ public class OpenGLRender implements Renderer { private static int x = 0x10000 / 4; private static float f = 0.5f; private static float[] array = new float[] { -f, 0, 0, 0, (float) (f * Math.sqrt(3) / 2), 0, f, 0, 0 }; private static int[] array2 = new int[] { x, x, 0, x, -x, 0, -x, x, 0, -x, -x, 0 }; private static int[] array3 = new int[] { x, x, 0, -x, x, 0, -2 * x, 0, 0, -x, -x, 0, x, -x, 0, 2 * x, 0, 0 }; private static float[] colorarray = new float[] { 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1 }; private static float rotateTri = 0f; private static int rotateQuad = 0x10000; /** * 调用此方法绘制当前窗口 */ @Override public void onDrawFrame(GL10 gl) { gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); gl.glLoadIdentity(); gl.glTranslatef(-0.5f, 0.5f, -1.0f); // 画三角行 gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); gl.glEnableClientState(GL10.GL_COLOR_ARRAY); FloatBuffer triangleBuffer = BufferToNativeOrder.getNativeOrderFloatBuffer(array); gl.glRotatef(rotateTri, 0f, 1f, 0f); gl.glVertexPointer(3, GL10.GL_FLOAT, 0, triangleBuffer); gl.glColorPointer(4, GL10.GL_FIXED, 0, BufferToNativeOrder.getNativeOrderFloatBuffer(colorarray)); gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 3); rotateTri += 0.5; gl.glDisableClientState(GL10.GL_COLOR_ARRAY); gl.glLoadIdentity(); gl.glTranslatef(0.5f, 0.5f, -1.0f); // 画矩形 IntBuffer quadrangleBuffer = BufferToNativeOrder.getNativeOrderIntBuffer(array2); gl.glVertexPointer(3, GL10.GL_FIXED, 0, quadrangleBuffer); gl.glColor4f(1f, 0f, 0f, 1f); gl.glRotatex(rotateQuad, 1, 0, 0); rotateQuad -= 0x10000; gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4); gl.glLoadIdentity(); gl.glTranslatef(-0.5f, -0.5f, -1.0f); // 画矩形 gl.glVertexPointer(3, GL10.GL_FIXED, 0, BufferToNativeOrder.getNativeOrderIntBuffer(array3)); gl.glColor4x(0x10000, 0, 0x10000, 0x10000); gl.glDrawArrays(GL10.GL_TRIANGLE_FAN, 0, 6); gl.glDisableClientState(GL10.GL_VERTEX_ARRAY); } /** * 窗口被创建或者窗口大小改变时被调用 */ @Override public void onSurfaceChanged(GL10 gl, int width, int height) { gl.glViewport(0, 0, width, height); float ratio = (float) width / height; gl.glMatrixMode(GL10.GL_PROJECTION); gl.glLoadIdentity(); gl.glFrustumf(-ratio, ratio, -1, 1, 1, 10); } /** * 窗口被创建或者被重新创建时调用,做初始化工作 */ @Override public void onSurfaceCreated(GL10 gl, EGLConfig config) { gl.glClearColor(0, 0, 1.0f, 0.5f); } }
5.效果
最新技术文章: