SHARE
TWEET

Untitled

a guest Oct 3rd, 2012 7,275 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import android.app.Activity;
  2. import android.graphics.Canvas;
  3. import android.graphics.Paint;
  4. import android.graphics.PorterDuff;
  5. import android.graphics.SurfaceTexture;
  6. import android.os.Bundle;
  7. import android.view.Gravity;
  8. import android.view.TextureView;
  9. import android.widget.FrameLayout;
  10.  
  11. @SuppressWarnings({"UnusedDeclaration"})
  12. public class CanvasTextureViewActivity extends Activity
  13.         implements TextureView.SurfaceTextureListener {
  14.     private TextureView mTextureView;
  15.     private CanvasTextureViewActivity.RenderingThread mThread;
  16.  
  17.     @Override
  18.     protected void onCreate(Bundle savedInstanceState) {
  19.         super.onCreate(savedInstanceState);
  20.  
  21.         FrameLayout content = new FrameLayout(this);
  22.  
  23.         mTextureView = new TextureView(this);
  24.         mTextureView.setSurfaceTextureListener(this);
  25.         mTextureView.setOpaque(false);
  26.  
  27.         content.addView(mTextureView, new FrameLayout.LayoutParams(500, 500, Gravity.CENTER));
  28.         setContentView(content);
  29.     }
  30.  
  31.     @Override
  32.     public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
  33.         mThread = new RenderingThread(mTextureView);
  34.         mThread.start();
  35.     }
  36.  
  37.     @Override
  38.     public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
  39.         // Ignored
  40.     }
  41.  
  42.     @Override
  43.     public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
  44.         if (mThread != null) mThread.stopRendering();
  45.         return true;
  46.     }
  47.  
  48.     @Override
  49.     public void onSurfaceTextureUpdated(SurfaceTexture surface) {
  50.         // Ignored
  51.     }
  52.  
  53.     private static class RenderingThread extends Thread {
  54.         private final TextureView mSurface;
  55.         private volatile boolean mRunning = true;
  56.  
  57.         public RenderingThread(TextureView surface) {
  58.             mSurface = surface;
  59.         }
  60.  
  61.         @Override
  62.         public void run() {
  63.             float x = 0.0f;
  64.             float y = 0.0f;
  65.             float speedX = 5.0f;
  66.             float speedY = 3.0f;
  67.            
  68.             Paint paint = new Paint();
  69.             paint.setColor(0xff00ff00);
  70.  
  71.             while (mRunning && !Thread.interrupted()) {
  72.                 final Canvas canvas = mSurface.lockCanvas(null);
  73.                 try {
  74.                     canvas.drawColor(0x00000000, PorterDuff.Mode.CLEAR);
  75.                     canvas.drawRect(x, y, x + 20.0f, y + 20.0f, paint);
  76.                 } finally {
  77.                     mSurface.unlockCanvasAndPost(canvas);
  78.                 }
  79.  
  80.                 if (x + 20.0f + speedX >= mSurface.getWidth() || x + speedX <= 0.0f) {
  81.                     speedX = -speedX;
  82.                 }
  83.                 if (y + 20.0f + speedY >= mSurface.getHeight() || y + speedY <= 0.0f) {
  84.                     speedY = -speedY;
  85.                 }
  86.  
  87.                 x += speedX;
  88.                 y += speedY;
  89.  
  90.                 try {
  91.                     Thread.sleep(15);
  92.                 } catch (InterruptedException e) {
  93.                     // Interrupted
  94.                 }
  95.             }
  96.         }
  97.        
  98.         void stopRendering() {
  99.             interrupt();
  100.             mRunning = false;
  101.         }
  102.     }
  103. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top