添加到 window 时如何获取视图宽度和高度?

How to get view width and height when I added it to window?

我要写一个后台截图的服务,所以我应该用服务来做。在这些代码中,我无法创建位图,因为我无法获取宽度和高度

@Override
public void onCreate() {
    super.onCreate();

    try {
        mView = new LinearLayout(this);
        mView.setBackgroundColor(0);
        WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
                0 | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                        | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
                        | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
                        | WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR
                        | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED,
                PixelFormat.TRANSLUCENT);
        WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
        wm.addView(mView, params);


        //all return 0
        int w = mView.getWidth();
        int h = mView.getHeight();
        w = mView.getMeasuredWidth();
        h = mView.getMeasuredHeight();


        mView.measure(View.MeasureSpec.makeMeasureSpec(
                        View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED),
                View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
        mView.layout(0, 0, mView.getMeasuredWidth(),
                mView.getMeasuredHeight());
        mView.setDrawingCacheEnabled(true);
        mView.buildDrawingCache();
        Bitmap bitmap = Bitmap.createBitmap(mView.getMeasuredWidth(),
                mView.getMeasuredHeight(), Bitmap.Config.ARGB_8888);

        Canvas canvas = new Canvas(bitmap);
        Paint paint = new Paint();
        int height = bitmap.getHeight();
        canvas.drawBitmap(bitmap, 0, height, paint);
        mView.draw(canvas);

        if (bitmap != null) {
            try {
                String filePath = Environment.getExternalStorageDirectory()
                        .toString();
                OutputStream out = null;
                File file = new File(filePath, "/mViewScreenShot.png");
                Log.v("0",file.getPath());
                out = new FileOutputStream(file);

                bitmap.compress(Bitmap.CompressFormat.PNG, 50, out);
                out.flush();
                out.close();
                bitmap.recycle();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }
    catch(Exception e)
    {
        stopSelf();
    }
    stopSelf();
}

我尝试了 getWidth 和 getMeasuredWidth,它们也是 return 0

int w = mView.getWidth();
int h = mView.getHeight();
w = mView.getMeasuredWidth();
h = mView.getMeasuredHeight();

我在getMeasuredWidth和getMeasuredHeight之前也试过了,还是不行。

mView.measure(MeasureSpec.UNSPECIFIED,MeasureSpec.UNSPECIFIED)

getMeasuredWidth()getMeasuredHeight() 不会 return 一个非零值,除非你调用 measure() first.

之后,您可以使用测量值调用 layout() 并从 getWidth()getHeight() 中获取正确的值。

mView.measure(View.MeasureSpec.makeMeasureSpec(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
mView.layout(0, 0, mView.getMeasuredWidth(), mView.getMeasuredHeight());
int w = mView.getWidth();
int h = mView.getHeight();
w = mView.getMeasuredWidth();
h = mView.getMeasuredHeight();

您可以使用 ViewTreeObserver 获取视图的宽度和高度。您可以像下面这样使用它的 onCreate 方法:

LinearLayout layout = (LinearLayout)findViewById(R.id.YOUR_VIEW_ID);
ViewTreeObserver vto = layout.getViewTreeObserver(); 
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 
    @Override 
    public void onGlobalLayout() { 
        this.layout.getViewTreeObserver().removeGlobalOnLayoutListener(this); 
        int width  = layout.getMeasuredWidth();
        int height = layout.getMeasuredHeight(); 

    } 
});

View 会在绘制后知道它变暗了。试试这个:

mView.post(new Runnable{
    @Override
    public void run() {
        width = mView.getWidth();
        height = mView.getHeight();
    }
});