java - 截取特定 LinearLayout 的屏幕截图

标签 java android android-linearlayout screenshot android-bitmap

我正在尝试截取内部有 ImageView 的 LinearLayout 的屏幕截图并截取该图像的屏幕截图,但是当我按下按钮时,它会截取整个屏幕的屏幕截图,我只想保存 LinearLayout自从我寻找解决方案近 2 天以来,里面就有图像

使用的代码:

 LinearLayout ll;

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_meme, container, false);
        Button downloadButton = view.findViewById(R.id.downloadButton);
        ll = view.findViewById(R.id.rootView);

        downloadButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bitmap bitmap = takeScreenshot();
                saveBitmap(bitmap);
            }
        });

 return view;

}

public Bitmap takeScreenshot() {

        View rootView=ll.getRootView();
        rootView.setDrawingCacheEnabled(true);
        return rootView.getDrawingCache();

    }
    public void saveBitmap(Bitmap bitmap) {

        try {
            FileOutputStream fos = new FileOutputStream(new File(Environment
                    .getExternalStorageDirectory().toString(), "SCREEN"
                    + System.currentTimeMillis() + ".png"));
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
            fos.flush();
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

xml:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".fragments.MemesFragment">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".ProfileActivity">

        <Button
            android:id="@+id/downloadButton"
            android:layout_width="41dp"
            android:layout_height="41dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="8dp"
            android:drawableBottom="@drawable/download"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <LinearLayout
            android:id="@+id/rootView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <androidx.viewpager2.widget.ViewPager2
                android:id="@+id/memeRandomView"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintVertical_bias="0.0"
                tools:layout_editor_absoluteX="0dp">
            </androidx.viewpager2.widget.ViewPager2>
        </LinearLayout>


    </androidx.constraintlayout.widget.ConstraintLayout>


</FrameLayout>

最佳答案

像这样将 View 转换为位图

引用号:Convert view to bitmap on Android

public static Bitmap getBitmapFromView(View view) {
        //Define a bitmap with the same size as the view
        Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),Bitmap.Config.ARGB_8888);
        //Bind a canvas to it
        Canvas canvas = new Canvas(returnedBitmap);
        //Get the view's background
        Drawable bgDrawable =view.getBackground();
        if (bgDrawable!=null) 
            //has background drawable, then draw it on the canvas
            bgDrawable.draw(canvas);
        else 
            //does not have background drawable, then draw white background on the canvas
            canvas.drawColor(Color.WHITE);
        // draw the view on the canvas
        view.draw(canvas);
        //return the bitmap
        return returnedBitmap;
    }

然后使用此位图并将其保存在外部,就像您一样。

关于java - 截取特定 LinearLayout 的屏幕截图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67063849/

相关文章:

javascript - Nativescript {N} oAuth 插件 - 检索用户 Facebook 数据

java - Android Activity 转换动画仅适用于被调用的 Activity

android - 如何从已部署的数据中检索数据以在 Android 应用程序中使用?

javascript - 无法在 ionic 3 的多个页面中注入(inject)提供程序

java - View.Gone 位于适配器内部,在 onBindViewHolder 中留下空间

java - 在相对布局中将宽度和高度设置为百分比

java - 使用同步方法测试两个线程递增单个int的结果令人困惑

java - 以 o(n) 或 o(1) 的时间复杂度对整数数组进行排序

java - 什么时候应该在项目生命周期中使用 mvn release?

android - marginTop 和 marginBottom 不适用于 LinearLayout