java - Android如何在屏幕截图中捕获阴影

标签 java android screenshot shadow

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

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

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

欣赏任何替代方法 enter image description here

结果

enter image description here

最佳答案

试试这个。

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

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

您可以传递任何 View ,如 RelativeLayout、LinearLayout 等任何 View 都可以传递给 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);
}

关于java - Android如何在屏幕截图中捕获阴影,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46220221/

相关文章:

php - 从发布的屏幕截图数据 php 中删除 img src

java - Spring Boot 2 和迁移 OAuth2 配置

java - 缺少字符(此行有多个标记

java - Eclipse IDE 如何解析文件/资源​​位置?

Java 命令流程链生成器

android - putExtra treeMap 返回 HashMap cannot be cast to TreeMap android

android - View 的屏幕截图始终为空。安卓

android - Android 聊天消息的可排序唯一 ID

android - 在我的 Android 应用程序中实现没有 ActionBarSherlock 的 Holoeverywhere 主题

Android:如何在 root 设备上获取帧缓冲区(屏幕截图)?