【问题标题】:Android how to capture shadow in screenshotsAndroid如何在屏幕截图中捕获阴影
【发布时间】:2017-09-14 13:16:36
【问题描述】:

如何在屏幕截图中捕获活动布局中视图的阴影或高度。此代码为视图截取屏幕截图,但未显示视图的阴影在此处输入图像描述

View screenView = parentMain;
    screenView.buildDrawingCache();
    screenView.setDrawingCacheEnabled(true);
    Bitmap bitmap = Bitmap.createBitmap(screenView.getWidth() , screenView.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(bitmap);
    screenView.layout(0, 0, screenView.getLayoutParams().width, screenView.getLayoutParams().height);
    screenView.draw(c);
    screenView.setDrawingCacheEnabled(false);
    fakeImgView.setImageBitmap(bitmap);

即使我们在活动级别添加硬件加速,它也不会提供任何效果。

欣赏任何替代方法

结果

【问题讨论】:

  • 尝试将您的图像保存在存储目录中,然后将该图像设置为您的图像视图。
  • 见我的回答先生希望这会帮助你。
  • @commonsware 请帮忙
  • 对此有任何解决方案吗?

标签: java android screenshot shadow


【解决方案1】:

试试这个。

CardView card = (CardView) findViewById(R.id.card);

现在只需将卡片传递给 captureScreenShot()。它返回位图并保存该位图 saveImage()。

您可以传递任何视图,如 RelativeLayout、LinearLayout 等,任何视图都可以传递给 captureScreenShot()。

// Function which capture Screenshot
public Bitmap captureScreenShot(View view) {
    /*
     * Creating a Bitmap of view with ARGB_4444.
     * */
    Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_4444);
    Canvas canvas = new Canvas(bitmap);
    Drawable backgroundDrawable = view.getBackground();

    if (backgroundDrawable != null) {
        backgroundDrawable.draw(canvas);
    } else {
        canvas.drawColor(Color.parseColor("#80000000"));
    }
    view.draw(canvas);
    return bitmap;
}

// Function which Save image.
private void saveImage(Bitmap bitmap) {
    File file = // Your Storage directory name + your filename
    if (file == null) {
        return;
    }
    try {
        FileOutputStream fos = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
        fos.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

最后这样调用这个函数。

saveImage(captureScreenShot(card));

现在像这样设置你的图像。

File file = new  File(“yourImageFilePath”);
if(file.exists())
{
    yourImageView.setImageURI(Uri.fromFile(file));
}

注意:如果 setImageURI() 不起作用,那么您可以使用以下代码。

File file = new  File(“yourImageFilePath”);
if(file.exists())
{
  Bitmap bitmap = BitmapFactory.decodeFile(file.toString());
  yourImageView.setImageBitmap(bitmap);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-20
    • 1970-01-01
    • 1970-01-01
    • 2014-05-31
    • 2023-03-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多