public class ScreenshotDemoActivity extends Activity {
private Button shotButton;
private ImageView imageView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
shotButton=(Button)findViewById(R.id.shotButton);
imageView=(ImageView)findViewById(R.id.imageView);
shotButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//Bitmap-->Drawable
BitmapDrawable bd=new BitmapDrawable(shot());
imageView.setBackgroundDrawable(bd);
// imageView.setImageBitmap(shot());
}
});
}
/**
* 截屏方法
* @return
*/
private Bitmap shot() {
View view = getWindow().getDecorView();
Display display = this.getWindowManager().getDefaultDisplay();
view.layout(0, 0, display.getWidth(), display.getHeight());
view.setDrawingCacheEnabled(true);//允许当前窗口保存缓存信息,这样getDrawingCache()方法才会返回一个Bitmap
Bitmap bmp = Bitmap.createBitmap(view.getDrawingCache());
return bmp;
}
}
android 截图方法
最新推荐文章于 2024-05-30 22:41:26 发布