在其背景中使用opengl和Gesture ImageView绘制透明三角形

时间:2012-10-10 10:29:14

标签: android imageview

我需要制作一个三角形框架。并且GestureImageView将成为该Triangle Frame的背景。三角形只有坚实的边界。这样用户就可以放大并缩小三角形内的图像。 任何帮助?

我制作了GestureImageView。唯一需要的是三角透明框架。请帮帮忙?

以下是代码

public class TriangleActivity extends Activity implements android.opengl.GLSurfaceView.Renderer
{
    private static final String TAG = TriangleActivity.class.getSimpleName();

    private static final int Coordinates_In_A_Vertex = 2;
    private static final int Vertices_In_A_Triangle = 3;
    private static final int Bits_In_A_Byte = 8;
    private static final int Bits_In_A_Float = Float.SIZE;
    private static final int Bytes_In_A_Float = Bits_In_A_Float / Bits_In_A_Byte;

    private GLSurfaceView glSurfaceView;
    private FloatBuffer vertices;
    private int width;
    private int height;

    /*
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.triangle);
        glSurfaceView=(GLSurfaceView)findViewById(R.id.surfaceview);
        //glSurfaceView.setEGLConfigChooser(8,8,8,8,16,0);

        glSurfaceView.setRenderer(this);
//      glSurfaceView.getHolder().setFormat(PixelFormat.TRANSPARENT);
//      glSurfaceView.setZOrderOnTop(true);

    }

    /*
     * Called when the activity is resumed.
     * The activity is resumed whenever it is being presented to user (from the background).
     */
    @Override
    public void onResume()
    {
        super.onResume();
        glSurfaceView.onResume();
    }

    /*
     * Called when the activity is paused.
     * The activity is paused whenever it is placed into the background. Various things can cause this (pressing the home button, receiving a phone call, turning off the display, etc.).
     */
    @Override
    public void onPause()
    {
        glSurfaceView.onPause();
        super.onPause();
    }

    @Override
    public void onDrawFrame(GL10 gl)
    {
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
        gl.glColor4f(0.64313725490196f, 0.77647058823529f, 0.22352941176471f, 1);
        gl.glVertexPointer(2, GL10.GL_FLOAT, 0, vertices);
        gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 3);
    }

    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height)
    {
        Log.d(TAG, "Surface changed! width = " + String.valueOf(width) + ", height = " + String.valueOf(height));
    }

    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config)
    {
        Log.d(TAG, "Surface Created!");

        width = glSurfaceView.getWidth();
        height = glSurfaceView.getHeight();


        gl.glViewport(0, 0, width, height);
        gl.glMatrixMode(GL10.GL_PROJECTION);
        gl.glLoadIdentity();
        gl.glOrthof(0, 1, 0, 1, 1, -1);
        //New Changes
//      gl.glDisable(GL10.GL_DITHER);
//      gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT,
//              GL10.GL_FASTEST);

         //gl.glClearColor(1, 1, 1, 1);
//       gl.glEnable(GL10.GL_CULL_FACE);
//       gl.glShadeModel(GL10.GL_SMOOTH);
//       gl.glEnable(GL10.GL_DEPTH_TEST);
         //End

        ByteBuffer byteBuffer = ByteBuffer.allocateDirect(Coordinates_In_A_Vertex * Vertices_In_A_Triangle * Bytes_In_A_Float);
        byteBuffer.order(ByteOrder.nativeOrder());

        vertices = byteBuffer.asFloatBuffer();

        vertices.put(new float[]
                { 
                    0.0f, 0.0f,
                    0.5f, 1.0f,
                    1.0f, 0.0f
                }
        );

        vertices.flip();

        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
    }
}

0 个答案:

没有答案