安卓開發_實現截圖功能

來源:互聯網
上載者:User

標籤:

4-17號在360雲端硬碟提供了 群雄兵法APP的下載,將其作為測試版,在短短10天內便下載量超過1000,雖說不是很高,但是也證明我一個月的努力是值得的。

 

這兩天遊戲更新了,我也將推出最新的版本,在原來的測試版本的基礎上,修改了一些內容的錯誤,最佳化了部分介面,添加了武將生平介紹,武將成長值等資料。

 

因為廣大的使用者提出裝備類比之後無法儲存的問題,我在自己所學的基礎上進行的解決,但是無奈沒有解決成功,於是乎先用功能來實現裝備類比後的屬性儲存吧。

 

網上百度了很多,大部分相當複雜,對於我這個水平來說,目前還是無法消化的,尋找了幾天,終於找到一個簡單的實現方法。

 

其實就是一個函數,這個函數實現了功能

 

 1     /** 2     * 擷取和儲存當前螢幕的 3     */ 4     private void GetandSaveCurrentImage()   5     {   6     //構建Bitmap   7     WindowManager windowManager = getWindowManager();   8     Display display = windowManager.getDefaultDisplay();   9     int w = display.getWidth();  10     int h = display.getHeight();  11     Bitmap Bmp = Bitmap.createBitmap( w, h, Config.ARGB_8888 );      12     //擷取螢幕  13     View decorview = this.getWindow().getDecorView();   14     decorview.setDrawingCacheEnabled(true);   15     Bmp = decorview.getDrawingCache();   16     //圖片儲存路徑17     String SavePath = getSDCardPath()+"/qxbf/ScreenImages";  //這裡是儲存的路徑18     //儲存Bitmap   19     try {  20     File path = new File(SavePath);  21      Time time = new Time("GMT+8");     //這裡求出了手機系統當前的時間,用來給截出的圖片作為名字。否則名字相同,就只會產生一個圖片,要想產生多個圖片,便需要每個                                                 圖片的名字不同,我就用最水的辦法,用系統時間來命名了22      time.setToNow();    23      int year = time.year;    24      int month = time.month;    25      int day = time.monthDay;    26      int minute = time.minute;    27      int hour = time.hour;    28      int sec = time.second; 29     //檔案  30     String filepath = SavePath+"/" + year+month+day+minute+sec+".png";   //這裡給圖片命名  31     File file = new File(filepath);  32     if(!path.exists()){   //判斷路徑是否存在33     path.mkdirs();  34     }  35     if (!file.exists()) {  36     file.createNewFile();    37     }  38     FileOutputStream fos = null;  39     fos = new FileOutputStream(file);  40     if (null != fos) {  41     Bmp.compress(Bitmap.CompressFormat.PNG, 90, fos);  42     fos.flush();  43     fos.close();    44     Toast.makeText(Equip_pk_result.this, "截屏檔案已儲存至SDCard/qxbf/ScreenImages/目錄下",Toast.LENGTH_LONG).show();  45     }  46     } catch (Exception e) {  47     e.printStackTrace();  48     }  49     }  50     /**51     * 擷取SDCard的目錄路徑功能52     * @return53     */54     private String getSDCardPath(){55     File sdcardDir = null;56     //判斷SDCard是否存在57     boolean sdcardExist = Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);58     if(sdcardExist){59     sdcardDir = Environment.getExternalStorageDirectory();60     }61     return sdcardDir.toString();62     

與函數對應的還需要添加一個系統許可權

在AndroidManifest.xml檔案下 

1 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

 

 

具體使用就是點擊一個按鈕,在按鈕的響應事件裡調用這個函數就可以了。

安卓開發_實現功能

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.