2011-11-29 57 views

回答

0

好的,所以我們計算出來了。 USAGE:

if(Debug.isDebuggerConnected()) 
     grid = new Grid(this); 

源代碼如下

import android.content.Context; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.graphics.Paint; 
import android.graphics.PixelFormat; 
import android.graphics.Rect; 
import android.util.AttributeSet; 
import android.view.Display; 
import android.view.View; 
import android.view.WindowManager; 
import android.view.WindowManager.LayoutParams; 

public class Grid { 
    private WindowManager mWindowManager; 
    private GeoView geoView = null; 

    AttributeSet attr = null; 

    public Grid(Context context) { 
     geoView = new GeoView(context, attr); 
     // context.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
     // WindowManager.LayoutParams.FLAG_FULLSCREEN); 
     mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); 
     WindowManager.LayoutParams lp = new WindowManager.LayoutParams(
       LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, 
       WindowManager.LayoutParams.TYPE_APPLICATION, 
       WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE 
         | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, 
       PixelFormat.TRANSLUCENT); 
     // | WindowManager.LayoutParams.FLAG_FULLSCREEN 

     mWindowManager.addView(geoView, lp); 

    } 

    protected void finalize() { 
     mWindowManager.removeView(geoView); 
    } 

    public class GeoView extends View implements Runnable { 
     private Paint mPaint = null; 
     private int rectWidth = 25; 
     private int rectHeight = 25; 
     private int rectSpace = 12; 
     private int space = 12; 
     private WindowManager mWindowManager; 
     private int screenWidth = 0; 
     private int screenHeight = 0; 
     private int columnLoopCount = 0; 
     private int rowLoopCount = 0; 

     public GeoView(Context context, AttributeSet attr) { 
      super(context, attr); 
      mWindowManager = (WindowManager) context.getSystemService("window"); 
      Display disp = mWindowManager.getDefaultDisplay(); 
      screenWidth = disp.getWidth(); 
      screenHeight = disp.getHeight(); 
      // Log.d(TAG, "screen width="+disp.getWidth()); 
      // Log.d(TAG, "screen height="+disp.getHeight()); 
      columnLoopCount = (screenWidth - space)/(rectWidth + rectSpace); 
      rowLoopCount = (screenHeight - space)/(rectHeight + rectSpace); 
      columnLoopCount++; 
      rowLoopCount++; 

      mPaint = new Paint(); 

      new Thread(this).start(); 
     } 

     @Override 
     protected void onDraw(Canvas canvas) { 
      super.onDraw(canvas); 

      mPaint.setAntiAlias(true); 


      // mPaint.setStyle(Paint.Style.FILL); 
      mPaint.setStyle(Paint.Style.STROKE); 

      for (int i = 0; i < rowLoopCount; i++) { 
       for (int j = 0; j < columnLoopCount; j++) { 
        int newX = j * (rectWidth + rectSpace); 
        int newY = i * (rectWidth + rectSpace); 


        Rect rect1 = new Rect(); 

        rect1.left = space + newX; 
        rect1.top = space + newY; 
        rect1.right = space + rectWidth + newX; 
        rect1.bottom = space + rectHeight + newY; 
        // Log.d(TAG, 
        // "i="+i+",j="+j+",lfet="+rect1.left+" top="+rect1.top+" bottom"+rect1.bottom+" right="+rect1.right); 
        mPaint.setColor(Color.DKGRAY); 

        canvas.drawRect(rect1, mPaint); 

       } 
      } 
     } 

     @Override 
     public void run() { 
      while (!Thread.currentThread().isInterrupted()) { 
       try { 
        Thread.sleep(100); 
       } catch (InterruptedException e) { 
        Thread.currentThread().interrupt(); 
       } 

       postInvalidate(); 
      } 
     } 

    } 
}