Android截屏表面视图显示黑屏

Android截屏表面视图显示黑屏

我试图通过代码拍摄我的游戏的截图,并通过一个意图来分享它。我能做这些事情,但是截图总是看起来是黑色的。下面是与分享截图相关的代码:

View view = MainActivity.getView();view.setDrawingCacheEnabled(true);Bitmap screen = Bitmap.createBitmap(view.getDrawingCache(true));.. save Bitmap

这是MainActivity中的内容:

view = new GameView(this);view.setLayoutParams(new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.FILL_PARENT,
            RelativeLayout.LayoutParams.FILL_PARENT));public static SurfaceView getView() {
    return view;}

以及视图本身:

public class GameView extends SurfaceView implements SurfaceHolder.Callback {private static SurfaceHolder surfaceHolder;...etc

我就是这样画所有东西的:

Canvas canvas = surfaceHolder.lockCanvas(null);
        if (canvas != null) {
                Game.draw(canvas);...

好的,基于一些答案,我构建了:

public static void share() {
    Bitmap screen = GameView.SavePixels(0, 0, Screen.width, Screen.height);
    Calendar c = Calendar.getInstance();
    Date d = c.getTime();
    String path = Images.Media.insertImage(
            Game.context.getContentResolver(), screen, "screenShotBJ" + d                    + ".png", null);
    System.out.println(path + " PATH");
    Uri screenshotUri = Uri.parse(path);
    final Intent emailIntent = new Intent(
            android.content.Intent.ACTION_SEND);
    emailIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    emailIntent.putExtra(Intent.EXTRA_STREAM, screenshotUri);
    emailIntent.setType("image/png");
    Game.context.startActivity(Intent.createChooser(emailIntent,
            "Share High Score:"));}


镜头仍然是黑色的。我拯救它的方式有什么问题吗?

我尝试过几种不同的方法来获取屏幕快照,但是没有一种方法起作用:上面代码中显示的方法是最常用的方法。但这似乎行不通。这是使用SurfaceView的问题吗?如果是这样的话,如果我不能使用它,为什么view.getDrawinCache(True)还存在,以及如何解决这个问题?


谢谢。


慕森王
浏览 2455回答 3
3回答
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Android