Android滾動視圖截屏代碼

jopen 10年前發布 | 49K 次閱讀 Android Android開發 移動開發

在開發過程中,經常要用到分享功能,有時需要截取當前屏幕的畫面,一起分享出去。如果當前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;
}</pre> 來自:http://blog.csdn.net/laoziyueguo3/article/details/19078605
 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!