点击事件中调用
View view;
view = MainActivity.this.getWindow().getDecorView();
initPopuptWindow(view);
//释放资源,防止多次截屏后显示的是同一张图片
view.destroyDrawingCache();
private void initPopuptWindow(View layout) {
// private void initPopuptWindow(Bitmap screen) {
// TODO Auto-generated method stub
//对当前页面进行截屏
layout.setDrawingCacheEnabled(true);
layout.buildDrawingCache(); //启用DrawingCache并创建位图
// Bitmap screen = Bitmap.createBitmap(layout.getDrawingCache()); //创建一个DrawingCache的拷贝,因为DrawingCache得到的位图在禁用后会被回收
Bitmap screen = Bitmap.createBitmap(getViewBitmap(layout)); //创建一个DrawingCache的拷贝,因为DrawingCache得到的位图在禁用后会被回收
// layout.setDrawingCacheEnabled(false); //禁用DrawingCahce否则会影响性能
// Log.e("Alex", "转换前bitmap的大小是"+screen.getWidth()+" : "+screen.getHeight());
screen = scaleBitmap(screen, screen.getWidth()/2, screen.getHeight()/2);//压缩bitmap到指定大小
// Log.e("Alex", "转换后bitmap的大小是"+screen.getWidth()+" : "+screen.getHeight());
//将截屏进行模糊
screen = Blur.fastblur(MainActivity.this, screen, 10);
Log.e("alex","screen是否为空"+screen);
// 获取自定义布局文件activity_popupwindow_left.xml的视图
final View popupWindow_view = getLayoutInflater().inflate(R.layout.home_add_popuwind, null, false);
// 创建PopupWindow实例,200,LayoutParams.MATCH_PARENT分别是宽度和高度
background.setImageBitmap(screen);
background.setVisibility(View.VISIBLE);
final int screenWidth = getScreenWidth(this);
WindowManager wm1 = MainActivity.this.getWindowManager();
int width1 = wm1.getDefaultDisplay().getWidth();
// popupWindow = new PopupWindow(popupWindow_view, width1 - 200, LinearLayout.LayoutParams.WRAP_CONTENT); //设置popwindow的大小
popupWindow = new PopupWindow(popupWindow_view, width1, LinearLayout.LayoutParams.WRAP_CONTENT); //设置popwindow的大小
popupWindow_view.findViewById(R.id.a5).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
popupWindow.dismiss();
background.setVisibility(View.GONE);
}
});
popupWindow_view.findViewById(R.id.a1).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "写问卷", Toast.LENGTH_SHORT).show();
}
});
popupWindow_view.findViewById(R.id.a2).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "写调查", Toast.LENGTH_SHORT).show();
}
});
popupWindow_view.findViewById(R.id.a3).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "写报表", Toast.LENGTH_SHORT).show();
}
});
popupWindow.showAtLocation(MainActivity.this.getWindow().getDecorView(), Gravity.CENTER, 0, 0);
}
/**
* Get the screen width.
*
* @param context
* @return the screen width
*/
@SuppressWarnings("deprecation")
@SuppressLint("NewApi")
public static int getScreenWidth(Activity context) {
Display display = context.getWindowManager().getDefaultDisplay();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
Point size = new Point();
display.getSize(size);
return size.x;
}
return display.getWidth();
}
/**
* 把一个bitmap压缩,压缩到指定大小
*
* @param bm
* @param width
* @param height
* @return
*/
private static Bitmap scaleBitmap(Bitmap bm, float width, float height) {
if (bm == null) {
return null;
}
int bmWidth = bm.getWidth();
int bmHeight = bm.getHeight();
float scaleWidth = width / bmWidth;
float scaleHeight = height / bmHeight;
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
if (scaleWidth == 1 && scaleHeight == 1) {
return bm;
} else {
Bitmap resizeBitmap = Bitmap.createBitmap(bm, 0, 0, bmWidth,
bmHeight, matrix, false);
bm.recycle();//回收图片内存
bm.setDensity(240);
return resizeBitmap;
}
}
public static Bitmap getViewBitmap(View view) {
// Log.e("宽",view.getWidth()+"");
// Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
// Canvas canvas = new Canvas(bitmap);
// view.draw(canvas);
// view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
// view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
view.buildDrawingCache();
Bitmap bitmap = view.getDrawingCache();
return bitmap;
}
多次运行截屏显示同一张图片
问题原因是因为View的缓存机制造成的,解决方法清除缓存即可
view.destroyDrawingCache();执行这个方法,释放view的缓存资源