Search code examples
androidandroid-layoutandroid-serviceandroid-viewandroid-windowmanager

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


I am going to write a service to do screenshot in background, so I should use service to do. In these code, I cannot create Bitmap because I cannot get the width and height

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

I tried both getWidth and getMeasuredWidth, they also return 0

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

And I also tried this before getMeasuredWidth and getMeasuredHeight, it still not work.

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

Solution

  • View will know it's dimmens after drawing. Try this:

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