Android截圖緩存、螢幕畫面擷取實作
Android截圖緩存、螢幕畫面擷取實作
全螢幕, 安卓, 截圖, 緩存, 螢幕畫面, 擷取, 體感、震動、截圖, android, Bitmap, Cache, DrawingCache, Screen, ScreenShot, Shot
public class MainActivity extends Activity
{
//擷取畫面按鈕
private Button mBtn;
//截圖的畫面
private ImageView mImg;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//取得Button與ImageView元件
mBtn = (Button) findViewById(R.id.btn);
mImg = (ImageView) findViewById(R.id.img);
//點擊按鈕觸發
mBtn.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
//將截圖Bitmap放入ImageView
mImg.setImageBitmap(getScreenShot());
}
});
}
//將全螢幕畫面轉換成Bitmap
private Bitmap getScreenShot()
{
//藉由View來Cache全螢幕畫面後放入Bitmap
View mView = getWindow().getDecorView();
mView.setDrawingCacheEnabled(true);
mView.buildDrawingCache();
Bitmap mFullBitmap = mView.getDrawingCache();
//取得系統狀態列高度
Rect mRect = new Rect();
getWindow().getDecorView().getWindowVisibleDisplayFrame(mRect);
int mStatusBarHeight = mRect.top;
//取得手機螢幕長寬尺寸
int mPhoneWidth = getWindowManager().getDefaultDisplay().getWidth();
int mPhoneHeight = getWindowManager().getDefaultDisplay().getHeight();
//將狀態列的部分移除並建立新的Bitmap
Bitmap mBitmap = Bitmap.createBitmap(mFullBitmap, 0, mStatusBarHeight, mPhoneWidth, mPhoneHeight – mStatusBarHeight);
//將Cache的畫面清除
mView.destroyDrawingCache();
return mBitmap;
}
}