Android替代MetroGridHelper

时间:2011-11-29 17:22:40

标签: android debugging windows-phone-7 overlay

我是Android开发的新手,我来自WP7世界,所以我发现许多熟悉的工具缺失。我最喜欢的一个是http://www.jeff.wilcox.name/2011/10/metrogridhelper/,这对我来说非常有帮助,可以在调试过程中找出UI布局或对齐方面的问题。所以我想知道在android世界中是否有另一种选择,或者任何人都可以给我一些关于如何在android中实现类似内容的提示。

1 个答案:

答案 0 :(得分:0)

好的,我们弄清楚了。 用法:

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();
            }
        }

    }
}