- 获取View的缓存,设置到新的ImageView 固定View的位置
public void init() {
View view = mApplication.getView();
Bitmap bitmap = getViewImageCache(view);
if (bitmap != null) {
imgView.setImageBitmap(bitmap);
}
//获取状态栏高度
int barh=DensityUtil.getStatusBarH(this);
//获取view的位置
int[] location = new int[2];
view.getLocationOnScreen(location);
int left = location[0];
int top = location[1];//整个屏幕高度
LinearLayout.MarginLayoutParams marginLayoutParams = new ViewGroup.MarginLayoutParams(imgView.getLayoutParams());
marginLayoutParams.topMargin = top-barh;//整个屏幕高度-状态栏高度
marginLayoutParams.leftMargin = left;
marginLayoutParams.width = view.getWidth();
marginLayoutParams.height = view.getHeight();
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(marginLayoutParams);
imgView.setLayoutParams(layoutParams);
}
//获取View缓存
private Bitmap getViewImageCache(View view) {
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap bitmap = view.getDrawingCache();
if (bitmap == null) {
return null;
}
bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight());
view.destroyDrawingCache();
return bitmap;
}
获取状态栏高度 方法
public static int getStatusBarH(Context context) {
Class<?> c;
Object obj;
Field field;
int statusBarHeight = 0;
try {
c = Class.forName("com.android.internal.R$dimen");
obj = c.newInstance();
field = c.getField("status_bar_height");
int x = Integer.parseInt(field.get(obj).toString());
statusBarHeight = context.getResources().getDimensionPixelSize(x);
} catch (Exception e1) {
e1.printStackTrace();
}
return statusBarHeight;
}
网友评论