android 開發之ScrollView 截屏代碼

來源:互聯網
上載者:User

在開發過程中,經常要用到分享功能,有時需要截取當前螢幕的畫面,一起分享出去。如果當前Activity高度固定,不能滑動,截取螢幕代碼網上到處都是,這裡就不貼出了;如果當前Activity嵌套有可滑動子控制項,如ScrollView或ListView,想要連未顯示的部分一起截下來,前一種方法就行不通了,這裡貼出一段有效代碼:

 /**     * 截取scrollview的螢幕     * **/    public static Bitmap getBitmapByView(ScrollView scrollView) {        int h = 0;        Bitmap bitmap = null;        // 擷取listView實際高度        for (int i = 0; i < scrollView.getChildCount(); i++) {            h += scrollView.getChildAt(i).getHeight();            scrollView.getChildAt(i).setBackgroundResource(R.drawable.bg3);        }        Log.d(TAG, "實際高度:" + h);        Log.d(TAG, " 高度:" + scrollView.getHeight());        // 建立對應大小的bitmap        bitmap = Bitmap.createBitmap(scrollView.getWidth(), h,                Bitmap.Config.ARGB_8888);        final Canvas canvas = new Canvas(bitmap);        scrollView.draw(canvas);        // 測試輸出        FileOutputStream out = null;        try {            out = new FileOutputStream("/sdcard/screen_test.png");        } catch (FileNotFoundException e) {            e.printStackTrace();        }        try {            if (null != out) {                bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);                out.flush();                out.close();            }        } catch (IOException e) {            // TODO: handle exception        }        return bitmap;    }

附Demo:點擊開啟連結

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.