java – Android如何在截图中捕获阴影

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

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

即使我们在活动级别添加硬件加速,它也不会产生任何影响.

欣赏任何替代方法
《java – Android如何在截图中捕获阴影》

结果

《java – Android如何在截图中捕获阴影》

最佳答案 试试这个.

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