3C科技 娛樂遊戲 美食旅遊 時尚美妝 親子育兒 生活休閒 金融理財 健康運動 寰宇綜合

Zi 字媒體

2017-07-25T20:27:27+00:00
加入好友
Android截圖緩存、螢幕畫面擷取實作 資料來源:http://dean-android.blogspot.tw/2015/01/android_27.html  全螢幕, 安卓, 截圖, 緩存, 螢幕畫面, 擷取, 體感、震動、截圖, 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;           }   }  

本文由jashliaoeuwordpress提供 原文連結

寫了 5860316篇文章,獲得 23313次喜歡
精彩推薦