简体   繁体   English

Android Studio(Chipmunk)屏幕截图图像变暗

[英]Android studio (Chipmunk) screen capture image gets darken

I am programming a simple drawing application with drawLines:我正在用 drawLines 编写一个简单的绘图应用程序:

public void onDraw(Canvas canvas) {
        setBackgroundColor(cfgBackground); //background
        //canvas.drawColor(ConfigActivity.getCfgBackground(ConfigActivity.cfgBackground)); //to keep background when export to PNG/JPG
        //title
        paint.setColor(0xbb020a0d);
        paint.setTextSize(40);
        canvas.drawText(Title, 0, 40, paint);

        paint.setColor(color);
        paint.setStrokeWidth(thickness);
        canvas.drawLines(lines, paint);
}

This is no problem.这没有问题。 When export the screen to image file, the image shows normal in the preview screen (with all other photos in the phone), but when open this image, it shows darken, as following:将屏幕导出为图片文件时,图片在预览屏幕中显示正常(与手机中的所有其他照片一样),但打开此图片时,显示变暗,如下所示: 在此处输入图像描述

I have tried Method A/B/C like following, but the result not much different:我已经尝试过如下方法 A/B/C,但结果差别不大:

        File sdCard = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
        File file = new File(sdCard, imageName);
        FileOutputStream fos = null;
        try {
            // Make sure the Pictures directory exists.
            sdCard.mkdirs();

            fos = new FileOutputStream(file);  
        } catch (FileNotFoundException e) {
            Toast.makeText(getContext(), file + " open failed: " + e, Toast.LENGTH_SHORT).show();
            e.printStackTrace();
            return;
        }

        /*  //method A: will get dark image
        setDrawingCacheEnabled(true);
        Bitmap b = getDrawingCache();
        //b.setHasAlpha(true); //to set transparency
        //Canvas canvas = new Canvas(b);
        //canvas.drawColor(cfgBackground)); //trying to solve darken image
        b.compress(Bitmap.CompressFormat.PNG, 100, fos);
        setDrawingCacheEnabled(false); //for next screen update
        */
        Bitmap b = screenShot(this); //Method B
        b.compress(Bitmap.CompressFormat.PNG, 100, fos);
        try {
            fos.flush();
            fos.close();
        } catch (IOException e) {
            Toast.makeText(getContext(), file + " save failed: " + e, Toast.LENGTH_SHORT).show();
            e.printStackTrace();
        }
        Toast.makeText(getContext(), file + " saved in Albums", Toast.LENGTH_SHORT).show();
        //add to gallary
        getContext().sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + file.toString())));

Two functions:两个功能:

    //Method C
    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(cfgBackground);
        // draw the view on the canvas
        view.draw(canvas);
        //return the bitmap
        return returnedBitmap;
    }
    //Method B
    public Bitmap screenShot(View view) {  
        Bitmap bitmap = Bitmap.createBitmap(view.getWidth(),
                view.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        view.draw(canvas);
        return bitmap;
    }

Anyone encountered this problem?有人遇到过这个问题吗?

I finally found the solution (although not sure why): the background color should be 0xFFrrggbb, eg.我终于找到了解决方案(虽然不知道为什么):背景颜色应该是 0xFFrrggbb,例如。 0xffeeee00, though the background looks ugly but just needs more finetune... If you know why, kindly share here, appreciated! 0xffeeee00,虽然背景看起来很丑,但需要更多的微调......如果你知道为什么,请在这里分享,不胜感激!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM