Android如何在截屏中捕捉阴影

4
如何在活动布局中捕获视图的阴影或高程以进行截图。此代码对视图进行截图,但未显示视图的阴影。输入图像说明在此处。
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);

即使在活动级别添加硬件加速也没有任何效果。
感谢任何替代方法。输入图像描述 结果。

enter image description here


尝试将您的图像保存在存储目录中,然后将该图像设置为您的图像视图。 - Andy Developer
希望这能对您有所帮助,先生。 - Andy Developer
@commonsware 请帮忙 - Akshay Mukadam
你有解决方案吗? - Parth Anjaria
1个回答

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);
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接