【问题标题】:How to make the Image Background white after screenshot?截图后如何使图像背景变白?
【发布时间】:2019-11-26 08:28:53
【问题描述】:

我想截取框架布局并使用意图共享它。问题是截图后,我的图像背景变黑了。任何人都可以请帮助解决这个问题。我想让背景变成正常的白色。

这是我的代码

public void sharePetrol()
{
    bitmap = screenShot(layPetrol);
    shareBitmap(bitmap);
}

public Bitmap screenShot(View view)
{
    view.measure(View.MeasureSpec.makeMeasureSpec(view.getWidth(), View.MeasureSpec.EXACTLY), View.MeasureSpec.makeMeasureSpec(view.getHeight(), View.MeasureSpec.EXACTLY));
    view.layout((int) view.getX(), (int) view.getY(), (int) view.getX() + view.getMeasuredWidth(), (int) view.getY() + view.getMeasuredHeight());
    view.setDrawingCacheEnabled(true);
    view.buildDrawingCache(true);

    Bitmap createBitmap = Bitmap.createBitmap(view.getDrawingCache());
    view.setDrawingCacheEnabled(false);

    return createBitmap;
}

public void shareBitmap(Bitmap bitmap)
{
    Date now = new Date();
    android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now);

    String fileName = now + ".jpg";
    String directoryPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Screenshots";

    File directory = new File(directoryPath);

    if(!directory.exists())
    {
        directory.mkdir();
    }

    File imageFile = new File(directoryPath, fileName);

    try
    {
        FileOutputStream outputStream = new FileOutputStream(imageFile);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
        outputStream.flush();
        outputStream.close();

        Uri uri = Uri.fromFile(imageFile);

        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("image/jpeg");
        intent.putExtra(Intent.EXTRA_STREAM, uri);

        try
        {
            startActivity(Intent.createChooser(intent, "Share Screenshot"));
        }
        catch (ActivityNotFoundException e)
        {
            Toast.makeText(PetrolActivity.this, "No App Available", Toast.LENGTH_SHORT).show();
        }
    }
    catch (FileNotFoundException e)
    {
        e.printStackTrace();
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
}

`

这是我在截屏后得到的结果

【问题讨论】:

    标签: android share screenshot


    【解决方案1】:

    我已经使用这种方法来处理我的问题。

      public Bitmap screenShot(View view)
        {
        view.measure(View.MeasureSpec.makeMeasureSpec(view.getWidth(), View.MeasureSpec.EXACTLY), View.MeasureSpec.makeMeasureSpec(view.getHeight(), View.MeasureSpec.EXACTLY));
        view.layout((int) view.getX(), (int) view.getY(), (int) view.getX() + view.getMeasuredWidth(), (int) view.getY() + view.getMeasuredHeight());
        view.setDrawingCacheEnabled(true);
        view.buildDrawingCache(true);
        view.setBackgroundColor(ContextCompat.getColor(this, color.white)); //set background color
    
        Bitmap createBitmap = Bitmap.createBitmap(view.getDrawingCache());
        view.setDrawingCacheEnabled(false);
    
        return createBitmap;
    }
    

    【讨论】:

    • 如何保持原来的背景?我没有将背景设置为任何颜色
    • 这很可能会保持不变,但如果你想在设置它之前将背景存储在变量中,一旦你创建了位图而不是使用 view.getBackground() 将其恢复为原始状态
    【解决方案2】:

    您可以尝试使用 Canvas 设置背景颜色,添加以下 3 行:

    public Bitmap screenShot(View view)
    {
        view.measure(View.MeasureSpec.makeMeasureSpec(view.getWidth(), View.MeasureSpec.EXACTLY), View.MeasureSpec.makeMeasureSpec(view.getHeight(), View.MeasureSpec.EXACTLY));
        view.layout((int) view.getX(), (int) view.getY(), (int) view.getX() + view.getMeasuredWidth(), (int) view.getY() + view.getMeasuredHeight());
        view.setDrawingCacheEnabled(true);
        view.buildDrawingCache(true);
    
        Bitmap createBitmap = Bitmap.createBitmap(view.getDrawingCache());
        Canvas canvas = new Canvas(createBitmap); // Add these lines
        canvas.drawColor(Color.WHITE); // Add these lines
        view.draw(canvas); // Add these lines
        view.setDrawingCacheEnabled(false);
    
        return createBitmap;
    }
    

    【讨论】:

    • 不错!希望对你有帮助
    猜你喜欢
    • 1970-01-01
    • 2021-04-28
    • 2015-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多