| 1 | 
 | 
  
    | 2 | package org.distortedandroid.examples.bean;
 | 
  
    | 3 | 
 | 
  
    | 4 | import java.io.IOException;
 | 
  
    | 5 | import java.io.InputStream;
 | 
  
    | 6 | 
 | 
  
    | 7 | import javax.microedition.khronos.egl.EGLConfig;
 | 
  
    | 8 | import javax.microedition.khronos.opengles.GL10;
 | 
  
    | 9 | 
 | 
  
    | 10 | import org.distortedandroid.examples.R;
 | 
  
    | 11 | 
 | 
  
    | 12 | import org.distortedandroid.library.Interpolator2D;
 | 
  
    | 13 | import org.distortedandroid.library.Distorted;
 | 
  
    | 14 | import org.distortedandroid.library.DistortedBitmap;
 | 
  
    | 15 | import org.distortedandroid.library.Float2D;
 | 
  
    | 16 | import org.distortedandroid.library.Float4D;
 | 
  
    | 17 | 
 | 
  
    | 18 | import android.graphics.Bitmap;
 | 
  
    | 19 | import android.graphics.BitmapFactory;
 | 
  
    | 20 | import android.opengl.GLES20;
 | 
  
    | 21 | import android.opengl.GLSurfaceView;
 | 
  
    | 22 | 
 | 
  
    | 23 | ///////////////////////////////////////////////////////////////////////////////////////////////////
 | 
  
    | 24 | 
 | 
  
    | 25 | class BeanRenderer implements GLSurfaceView.Renderer 
 | 
  
    | 26 | {
 | 
  
    | 27 |    private GLSurfaceView mView;
 | 
  
    | 28 |    private DistortedBitmap mBean;
 | 
  
    | 29 |    private Float2D pLeft, pRight;
 | 
  
    | 30 |    private Interpolator2D iLeft, iRight;
 | 
  
    | 31 |    private Float4D rLeft, rRight;
 | 
  
    | 32 |    private int bmpHeight, bmpWidth;
 | 
  
    | 33 |     
 | 
  
    | 34 | ///////////////////////////////////////////////////////////////////////////////////////////////////
 | 
  
    | 35 | 
 | 
  
    | 36 |    public BeanRenderer(GLSurfaceView v) 
 | 
  
    | 37 |       {
 | 
  
    | 38 |       mView = v;
 | 
  
    | 39 |      
 | 
  
    | 40 |       pLeft = new Float2D(100, 199);
 | 
  
    | 41 |       pRight= new Float2D(230, 150);
 | 
  
    | 42 |       
 | 
  
    | 43 |       rLeft = new Float4D(-9,-31,35,35);
 | 
  
    | 44 |       rRight= new Float4D(-9,-31,35,35);
 | 
  
    | 45 |      
 | 
  
    | 46 |       iLeft = new Interpolator2D();
 | 
  
    | 47 |       iRight= new Interpolator2D();
 | 
  
    | 48 |      
 | 
  
    | 49 |       iLeft.setCount(0.0f);
 | 
  
    | 50 |       iRight.setCount(0.0f);
 | 
  
    | 51 |       iLeft.setDuration(1500);
 | 
  
    | 52 |       iRight.setDuration(1500);
 | 
  
    | 53 |       
 | 
  
    | 54 |       Float2D p1 = new Float2D(0,0);
 | 
  
    | 55 |       Float2D p2 = new Float2D(-10,-34);
 | 
  
    | 56 |       
 | 
  
    | 57 |       iLeft.add(p1);
 | 
  
    | 58 |       iLeft.add(p1);
 | 
  
    | 59 |       iLeft.add(p1);
 | 
  
    | 60 |       iLeft.add(p1);
 | 
  
    | 61 |       iLeft.add(p2);
 | 
  
    | 62 |       iLeft.add(p2);
 | 
  
    | 63 |       
 | 
  
    | 64 |       iRight.add(p1);
 | 
  
    | 65 |       iRight.add(p2);
 | 
  
    | 66 |       iRight.add(p2);
 | 
  
    | 67 |       iRight.add(p1);
 | 
  
    | 68 |       iRight.add(p1);
 | 
  
    | 69 |       iRight.add(p1);
 | 
  
    | 70 |       }
 | 
  
    | 71 | 
 | 
  
    | 72 | ///////////////////////////////////////////////////////////////////////////////////////////////////
 | 
  
    | 73 |    
 | 
  
    | 74 |     public void onDrawFrame(GL10 glUnused) 
 | 
  
    | 75 |       {
 | 
  
    | 76 |       GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
 | 
  
    | 77 |       GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
 | 
  
    | 78 |       
 | 
  
    | 79 |       mBean.draw(System.currentTimeMillis());
 | 
  
    | 80 |       }
 | 
  
    | 81 | 
 | 
  
    | 82 | ///////////////////////////////////////////////////////////////////////////////////////////////////
 | 
  
    | 83 |     
 | 
  
    | 84 |     public void onSurfaceChanged(GL10 glUnused, int width, int height) 
 | 
  
    | 85 |       { 
 | 
  
    | 86 |       mBean.abortAllEffects(Distorted.TYPE_MATR);  
 | 
  
    | 87 |          
 | 
  
    | 88 |       if( bmpHeight/bmpWidth > height/width )
 | 
  
    | 89 |         {
 | 
  
    | 90 |         int w = (height*bmpWidth)/bmpHeight;
 | 
  
    | 91 |         mBean.move((width-w)/2 ,0, 0);
 | 
  
    | 92 |         mBean.scale((float)height/bmpHeight);
 | 
  
    | 93 |         }
 | 
  
    | 94 |       else
 | 
  
    | 95 |         {
 | 
  
    | 96 |         int h = (width*bmpHeight)/bmpWidth;
 | 
  
    | 97 |         mBean.move(0 ,(height-h)/2, 0);
 | 
  
    | 98 |         mBean.scale((float)width/bmpWidth);
 | 
  
    | 99 |         }
 | 
  
    | 100 |       
 | 
  
    | 101 |       Distorted.onSurfaceChanged(width, height); 
 | 
  
    | 102 |       }
 | 
  
    | 103 | 
 | 
  
    | 104 | ///////////////////////////////////////////////////////////////////////////////////////////////////
 | 
  
    | 105 |     
 | 
  
    | 106 |     public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
 | 
  
    | 107 |       {
 | 
  
    | 108 |       InputStream is = mView.getContext().getResources().openRawResource(R.raw.bean);
 | 
  
    | 109 |       Bitmap bitmap;
 | 
  
    | 110 |         
 | 
  
    | 111 |       try 
 | 
  
    | 112 |         {
 | 
  
    | 113 |         bitmap = BitmapFactory.decodeStream(is);
 | 
  
    | 114 |         } 
 | 
  
    | 115 |       finally 
 | 
  
    | 116 |         {
 | 
  
    | 117 |         try 
 | 
  
    | 118 |           {
 | 
  
    | 119 |           is.close();
 | 
  
    | 120 |           } 
 | 
  
    | 121 |         catch(IOException e) { }
 | 
  
    | 122 |         }  
 | 
  
    | 123 |       
 | 
  
    | 124 |       bmpHeight = bitmap.getHeight();
 | 
  
    | 125 |       bmpWidth  = bitmap.getWidth();
 | 
  
    | 126 |       
 | 
  
    | 127 |       mBean = new DistortedBitmap(bitmap, 10);     
 | 
  
    | 128 |       mBean.distort(iLeft , rLeft , pLeft );
 | 
  
    | 129 |       mBean.distort(iRight, rRight, pRight);
 | 
  
    | 130 |       
 | 
  
    | 131 |       try
 | 
  
    | 132 |         {
 | 
  
    | 133 |         Distorted.onSurfaceCreated(mView);
 | 
  
    | 134 |         }
 | 
  
    | 135 |       catch(Exception ex)
 | 
  
    | 136 |         {
 | 
  
    | 137 |         android.util.Log.e("Bean", ex.getMessage() );
 | 
  
    | 138 |         }
 | 
  
    | 139 |       }
 | 
  
    | 140 | }
 |