Advertisement
Guest User

Untitled

a guest
Jul 25th, 2014
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.91 KB | None | 0 0
  1. import android.content.Context;
  2. import android.graphics.Bitmap;
  3. import android.graphics.BitmapFactory;
  4. import android.graphics.Canvas;
  5. import android.graphics.Matrix;
  6. import android.graphics.RectF;
  7. import android.util.AttributeSet;
  8. import android.view.SurfaceHolder;
  9. import android.view.SurfaceView;
  10.  
  11. public class MySurface extends SurfaceView implements Runnable {
  12.    
  13.     SurfaceHolder ourHolder;
  14.     Thread ourThread = null;
  15.     boolean isRunning = true;
  16.    
  17.     Bitmap Background;
  18.     Bitmap clouda;
  19.    
  20.     Matrix matrix = new Matrix();
  21.    
  22.    
  23.    
  24.  
  25.     public MySurface(Context context) {
  26.         super(context);
  27.         init(context);
  28.        
  29.     }
  30.  
  31.     public MySurface(Context context, AttributeSet attrs) {
  32.         super(context, attrs);
  33.         init(context);
  34.     }
  35.  
  36.     public MySurface(Context context, AttributeSet attrs, int defStyle) {
  37.         super(context, attrs, defStyle);
  38.         init(context);
  39.     }
  40.  
  41.     private void init(Context context) {
  42.         // do stuff that was in your original constructor...
  43.         ourHolder = getHolder();
  44.         ourThread = new Thread(this);
  45.         ourThread.start();
  46.        
  47.         Background = BitmapFactory.decodeResource(getResources(), R.drawable.island);
  48.         clouda = BitmapFactory.decodeResource(getResources(), R.drawable.clouda);
  49.        
  50.        
  51.         Matrix matrix = getMatrix();
  52.         RectF drawableRect = new RectF(0, 0, clouda.getWidth(), clouda.getHeight());
  53.         RectF viewRect = new RectF(0, 0, getWidth(), getHeight());
  54.        
  55.         matrix.setRectToRect(drawableRect, viewRect, Matrix.ScaleToFit.CENTER);
  56.  
  57.        
  58.        
  59.  
  60.     }
  61.    
  62.    
  63.    
  64.     public void pause(){
  65.        
  66.     }
  67.    
  68.     public void resume(){
  69.        
  70.     }
  71.  
  72.     @Override
  73.     public void run() {
  74.         // TODO Auto-generated method stub
  75.         while(isRunning){
  76.             if (!ourHolder.getSurface().isValid())
  77.                 continue;
  78.            
  79.             Canvas canvas = ourHolder.lockCanvas();
  80.            
  81.            
  82.             canvas.drawBitmap(Background, matrix, null);
  83.             canvas.drawBitmap(clouda, 0, 0, null);
  84.            
  85.            
  86.            
  87.             ourHolder.unlockCanvasAndPost(canvas);
  88.            
  89.         }
  90.        
  91.     }
  92.  
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement