Android替代MetroGridHelper的解决方案

6

我是一个Android开发的新手,之前从WP7的世界过来,所以发现很多熟悉的工具都不见了。其中我最喜欢的一个是http://www.jeff.wilcox.name/2011/10/metrogridhelper/,它对我在调试期间查找UI布局或对齐方面出错非常有帮助。因此,我想知道在Android世界中是否有替代品,或者有没有人能够给我一些提示如何在Android中实现类似的功能。

1个回答

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

    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接