对透明活动进行屏幕截图后出现黑屏

时间:2019-05-04 10:19:55

标签: java android

我正在尝试制作一个用于在无根android kitkat设备上截屏的应用,当我制作该应用时,它可以拍摄屏幕截图,但颜色为黑色,因为我正在使用透明活动在另一个应用上拍摄屏幕截图(例如:铬)。任何人都有解决这个问题的想法? (对不起,英语不好)

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.vocaloid.miku.screenshotit">

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

    <application
        android:name=".App"
        android:allowBackup="true"
        android:icon="@drawable/smile"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".Main2Activity" android:theme="@style/TransparentCompat"/>
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service android:name=".ScreenshotService" />
    </application>

</manifest>

activity_main2.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".Main2Activity"
    android:orientation="vertical">

    <Button
        android:id="@+id/btnTakeScreenShot"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Take Screenshot" />
        <!--android:background="@android:color/transparent"/>-->

    <!--that button is only for take screenshot, in final that button will transparent with no text in button and has width and height equal match parent, so it like hidden button on full screen -->

</LinearLayout>

styles.xml

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <style name="TransparentCompat" parent="@style/Theme.AppCompat.Light.NoActionBar">
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:windowIsTranslucent">true</item>
    </style>

</resources>

Main2Activity.java

...

public class Main2Activity extends AppCompatActivity {
    Button btnTakeScreenShot;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); //to make activity running in full screen mode
        setContentView(R.layout.activity_main2);

        btnTakeScreenShot = findViewById(R.id.btnTakeScreenShot);

        btnTakeScreenShot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                takeScreenshot(); //trigger takeScreenshot method
                finish(); //to close app after screenshot without user notice
            }
        });
    }

    protected void takeScreenshot() { //to take screenshot
        Date now = new Date();
        android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now);

        try {
            // image naming and path  to include sd card  appending name you choose for file
            String mPath = Environment.getExternalStorageDirectory().toString() + "/" + now + ".jpg";

            // create bitmap screen capture
            View v1 = getWindow().getDecorView().getRootView();
            v1.setDrawingCacheEnabled(true);
            Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
            v1.setDrawingCacheEnabled(false);

            File imageFile = new File(mPath);

            FileOutputStream outputStream = new FileOutputStream(imageFile);
            int quality = 100;
            bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);
            outputStream.flush();
            outputStream.close();
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }
}

<---项目结束--->

所以目标是我想为另一个应用程序(例如chrome等)拍摄屏幕快照,该应用程序使用透明度活动和透明度完整按钮,用户无需搜索就可以点击任何地方以截屏。但是实际的最终屏幕截图只是黑色,我想应该做透明活动,我注意到了,因为当我使用背景按钮默认值(不是透明)并且使用大小等于换行内容的按钮时,其余的活动都变成了黑色不像是在截屏之前用眼睛看到的东西。

0 个答案:

没有答案