我打算写一个服务来在后台做截图,所以我应该用服务来做。在这些代码中,我无法创建Bitmap,因为我无法获得宽度和高度
@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,它们也返回0
int w = mView.getWidth();
int h = mView.getHeight();
w = mView.getMeasuredWidth();
h = mView.getMeasuredHeight();
我还在getMeasuredWidth和getMeasuredHeight之前尝试了这个,但它仍然不起作用。
mView.measure(MeasureSpec.UNSPECIFIED,MeasureSpec.UNSPECIFIED)
答案 0 :(得分:2)
getMeasuredWidth()
和getMeasuredHeight()
除非您先致电measure()
,否则不会返回非零值。
之后,您可以使用测量值来调用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();
答案 1 :(得分:2)
您可以使用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();
}
});
答案 2 :(得分:2)
视图在绘制后会知道它的dimmens。试试这个:
Private WithEvents mclsCbx As MSForms.ComboBox
Public Property Set Cbx(ByVal clsCbx As MSForms.ComboBox): Set mclsCbx = clsCbx: End Property
Public Property Get Cbx() As MSForms.ComboBox: Set Cbx = mclsCbx: End Property
Private Sub mclsCbx_Change()
MsgBox Me.Cbx.Name
End Sub