標籤:android 安卓 技術
1、擷取標題列的高度
Rect frame = new Rect();getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);int statusBarHeight = frame.top;int contentTop = getWindow().findViewById(Window.ID_ANDROID_CONTENT).getTop();int titleBarHeight = contentTop - statusBarHeight;textView.setText("statusBarHeight" + statusBarHeight + ";contentTop="+ contentTop + ";titleBarHeight" + titleBarHeight);
2、以Acivity作為布局
註:該類必須繼承ActivityGroup
LocalActivityManager activityManager;activityManager = getLocalActivityManager();View view2 = activityManager.startActivity("act2",new Intent(this, Act2.class)).getDecorView();linearLayout.addView(view2, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
3、螢幕並儲存
View view = getWindow().getDecorView();Bitmap bitmap = Bitmap.createBitmap(view.getWidth(),view.getHeight(), Config.RGB_565); view.draw(new Canvas(bitmap)); imageView.setImageBitmap(bitmap);
4、android 擷取組件尺寸大小
在oncreate()中利用view.getWidth()或是view.getHeiht()來擷取view的寬和高,看似沒有問題,其實他們去得值是0,並不是你想要的結果?
這是為什麼呢?
在調用oncreate()方法時,介面處於不可見狀態,記憶體載入組件還沒有繪製出來,你是無法擷取他的尺寸。
那如何在繪製組件之前能擷取到該組件的尺寸大小呢?
這裡有三種方法,經過驗證的:
(1)
int width =View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED); int height =View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED); view.measure(width,height); int height=view.getMeasuredHeight(); int width=view.getMeasuredWidth();
(2)
//增加組件繪製之前的監聽ViewTreeObserver vto =view.getViewTreeObserver(); vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() { @Override public booleanonPreDraw() { int height =view.getMeasuredHeight(); int width =view.getMeasuredWidth(); } }); (3)
//增加整體布局監聽ViewTreeObserver vto = view.getViewTreeObserver(); vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener(){ @Override public voidonGlobalLayout() { view.getViewTreeObserver().removeGlobalOnLayoutListener(this); int height =view.getMeasuredHeight(); int width =view.getMeasuredWidth(); } });那麼,在activity進入運行期時,組件的尺寸擷取方法就很簡單了,直接getWidth()和getHeight().
安卓零碎知識集中(2)