ImageView iv = (ImageView) findViewById(R.id.ImageView01);
ImageView iv2 = (ImageView) findViewById(R.id.ImageView02);
ImageView iv3 = (ImageView) findViewById(R.id.ImageView03);
//draw的方式对整屏截取,但状态栏为黑色,不过可以通过canvas的translate方法根据状态
//栏的高度进行调整
View cv = getWindow().getDecorView();
Bitmap b = Bitmap.createBitmap(200, 200, Bitmap.Config.RGB_565);
cv.draw(new Canvas(b));
iv.setImageBitmap(b);
//对隐藏的未画过的控件,截出的是黑色
Bitmap image = Bitmap.createBitmap(200, 200, Bitmap.Config.RGB_565);
bt3.draw(new Canvas(image));
iv2.setImageBitmap(image);
//也可以通过getDrawingCache获取
bt4.setDrawingCacheEnabled(true);
Bitmap bitmap = bt4.getDrawingCache();
Drawable drawable = (Drawable) new BitmapDrawable(bitmap);
iv3.setBackgroundDrawable(drawable);
1.获取状态栏高度:
decorView是window中的最顶层view,可以从window中获取到decorView,然后decorView有个getWindowVisibleDisplayFrame方法可以获取到程序显示的区域,包括标题栏,但不包括状态栏。
于是,我们就可以算出状态栏的高度了。
Java代码
Rect frame = new Rect();
getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
int statusBarHeight = frame.top;
Rect frame = new Rect();
getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
int statusBarHeight = frame.top;
2.获取标题栏高度:
getWindow().findViewById(Window.ID_ANDROID_CONTENT)这个方法获取到的view就是程序不包括标题栏的部分,然后就可以知道标题栏的高度了。
Java代码
int contentTop = getWindow().findViewById(Window.ID_ANDROID_CONTENT).getTop();
//statusBarHeight是上面所求的状态栏的高度
int titleBarHeight = contentTop - statusBarHeight
int contentTop = getWindow().findViewById(Window.ID_ANDROID_CONTENT).getTop();
//statusBarHeight是上面所求的状态栏的高度
int titleBarHeight = contentTop - statusBarHeight
################################################################################
状态栏高度最新获取方式:
public static int getStatusHeight(Activity activity){
int statusHeight = 0;
Rect localRect = new Rect();
activity.getWindow().getDecorView(
).getWindowVisibleDisplayFrame(localRect);
statusHeight = localRect.top;
if (0 == statusHeight){
Class<?> localClass;
try {
localClass = Class.forName(
"com.android.internal.R$dimen");
Object localObject = localClass.newInstance();
int i5 = Integer.parseInt(
localClass.getField("status_bar_height").get(
localObject).toString());
statusHeight = activity.getResources(
).getDimensionPixelSize(i5);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (NumberFormatException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
}
return statusHeight;
}