使用位图内容捕获SurfaceView的屏幕截图

时间:2014-12-12 05:35:27

标签: android android-canvas surfaceview android-bitmap

我想捕获表面视图的图像,并在表面视图上绘制内容。 在我的应用程序中,我有表面视图,用户可以通过拖放一些对象来绘制一些位图。 在按钮单击之后,我想在sd卡中存储该表面视图的图像及其位图内容。

对于这些我有一个画布对象,并在其上绘制所有位图。 下面是我的代码 -

                mHolder = mSurfaceView.getHolder();
                mHolder.lockCanvas();
                canvas.drawColor(0,Mode.CLEAR);
                //border's properties
                paint = new Paint();
                FontMetrics fm = new FontMetrics();
                Paint paint2 = new Paint();
                paint2.setColor(Color.WHITE);
                paint2.setTextSize(30);
                paint2.setTextAlign(Paint.Align.CENTER);
                paint2.getFontMetrics(fm);  
                canvas.drawText("Building", RectLeft+60, RectTop+60, paint2);
                paint.setStyle(Paint.Style.STROKE);
                paint.setColor(color);
                paint.setStrokeWidth(3);
                canvas.drawRect(RectLeft, RectTop, RectRight, RectBottom, paint);
                mHolder.unlockCanvasAndPost(canvas);

怎么做。

提前致谢。

EDITED

下面是我的代码,它只捕获我在xml中创建的视图,而不是绘制我在表面上绘制的内容。

public static Bitmap getBitmapFromView(View view) {
    //Define a bitmap with the same size as the view
    Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),Bitmap.Config.ARGB_8888);
    //Bind a canvas to it
    Canvas canvas = new Canvas(returnedBitmap);
    //Get the view's background
    Drawable bgDrawable =view.getBackground();
    if (bgDrawable!=null) 
        //has background drawable, then draw it on the canvas
        bgDrawable.draw(canvas);
    else 
        //does not have background drawable, then draw white background on the canvas
        canvas.drawColor(Color.WHITE);
    // draw the view on the canvas
    view.draw(canvas);
    //return the bitmap
    return returnedBitmap;
}

已编辑我的绘图孔片段位于 -

public class DrawingLayoutFragment extends Fragment implements SurfaceHolder.Callback{

      @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
  ....
  ....
   mSurfaceView = (SurfaceView) view.findViewById(R.id.surfaceview);
  init();
  return view;
}

public void init(){
    mSurfaceView.getHolder().addCallback(this);
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
        int height) {
        RectLeft = width/2 - 60;
        RectTop = height/2 - 60 ;
        RectRight = width/2 + 60;
        RectBottom = height/2 + 60;
        DrawFocusRect(RectLeft, RectTop, RectRight, RectBottom, getResources().getColor(com.nep.R.color.white_color));
}

@Override
public void surfaceCreated(SurfaceHolder holder) {
    mHolder = mSurfaceView.getHolder();
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    try{
        if(mHolder!=null){
            mHolder.lockCanvas();
            canvas = null;
            mHolder.addCallback(null);
            mHolder = null;
        }
    }catch(Exception e){
        e.printStackTrace();
    }
}

 private void DrawFocusRect(float RectLeft, float RectTop, float RectRight, float RectBottom, int color){
         try {
                if(canvas!=null){
                    mHolder.lockCanvas();
                }
                else{
                    canvas = mHolder.lockCanvas();
                }
                canvas.drawColor(0,Mode.CLEAR);
                paint = new Paint();
                FontMetrics fm = new FontMetrics();
                Paint paint2 = new Paint();
                paint2.setColor(Color.WHITE);
                paint2.setTextSize(30);
                paint2.setTextAlign(Paint.Align.CENTER);
                paint2.getFontMetrics(fm);  
                canvas.drawText("Building", RectLeft+60, RectTop+60, paint2);
                paint.setStyle(Paint.Style.STROKE);
                paint.setColor(color);
                paint.setStrokeWidth(3);
                canvas.drawRect(RectLeft, RectTop, RectRight, RectBottom, paint);
                mHolder.unlockCanvasAndPost(canvas);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
      public void createFIle(){
    try {
        DrawFocusRect(RectLeft, RectTop, RectRight, RectBottom, getResources().getColor(com.nep.R.color.white_color));
         Bitmap b = Bitmap.createBitmap(mSurfaceView.getWidth(), mSurfaceView.getHeight(), Bitmap.Config.ARGB_8888); 
         Canvas c = new Canvas(b);
         mSurfaceView.draw(c);

    //  Bitmap bmp = getBitmapFromView(frame);

        String mFile = mPath+"_layout011.png";
        File directory = new File (mFile);
        directory.createNewFile();
        OutputStream fo = new FileOutputStream(directory);
        ByteArrayOutputStream os = new ByteArrayOutputStream();

        b.compress(CompressFormat.JPEG, 100, os);
        os.writeTo(fo);
        fo.close();
        os.close();
    //  mSurfaceView.destroyDrawingCache();
        BasicMethods.showToast("Saved at - "+mFile, mContext);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
}

我有这个代码的用户并且它不起作用,所以我把我的表面视图放在其他视图中并尝试从我之前发布的那个中捕获位图。我已经尝试了两种方式但没有找到任何解决方案。

1 个答案:

答案 0 :(得分:3)

创建位图,为位图创建Canvas,在该Canvas上执行所有绘图命令,保存位图。

您无法捕捉SurfaceView的表面,因此重新绘制到Bitmap是最简单的方法。这经常出现;见例如this