【问题标题】:How to take screenshot of visible area of horizontalscrollview in android?如何在android中截取水平滚动视图的可见区域?
【发布时间】:2015-09-15 06:19:58
【问题描述】:

我正在开发一个 Android 应用程序,我想截取水平滚动视图的可见区域的屏幕截图。我为此使用以下代码:-

public Bitmap screenShot(View view) {

        /*Rect rect = new Rect(0, 0, 
                mDiviceWidth , view.getHeight());*/

        /*Rect rect = new Rect(view.getScrollX(), 0, 
                view.getScrollX() + mDiviceWidth , view.getHeight());*/

        Rect scrollBounds = new Rect();
    //  view.getHitRect(scrollBounds);
    //  view.getDrawingRect(scrollBounds);
    //  view.getLocalVisibleRect(scrollBounds);
    //  view.getGlobalVisibleRect(scrollBounds);
        view.getWindowVisibleDisplayFrame(scrollBounds);
        Log.e("X Scrolled", ""+view.getScrollX());
        Log.e("left", ""+scrollBounds.left);
        Log.e("right", ""+scrollBounds.right);
        Log.e("top", ""+scrollBounds.top);
        Log.e("bottom", ""+scrollBounds.bottom);
        /*view.setDrawingCacheEnabled(true);
        Bitmap bitmap = view.getDrawingCache();*/
        Bitmap bitmap = Bitmap.createBitmap(view.getWidth(),
                view.getHeight(), Config.ARGB_8888);

        /*float left,right,top,bottom;

        left = view.getLeft();
        right =   mDiviceWidth;
        top = 0;
        bottom = view.getHeight();*/

        Canvas canvas = new Canvas(bitmap);
    //  canvas.clipRect(left, top, 
    //          right , bottom);
        canvas.clipRect(scrollBounds);
        view.draw(canvas);
    //  view.setDrawingCacheEnabled(false);
        return bitmap;

    } 

以上代码截取屏幕截图但返回错误区域。有时它会捕获可见区域,但有时它会捕获不可见或部分可见的随机区域。我已经用过

//  view.getHitRect(scrollBounds);
//  view.getDrawingRect(scrollBounds);
//  view.getLocalVisibleRect(scrollBounds);
//  view.getGlobalVisibleRect(scrollBounds);

方法但没有得到完美的结果。

所以请帮我解决这个问题。

【问题讨论】:

    标签: android view screenshot horizontalscrollview


    【解决方案1】:

    使用此功能:

    private static Bitmap takeScreenShot(Activity activity) {
        View view = activity.getWindow().getDecorView();
        view.setDrawingCacheEnabled(true);
        view.buildDrawingCache();
        Bitmap b1 = view.getDrawingCache();
        Rect frame = new Rect();
        activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
        int statusBarHeight = frame.top;
        int width = activity.getWindowManager().getDefaultDisplay().getWidth();
        int height = activity.getWindowManager().getDefaultDisplay().getHeight();
    
        Bitmap b = Bitmap.createBitmap(b1, 0, statusBarHeight, width, height  - statusBarHeight);
        view.destroyDrawingCache();
        return b;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-26
      • 1970-01-01
      • 2016-09-14
      • 1970-01-01
      • 1970-01-01
      • 2012-10-05
      • 1970-01-01
      相关资源
      最近更新 更多