我想捕获表面 View 的图像,并在表面 View 上绘制内容。 在我的应用程序中,我有表面 View ,用户可以通过在其上拖放一些对象来绘制一些位图。 之后,单击按钮,我想将该 SurfaceView 的图像及其位图内容存储在 SD 卡中。
对于这些,我有一个 Canvas 对象,所有位图都绘制在其上。 下面是我的代码 -
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);
如何做到这一点。
提前致谢。
已编辑
下面是我的代码,它仅捕获我在 xml 中创建的 View ,而不绘制我在表面上绘制的内容。
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;
}
已编辑我用于绘制的孔 fragment 如下-
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();
}
}
}
我有用户此代码,但它不起作用,所以我将表面 View 放入其他 View 中,并尝试从我之前发布的位置捕获位图。我尝试了两种方法,但没有得到任何解决方案。
最佳答案
创建位图,为位图创建 Canvas ,在该 Canvas 上执行所有绘图命令,保存位图。
您无法捕获 SurfaceView 的表面,因此重新绘制到位图上是最简单的方法。这种情况经常出现;参见例如this .
关于android - 捕获带有位图内容的 SurfaceView 的屏幕截图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27437480/