├── .gitignore
├── .idea
├── .name
├── compiler.xml
├── copyright
│ └── profiles_settings.xml
├── encodings.xml
├── gradle.xml
├── misc.xml
├── modules.xml
└── runConfigurations.xml
├── LICENSE
├── README.md
├── app
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src
│ ├── androidTest
│ └── java
│ │ └── com
│ │ └── branch
│ │ └── www
│ │ └── screencapture
│ │ └── ApplicationTest.java
│ ├── main
│ ├── AndroidManifest.xml
│ ├── java
│ │ └── com
│ │ │ └── branch
│ │ │ └── www
│ │ │ └── screencapture
│ │ │ ├── FileUtil.java
│ │ │ ├── FloatWindowsService.java
│ │ │ ├── GlobalScreenShot.java
│ │ │ ├── MainActivity.java
│ │ │ ├── PreviewPictureActivity.java
│ │ │ └── ScreenCaptureApplication.java
│ └── res
│ │ ├── drawable
│ │ └── screenshot_panel.9.png
│ │ ├── layout
│ │ ├── activity_main.xml
│ │ ├── activity_preview_layout.xml
│ │ └── global_screenshot.xml
│ │ ├── mipmap-hdpi
│ │ └── ic_launcher.png
│ │ ├── mipmap-mdpi
│ │ └── ic_launcher.png
│ │ ├── mipmap-xhdpi
│ │ └── ic_launcher.png
│ │ ├── mipmap-xxhdpi
│ │ ├── ic_imagetool_crop.png
│ │ └── ic_launcher.png
│ │ ├── mipmap-xxxhdpi
│ │ └── ic_launcher.png
│ │ ├── values-w820dp
│ │ └── dimens.xml
│ │ └── values
│ │ ├── colors.xml
│ │ ├── dimens.xml
│ │ ├── strings.xml
│ │ └── styles.xml
│ └── test
│ └── java
│ └── com
│ └── branch
│ └── www
│ └── screencapture
│ └── ExampleUnitTest.java
├── build.gradle
├── gradle.properties
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
└── settings.gradle
/.gitignore:
--------------------------------------------------------------------------------
1 | # Built application files
2 | *.apk
3 | *.ap_
4 |
5 | # Files for the ART/Dalvik VM
6 | *.dex
7 |
8 | # Java class files
9 | *.class
10 |
11 | # Generated files
12 | bin/
13 | gen/
14 | out/
15 |
16 | # Gradle files
17 | .gradle/
18 | build/
19 |
20 | # Local configuration file (sdk path, etc)
21 | local.properties
22 |
23 | # Proguard folder generated by Eclipse
24 | proguard/
25 |
26 | # Log Files
27 | *.log
28 |
29 | # Android Studio Navigation editor temp files
30 | .navigation/
31 |
32 | # Android Studio captures folder
33 | captures/
34 |
35 | # Intellij
36 | *.iml
37 | .idea/workspace.xml
38 |
39 | # Keystore files
40 | *.jks
41 |
42 | *.iml
43 | .gradle
44 | /local.properties
45 | /.idea/workspace.xml
46 | /.idea/libraries
47 | .DS_Store
48 | /build
49 | /captures
50 |
--------------------------------------------------------------------------------
/.idea/.name:
--------------------------------------------------------------------------------
1 | ScreenCapture
--------------------------------------------------------------------------------
/.idea/compiler.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/.idea/copyright/profiles_settings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
--------------------------------------------------------------------------------
/.idea/encodings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/gradle.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
22 |
23 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 | 1.7
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
--------------------------------------------------------------------------------
/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/.idea/runConfigurations.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "{}"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright {yyyy} {name of copyright owner}
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
202 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | ###在Android 5.0,API 21 之前想要截图系统屏幕必须Root才能完成,5.0之后开放了接口,下面看我们是怎么实现的。
2 | --
3 | ### 1. 涉及到的相关类
4 | >#### 1. `MediaProjectionManager`
5 | >官方原话: Manages the retrieval of certain types of {@link MediaProjection} tokens.
6 | >这个类通过 `Context#getSystemService` 中`MEDIA_PROJECTION_SERVICE` 获取,他的功能就是获取`MediaProjection`
7 | >#### 2. `MediaProjection`
8 | >官方原话:A token granting applications the ability to capture screen contents and/or record system audio. The exact capabilities granted depend on the type of MediaProjection.在这个类中我们能获取到屏幕的内容
9 | >####
10 | >#### 3. `ImageReader`
11 | >官方原话:The ImageReader class allows direct application access to image data
12 | >rendered into a {@link android.view.Surface}
13 | 通过这个类我们可以把`Surface`转换成图片
14 |
15 | ### 2. 上面三个类就可以完成我们截取屏幕图片的操作,那么下面我们将解释他们是怎么合作完成的
16 | >#### 1. 首先获取用户授权,截图屏幕需要用户手动授权后才能操作
17 | >
18 | > @TargetApi(Build.VERSION_CODES.LOLLIPOP)
19 | > public void requestCapturePermission() {
20 | >
21 | > if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
22 | > //5.0 之后才允许使用屏幕截图
23 | >
24 | > return;
25 | > }
26 | >
27 | > MediaProjectionManager mediaProjectionManager = (MediaProjectionManager)
28 | > getSystemService(Context.MEDIA_PROJECTION_SERVICE);
29 | > startActivityForResult(
30 | > mediaProjectionManager.createScreenCaptureIntent(),
31 | > REQUEST_MEDIA_PROJECTION);
32 | > }
33 |
34 | >
35 | > 这里必须使用`startActivityForResult` 因为在`createScreenCaptureIntent()` 方法中会返回用户授权截取屏幕的结果,用户根据下面弹窗允许或者拒绝
36 |
37 | > 
38 | >
39 | > 用户选择后在Activity 的`onActivityResult` 中操作返回的结果data
40 | >
41 | > @Override
42 | > protected void onActivityResult(int requestCode, int resultCode, Intent data) {
43 | > super.onActivityResult(requestCode, resultCode, data);
44 | >
45 | > switch (requestCode) {
46 | > case REQUEST_MEDIA_PROJECTION:
47 | >
48 | > if (resultCode == RESULT_OK && data != null) {
49 | > FloatWindowsService.setResultData(data);
50 | > startService(new Intent(getApplicationContext(), FloatWindowsService.class));
51 | > }
52 | >
53 | > break;
54 | > }
55 | > }
56 | >
57 | >#### 这里我是用`FloatWindowsService`在桌面上显示一个悬浮按钮,点击截屏,下面我们看在`FloatWindowsService` 是如何实现截图
58 | ### 2. 截取屏幕内容生成Bitmap
59 | >#### 首先创建`ImageReader`实例
60 |
61 | > private void createImageReader() {
62 | >
63 | > mImageReader = ImageReader.newInstance(mScreenWidth, mScreenHeight, PixelFormat.RGBA_8888, 2);
64 | >
65 | > }
66 | >#### 然后点击事件中触发`startScreenShot()`
67 |
68 | > private void startScreenShot() {
69 | >
70 | > mFloatView.setVisibility(View.GONE);
71 | >
72 | > Handler handler = new Handler();
73 | > handler.postDelayed(new Runnable() {
74 | > public void run() {
75 | > //获取当前屏幕内容
76 | > startVirtual();
77 | > }
78 | > }, 5);
79 | >
80 | > handler.postDelayed(new Runnable() {
81 | > public void run() {
82 | > //生成图片保存到本地
83 | > startCapture();
84 | >
85 | > }
86 | > }, 30);
87 | > }
88 |
89 | >#### 在`startVirtual()` 方法中我们做一件事,就是获取当前屏幕内容
90 |
91 | > public void startVirtual() {
92 | > if (mMediaProjection != null) {
93 | > virtualDisplay();
94 | > } else {
95 | > setUpMediaProjection();
96 | > virtualDisplay();
97 | > }
98 | > }
99 |
100 | >#### 与此同时需要获取`MediaProjection` 实例,而`mResultData` 是授权后返回的结果
101 |
102 | > public void setUpMediaProjection() {
103 | > if (mResultData == null) {
104 | > Intent intent = new Intent(Intent.ACTION_MAIN);
105 | > intent.addCategory(Intent.CATEGORY_LAUNCHER);
106 | > startActivity(intent);
107 | > } else {
108 | //mResultData是在Activity中用户授权后返回的结果
109 | > mMediaProjection = getMediaProjectionManager().getMediaProjection(Activity.RESULT_OK, mResultData);
110 | > }
111 | > }
112 | >
113 | >#### 最终得到当前屏幕的内容,注意这里`mImageReader.getSurface()`被传入,屏幕的数据也将会在ImageReader中的Surface中
114 | >
115 | > private void virtualDisplay() {
116 | mVirtualDisplay = mMediaProjection.createVirtualDisplay("screen-mirror",
117 | mScreenWidth, mScreenHeight, mScreenDensity, DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR,
118 | mImageReader.getSurface(), null, null);
119 | }
120 | >
121 | >#### 最后把`mImageReader`得到的屏幕内容数据转换成图片,在AsyncTask中处理,
122 | >##### `Image.Plane`中的 buffer 数据并不是完全是Bitmap所需要的,需要注意下面3点
123 |
124 | >###### 1. Image 设置的图片格式与Bitmap设置的必须一致
125 | >###### 2. 缓冲数据存在行间距,所以我们必须去除这些间距
126 | >###### 3. Image 使用后必须调用`image.close();`关闭,否则再次使用会报错
127 | >
128 | > @Override
129 | > protected Bitmap doInBackground(Image... params) {
130 | >
131 | > if (params == null || params.length < 1 || params[0] == null) {
132 | >
133 | > return null;
134 | > }
135 | >
136 | > Image image = params[0];
137 | >
138 | > int width = image.getWidth();
139 | > int height = image.getHeight();
140 | > final Image.Plane[] planes = image.getPlanes();
141 | > final ByteBuffer buffer = planes[0].getBuffer();
142 | //每个像素的间距
143 | > int pixelStride = planes[0].getPixelStride();
144 | //总的间距
145 | > int rowStride = planes[0].getRowStride();
146 | > int rowPadding = rowStride - pixelStride * width;
147 | > Bitmap bitmap = Bitmap.createBitmap(width + rowPadding / pixelStride, height, Bitmap.Config.ARGB_8888);
148 | > bitmap.copyPixelsFromBuffer(buffer);
149 | > bitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height);
150 | > image.close();
151 | >
152 | >
153 | >##### 最后把生成的bitmap保存起来,就ok了
154 | >
155 | >###[源码](https://github.com/goodbranch/ScreenCapture)
156 | >
157 | >###[APK](https://raw.githubusercontent.com/goodbranch/AndroidNote/master/note/screenshot/ScreenCapture.apk)
158 | >
159 | >
160 |
--------------------------------------------------------------------------------
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 21
5 | buildToolsVersion "23.0.3"
6 |
7 | defaultConfig {
8 | applicationId "com.branch.www.screencapture"
9 | minSdkVersion 21
10 | targetSdkVersion 21
11 | versionCode 1
12 | versionName "1.0"
13 | }
14 | buildTypes {
15 | release {
16 | minifyEnabled false
17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
18 | }
19 | }
20 | }
21 |
22 | dependencies {
23 | compile fileTree(dir: 'libs', include: ['*.jar'])
24 | testCompile 'junit:junit:4.12'
25 | compile 'com.android.support:appcompat-v7:21.0.0'
26 | }
27 |
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in E:\BaiduYunDownload\adt-bundle-windows-x86_64-20140702\sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/com/branch/www/screencapture/ApplicationTest.java:
--------------------------------------------------------------------------------
1 | package com.branch.www.screencapture;
2 |
3 | import android.app.Application;
4 | import android.test.ApplicationTestCase;
5 |
6 | /**
7 | * Testing Fundamentals
8 | */
9 | public class ApplicationTest extends ApplicationTestCase {
10 | public ApplicationTest() {
11 | super(Application.class);
12 | }
13 | }
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
27 |
28 |
31 |
32 |
33 |
34 |
35 |
36 |
--------------------------------------------------------------------------------
/app/src/main/java/com/branch/www/screencapture/FileUtil.java:
--------------------------------------------------------------------------------
1 | package com.branch.www.screencapture;
2 |
3 | import android.content.Context;
4 | import android.os.Environment;
5 |
6 | import java.io.File;
7 | import java.text.SimpleDateFormat;
8 | import java.util.Date;
9 |
10 | /**
11 | * Created by ryze on 2016-5-26.
12 | */
13 | public class FileUtil {
14 |
15 | //系统保存截图的路径
16 | public static final String SCREENCAPTURE_PATH = "ScreenCapture" + File.separator + "Screenshots" + File.separator;
17 | // public static final String SCREENCAPTURE_PATH = "ZAKER" + File.separator + "Screenshots" + File.separator;
18 |
19 | public static final String SCREENSHOT_NAME = "Screenshot";
20 |
21 | public static String getAppPath(Context context) {
22 |
23 | if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
24 |
25 |
26 | return Environment.getExternalStorageDirectory().toString();
27 |
28 | } else {
29 |
30 | return context.getFilesDir().toString();
31 | }
32 |
33 | }
34 |
35 |
36 | public static String getScreenShots(Context context) {
37 |
38 | StringBuffer stringBuffer = new StringBuffer(getAppPath(context));
39 | stringBuffer.append(File.separator);
40 |
41 | stringBuffer.append(SCREENCAPTURE_PATH);
42 |
43 | File file = new File(stringBuffer.toString());
44 |
45 | if (!file.exists()) {
46 | file.mkdirs();
47 | }
48 |
49 | return stringBuffer.toString();
50 |
51 | }
52 |
53 | public static String getScreenShotsName(Context context) {
54 |
55 | SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd-hh-mm-ss");
56 |
57 | String date = simpleDateFormat.format(new Date());
58 |
59 | StringBuffer stringBuffer = new StringBuffer(getScreenShots(context));
60 | stringBuffer.append(SCREENSHOT_NAME);
61 | stringBuffer.append("_");
62 | stringBuffer.append(date);
63 | stringBuffer.append(".png");
64 |
65 | return stringBuffer.toString();
66 |
67 | }
68 |
69 |
70 | }
71 |
--------------------------------------------------------------------------------
/app/src/main/java/com/branch/www/screencapture/FloatWindowsService.java:
--------------------------------------------------------------------------------
1 | package com.branch.www.screencapture;
2 |
3 | import android.annotation.TargetApi;
4 | import android.app.Activity;
5 | import android.app.Service;
6 | import android.content.Context;
7 | import android.content.Intent;
8 | import android.graphics.Bitmap;
9 | import android.graphics.BitmapFactory;
10 | import android.graphics.PixelFormat;
11 | import android.hardware.display.DisplayManager;
12 | import android.hardware.display.VirtualDisplay;
13 | import android.media.Image;
14 | import android.media.ImageReader;
15 | import android.media.projection.MediaProjection;
16 | import android.media.projection.MediaProjectionManager;
17 | import android.net.Uri;
18 | import android.os.AsyncTask;
19 | import android.os.Build;
20 | import android.os.Handler;
21 | import android.os.IBinder;
22 | import android.support.v4.os.AsyncTaskCompat;
23 | import android.util.DisplayMetrics;
24 | import android.util.Log;
25 | import android.view.GestureDetector;
26 | import android.view.Gravity;
27 | import android.view.MotionEvent;
28 | import android.view.View;
29 | import android.view.WindowManager;
30 | import android.widget.ImageView;
31 |
32 | import java.io.File;
33 | import java.io.FileNotFoundException;
34 | import java.io.FileOutputStream;
35 | import java.io.IOException;
36 | import java.nio.ByteBuffer;
37 |
38 | /**
39 | * Created by branch on 2016-5-25.
40 | *
41 | * 启动悬浮窗界面
42 | */
43 | @TargetApi(Build.VERSION_CODES.LOLLIPOP)
44 | public class FloatWindowsService extends Service {
45 |
46 |
47 | public static Intent newIntent(Context context, Intent mResultData) {
48 |
49 | Intent intent = new Intent(context, FloatWindowsService.class);
50 |
51 | if (mResultData != null) {
52 | intent.putExtras(mResultData);
53 | }
54 | return intent;
55 | }
56 |
57 | private MediaProjection mMediaProjection;
58 | private VirtualDisplay mVirtualDisplay;
59 |
60 | private static Intent mResultData = null;
61 |
62 |
63 | private ImageReader mImageReader;
64 | private WindowManager mWindowManager;
65 | private WindowManager.LayoutParams mLayoutParams;
66 | private GestureDetector mGestureDetector;
67 |
68 | private ImageView mFloatView;
69 |
70 | private int mScreenWidth;
71 | private int mScreenHeight;
72 | private int mScreenDensity;
73 |
74 |
75 | @Override
76 | public void onCreate() {
77 | super.onCreate();
78 |
79 | createFloatView();
80 |
81 | createImageReader();
82 | }
83 |
84 | public static Intent getResultData() {
85 | return mResultData;
86 | }
87 |
88 | public static void setResultData(Intent mResultData) {
89 | FloatWindowsService.mResultData = mResultData;
90 | }
91 |
92 | @Override
93 | public IBinder onBind(Intent intent) {
94 | return null;
95 | }
96 |
97 | private void createFloatView() {
98 | mGestureDetector = new GestureDetector(getApplicationContext(), new FloatGestrueTouchListener());
99 | mLayoutParams = new WindowManager.LayoutParams();
100 | mWindowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
101 |
102 | DisplayMetrics metrics = new DisplayMetrics();
103 | mWindowManager.getDefaultDisplay().getMetrics(metrics);
104 | mScreenDensity = metrics.densityDpi;
105 | mScreenWidth = metrics.widthPixels;
106 | mScreenHeight = metrics.heightPixels;
107 |
108 | mLayoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
109 | mLayoutParams.format = PixelFormat.RGBA_8888;
110 | // 设置Window flag
111 | mLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
112 | | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
113 | mLayoutParams.gravity = Gravity.LEFT | Gravity.TOP;
114 | mLayoutParams.x = mScreenWidth;
115 | mLayoutParams.y = 100;
116 | mLayoutParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
117 | mLayoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
118 |
119 |
120 | mFloatView = new ImageView(getApplicationContext());
121 | mFloatView.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_imagetool_crop));
122 | mWindowManager.addView(mFloatView, mLayoutParams);
123 |
124 |
125 | mFloatView.setOnTouchListener(new View.OnTouchListener() {
126 | @Override
127 | public boolean onTouch(View v, MotionEvent event) {
128 | return mGestureDetector.onTouchEvent(event);
129 | }
130 | });
131 |
132 | }
133 |
134 |
135 | private class FloatGestrueTouchListener implements GestureDetector.OnGestureListener {
136 | int lastX, lastY;
137 | int paramX, paramY;
138 |
139 | @Override
140 | public boolean onDown(MotionEvent event) {
141 | lastX = (int) event.getRawX();
142 | lastY = (int) event.getRawY();
143 | paramX = mLayoutParams.x;
144 | paramY = mLayoutParams.y;
145 | return true;
146 | }
147 |
148 | @Override
149 | public void onShowPress(MotionEvent e) {
150 |
151 | }
152 |
153 | @Override
154 | public boolean onSingleTapUp(MotionEvent e) {
155 | startScreenShot();
156 | return true;
157 | }
158 |
159 | @Override
160 | public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
161 | int dx = (int) e2.getRawX() - lastX;
162 | int dy = (int) e2.getRawY() - lastY;
163 | mLayoutParams.x = paramX + dx;
164 | mLayoutParams.y = paramY + dy;
165 | // 更新悬浮窗位置
166 | mWindowManager.updateViewLayout(mFloatView, mLayoutParams);
167 | return true;
168 | }
169 |
170 | @Override
171 | public void onLongPress(MotionEvent e) {
172 |
173 | }
174 |
175 | @Override
176 | public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
177 | return false;
178 | }
179 | }
180 |
181 |
182 | private void startScreenShot() {
183 |
184 | mFloatView.setVisibility(View.GONE);
185 |
186 | Handler handler1 = new Handler();
187 | handler1.postDelayed(new Runnable() {
188 | public void run() {
189 | //start virtual
190 | startVirtual();
191 | }
192 | }, 5);
193 |
194 | handler1.postDelayed(new Runnable() {
195 | public void run() {
196 | //capture the screen
197 | startCapture();
198 |
199 | }
200 | }, 30);
201 | }
202 |
203 |
204 | private void createImageReader() {
205 |
206 | mImageReader = ImageReader.newInstance(mScreenWidth, mScreenHeight, PixelFormat.RGBA_8888, 1);
207 |
208 | }
209 |
210 | public void startVirtual() {
211 | if (mMediaProjection != null) {
212 | virtualDisplay();
213 | } else {
214 | setUpMediaProjection();
215 | virtualDisplay();
216 | }
217 | }
218 |
219 | public void setUpMediaProjection() {
220 | if (mResultData == null) {
221 | Intent intent = new Intent(Intent.ACTION_MAIN);
222 | intent.addCategory(Intent.CATEGORY_LAUNCHER);
223 | startActivity(intent);
224 | } else {
225 | mMediaProjection = getMediaProjectionManager().getMediaProjection(Activity.RESULT_OK, mResultData);
226 | }
227 | }
228 |
229 | private MediaProjectionManager getMediaProjectionManager() {
230 |
231 | return (MediaProjectionManager) getSystemService(Context.MEDIA_PROJECTION_SERVICE);
232 | }
233 |
234 | private void virtualDisplay() {
235 | mVirtualDisplay = mMediaProjection.createVirtualDisplay("screen-mirror",
236 | mScreenWidth, mScreenHeight, mScreenDensity, DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR,
237 | mImageReader.getSurface(), null, null);
238 | }
239 |
240 | private void startCapture() {
241 |
242 | Image image = mImageReader.acquireLatestImage();
243 |
244 | if (image == null) {
245 | startScreenShot();
246 | } else {
247 | SaveTask mSaveTask = new SaveTask();
248 | AsyncTaskCompat.executeParallel(mSaveTask, image);
249 | }
250 | }
251 |
252 |
253 | public class SaveTask extends AsyncTask {
254 |
255 | @Override
256 | protected Bitmap doInBackground(Image... params) {
257 |
258 | if (params == null || params.length < 1 || params[0] == null) {
259 |
260 | return null;
261 | }
262 |
263 | Image image = params[0];
264 |
265 | int width = image.getWidth();
266 | int height = image.getHeight();
267 | final Image.Plane[] planes = image.getPlanes();
268 | final ByteBuffer buffer = planes[0].getBuffer();
269 | //每个像素的间距
270 | int pixelStride = planes[0].getPixelStride();
271 | //总的间距
272 | int rowStride = planes[0].getRowStride();
273 | int rowPadding = rowStride - pixelStride * width;
274 | Bitmap bitmap = Bitmap.createBitmap(width + rowPadding / pixelStride, height, Bitmap.Config.ARGB_8888);
275 | bitmap.copyPixelsFromBuffer(buffer);
276 | bitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height);
277 | image.close();
278 | File fileImage = null;
279 | if (bitmap != null) {
280 | try {
281 | fileImage = new File(FileUtil.getScreenShotsName(getApplicationContext()));
282 | if (!fileImage.exists()) {
283 | fileImage.createNewFile();
284 | }
285 | FileOutputStream out = new FileOutputStream(fileImage);
286 | if (out != null) {
287 | bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
288 | out.flush();
289 | out.close();
290 | Intent media = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
291 | Uri contentUri = Uri.fromFile(fileImage);
292 | media.setData(contentUri);
293 | sendBroadcast(media);
294 | }
295 | } catch (FileNotFoundException e) {
296 | e.printStackTrace();
297 | fileImage = null;
298 | } catch (IOException e) {
299 | e.printStackTrace();
300 | fileImage = null;
301 | }
302 | }
303 |
304 | if (fileImage != null) {
305 | return bitmap;
306 | }
307 | return null;
308 | }
309 |
310 | @Override
311 | protected void onPostExecute(Bitmap bitmap) {
312 | super.onPostExecute(bitmap);
313 | //预览图片
314 | if (bitmap != null) {
315 |
316 | ((ScreenCaptureApplication) getApplication()).setmScreenCaptureBitmap(bitmap);
317 | Log.e("ryze", "获取图片成功");
318 | startActivity(PreviewPictureActivity.newIntent(getApplicationContext()));
319 | }
320 |
321 | mFloatView.setVisibility(View.VISIBLE);
322 |
323 | }
324 | }
325 |
326 |
327 | private void tearDownMediaProjection() {
328 | if (mMediaProjection != null) {
329 | mMediaProjection.stop();
330 | mMediaProjection = null;
331 | }
332 | }
333 |
334 | private void stopVirtual() {
335 | if (mVirtualDisplay == null) {
336 | return;
337 | }
338 | mVirtualDisplay.release();
339 | mVirtualDisplay = null;
340 | }
341 |
342 | @Override
343 | public void onDestroy() {
344 | // to remove mFloatLayout from windowManager
345 | super.onDestroy();
346 | if (mFloatView != null) {
347 | mWindowManager.removeView(mFloatView);
348 | }
349 | stopVirtual();
350 |
351 | tearDownMediaProjection();
352 | }
353 |
354 |
355 | }
356 |
--------------------------------------------------------------------------------
/app/src/main/java/com/branch/www/screencapture/GlobalScreenShot.java:
--------------------------------------------------------------------------------
1 | package com.branch.www.screencapture;
2 |
3 | /*
4 | * Copyright (C) 2011 The Android Open Source Project
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License.
17 | */
18 |
19 | import android.animation.Animator;
20 | import android.animation.AnimatorListenerAdapter;
21 | import android.animation.AnimatorSet;
22 | import android.animation.ValueAnimator;
23 | import android.animation.ValueAnimator.AnimatorUpdateListener;
24 | import android.content.Context;
25 | import android.content.res.Resources;
26 | import android.graphics.Bitmap;
27 | import android.graphics.PixelFormat;
28 | import android.graphics.PointF;
29 | import android.media.MediaActionSound;
30 | import android.net.Uri;
31 | import android.util.DisplayMetrics;
32 | import android.view.Display;
33 | import android.view.LayoutInflater;
34 | import android.view.MotionEvent;
35 | import android.view.View;
36 | import android.view.ViewGroup;
37 | import android.view.WindowManager;
38 | import android.view.animation.Interpolator;
39 | import android.widget.ImageView;
40 |
41 | /**
42 | * POD used in the AsyncTask which saves an image in the background.
43 | */
44 | class SaveImageInBackgroundData {
45 | Context context;
46 | Bitmap image;
47 | Uri imageUri;
48 | Runnable finisher;
49 | int iconSize;
50 | int result;
51 |
52 | void clearImage() {
53 | image = null;
54 | imageUri = null;
55 | iconSize = 0;
56 | }
57 |
58 | void clearContext() {
59 | context = null;
60 | }
61 | }
62 |
63 |
64 | /**
65 | * TODO: - Performance when over gl surfaces? Ie. Gallery - what do we say in the Toast? Which icon
66 | * do we get if the user uses another type of gallery?
67 | */
68 | class GlobalScreenshot {
69 | private static final String TAG = "GlobalScreenshot";
70 |
71 | private static final int SCREENSHOT_FLASH_TO_PEAK_DURATION = 130;
72 | private static final int SCREENSHOT_DROP_IN_DURATION = 430;
73 | private static final int SCREENSHOT_DROP_OUT_DELAY = 500;
74 | private static final int SCREENSHOT_DROP_OUT_DURATION = 430;
75 | private static final int SCREENSHOT_DROP_OUT_SCALE_DURATION = 370;
76 | private static final int SCREENSHOT_FAST_DROP_OUT_DURATION = 320;
77 | private static final float BACKGROUND_ALPHA = 0.5f;
78 | private static final float SCREENSHOT_SCALE = 1f;
79 | private static final float SCREENSHOT_DROP_IN_MIN_SCALE = SCREENSHOT_SCALE * 0.725f;
80 | private static final float SCREENSHOT_DROP_OUT_MIN_SCALE = SCREENSHOT_SCALE * 0.45f;
81 | private static final float SCREENSHOT_FAST_DROP_OUT_MIN_SCALE = SCREENSHOT_SCALE * 0.6f;
82 | private static final float SCREENSHOT_DROP_OUT_MIN_SCALE_OFFSET = 0f;
83 |
84 | private Context mContext;
85 | private WindowManager mWindowManager;
86 | private WindowManager.LayoutParams mWindowLayoutParams;
87 | private Display mDisplay;
88 | private DisplayMetrics mDisplayMetrics;
89 |
90 | private Bitmap mScreenBitmap;
91 | private View mScreenshotLayout;
92 | private ImageView mBackgroundView;
93 | private ImageView mScreenshotView;
94 | private ImageView mScreenshotFlash;
95 |
96 | private AnimatorSet mScreenshotAnimation;
97 |
98 | private float mBgPadding;
99 | private float mBgPaddingScale;
100 |
101 | private MediaActionSound mCameraSound;
102 |
103 |
104 | private onScreenShotListener mOnScreenShotListener;
105 |
106 | /**
107 | * @param context everything needs a context :(
108 | */
109 | public GlobalScreenshot(Context context) {
110 | Resources r = context.getResources();
111 | mContext = context;
112 | LayoutInflater layoutInflater = (LayoutInflater)
113 | context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
114 |
115 | // Inflate the screenshot layout
116 | mScreenshotLayout = layoutInflater.inflate(R.layout.global_screenshot, null);
117 | mBackgroundView = (ImageView) mScreenshotLayout.findViewById(R.id.global_screenshot_background);
118 | mScreenshotView = (ImageView) mScreenshotLayout.findViewById(R.id.global_screenshot);
119 | mScreenshotFlash = (ImageView) mScreenshotLayout.findViewById(R.id.global_screenshot_flash);
120 | mScreenshotLayout.setFocusable(true);
121 | mScreenshotLayout.setOnTouchListener(new View.OnTouchListener() {
122 | @Override
123 | public boolean onTouch(View v, MotionEvent event) {
124 | // Intercept and ignore all touch events
125 | return true;
126 | }
127 | });
128 |
129 | // Setup the window that we are going to use
130 | mWindowLayoutParams = new WindowManager.LayoutParams(
131 | ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, 0, 0,
132 | WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
133 | WindowManager.LayoutParams.FLAG_FULLSCREEN
134 | | WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED
135 | | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
136 | | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED,
137 | PixelFormat.TRANSLUCENT);
138 | mWindowLayoutParams.setTitle("ScreenshotAnimation");
139 | mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
140 |
141 | mDisplay = mWindowManager.getDefaultDisplay();
142 | mDisplayMetrics = new DisplayMetrics();
143 | mDisplay.getRealMetrics(mDisplayMetrics);
144 |
145 | // Scale has to account for both sides of the bg
146 | mBgPadding = (float) r.getDimensionPixelSize(R.dimen.global_screenshot_bg_padding);
147 | mBgPaddingScale = mBgPadding / mDisplayMetrics.widthPixels;
148 |
149 | // Setup the Camera shutter sound
150 | mCameraSound = new MediaActionSound();
151 | mCameraSound.load(MediaActionSound.SHUTTER_CLICK);
152 | }
153 |
154 |
155 | /**
156 | * Takes a screenshot of the current display and shows an animation.
157 | */
158 | void takeScreenshot(Bitmap bitmap, onScreenShotListener onScreenShotListener, boolean statusBarVisible, boolean navBarVisible) {
159 | // Take the screenshot
160 | mScreenBitmap = bitmap;
161 | this.mOnScreenShotListener = onScreenShotListener;
162 |
163 | if (mOnScreenShotListener != null) {
164 | mOnScreenShotListener.onStartShot();
165 | }
166 |
167 | if (mScreenBitmap == null) {
168 | notifyScreenshotError(mContext);
169 | return;
170 | }
171 |
172 | // Optimizations
173 | mScreenBitmap.setHasAlpha(false);
174 | mScreenBitmap.prepareToDraw();
175 |
176 | // Start the post-screenshot animation
177 | startAnimation(mDisplayMetrics.widthPixels, mDisplayMetrics.heightPixels,
178 | statusBarVisible, navBarVisible);
179 | }
180 |
181 |
182 | /**
183 | * Starts the animation after taking the screenshot
184 | */
185 | private void startAnimation(int w, int h, boolean statusBarVisible,
186 | boolean navBarVisible) {
187 | // Add the view for the animation
188 | mScreenshotView.setImageBitmap(mScreenBitmap);
189 | mScreenshotLayout.requestFocus();
190 |
191 | // Setup the animation with the screenshot just taken
192 | if (mScreenshotAnimation != null) {
193 | mScreenshotAnimation.end();
194 | mScreenshotAnimation.removeAllListeners();
195 | }
196 |
197 | mWindowManager.addView(mScreenshotLayout, mWindowLayoutParams);
198 | ValueAnimator screenshotDropInAnim = createScreenshotDropInAnimation();
199 | ValueAnimator screenshotFadeOutAnim = createScreenshotDropOutAnimation(w, h,
200 | statusBarVisible, navBarVisible);
201 | mScreenshotAnimation = new AnimatorSet();
202 | mScreenshotAnimation.playSequentially(screenshotDropInAnim, screenshotFadeOutAnim);
203 | mScreenshotAnimation.addListener(new AnimatorListenerAdapter() {
204 | @Override
205 | public void onAnimationEnd(Animator animation) {
206 | // Save the screenshot once we have a bit of time now
207 | saveScreenshotInWorkerThread();
208 | mWindowManager.removeView(mScreenshotLayout);
209 |
210 | // Clear any references to the bitmap
211 | mScreenBitmap = null;
212 | mScreenshotView.setImageBitmap(null);
213 |
214 | }
215 | });
216 | mScreenshotLayout.post(new Runnable() {
217 | @Override
218 | public void run() {
219 | // Play the shutter sound to notify that we've taken a screenshot
220 | mCameraSound.play(MediaActionSound.SHUTTER_CLICK);
221 |
222 | mScreenshotView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
223 | mScreenshotView.buildLayer();
224 | mScreenshotAnimation.start();
225 | }
226 | });
227 | }
228 |
229 | private ValueAnimator createScreenshotDropInAnimation() {
230 | final float flashPeakDurationPct = ((float) (SCREENSHOT_FLASH_TO_PEAK_DURATION)
231 | / SCREENSHOT_DROP_IN_DURATION);
232 | final float flashDurationPct = 2f * flashPeakDurationPct;
233 | final Interpolator flashAlphaInterpolator = new Interpolator() {
234 | @Override
235 | public float getInterpolation(float x) {
236 | // Flash the flash view in and out quickly
237 | if (x <= flashDurationPct) {
238 | return (float) Math.sin(Math.PI * (x / flashDurationPct));
239 | }
240 | return 0;
241 | }
242 | };
243 | final Interpolator scaleInterpolator = new Interpolator() {
244 | @Override
245 | public float getInterpolation(float x) {
246 | // We start scaling when the flash is at it's peak
247 | if (x < flashPeakDurationPct) {
248 | return 0;
249 | }
250 | return (x - flashDurationPct) / (1f - flashDurationPct);
251 | }
252 | };
253 | ValueAnimator anim = ValueAnimator.ofFloat(0f, 1f);
254 | anim.setDuration(SCREENSHOT_DROP_IN_DURATION);
255 | anim.addListener(new AnimatorListenerAdapter() {
256 | @Override
257 | public void onAnimationStart(Animator animation) {
258 | mBackgroundView.setAlpha(0f);
259 | mBackgroundView.setVisibility(View.VISIBLE);
260 | mScreenshotView.setAlpha(0f);
261 | mScreenshotView.setTranslationX(0f);
262 | mScreenshotView.setTranslationY(0f);
263 | mScreenshotView.setScaleX(SCREENSHOT_SCALE + mBgPaddingScale);
264 | mScreenshotView.setScaleY(SCREENSHOT_SCALE + mBgPaddingScale);
265 | mScreenshotView.setVisibility(View.VISIBLE);
266 | mScreenshotFlash.setAlpha(0f);
267 | mScreenshotFlash.setVisibility(View.VISIBLE);
268 | }
269 |
270 | @Override
271 | public void onAnimationEnd(Animator animation) {
272 | mScreenshotFlash.setVisibility(View.GONE);
273 |
274 | // mScreenshotView.setScaleX(SCREENSHOT_SCALE);
275 | // mScreenshotView.setScaleY(SCREENSHOT_SCALE);
276 | }
277 | });
278 | anim.addUpdateListener(new AnimatorUpdateListener() {
279 | @Override
280 | public void onAnimationUpdate(ValueAnimator animation) {
281 | float t = (Float) animation.getAnimatedValue();
282 | float scaleT = (SCREENSHOT_SCALE + mBgPaddingScale)
283 | - scaleInterpolator.getInterpolation(t)
284 | * (SCREENSHOT_SCALE - SCREENSHOT_DROP_IN_MIN_SCALE);
285 | mBackgroundView.setAlpha(scaleInterpolator.getInterpolation(t) * BACKGROUND_ALPHA);
286 | mScreenshotView.setAlpha(t);
287 | mScreenshotView.setScaleX(scaleT);
288 | mScreenshotView.setScaleY(scaleT);
289 | mScreenshotFlash.setAlpha(flashAlphaInterpolator.getInterpolation(t));
290 | }
291 | });
292 | return anim;
293 | }
294 |
295 | private ValueAnimator createScreenshotDropOutAnimation(int w, int h, boolean statusBarVisible,
296 | boolean navBarVisible) {
297 | ValueAnimator anim = ValueAnimator.ofFloat(0f, 1f);
298 | anim.setStartDelay(SCREENSHOT_DROP_OUT_DELAY);
299 | anim.addListener(new AnimatorListenerAdapter() {
300 | @Override
301 | public void onAnimationEnd(Animator animation) {
302 | mBackgroundView.setVisibility(View.GONE);
303 | mScreenshotView.setVisibility(View.GONE);
304 | mScreenshotView.setLayerType(View.LAYER_TYPE_NONE, null);
305 | }
306 | });
307 |
308 | if (!statusBarVisible || !navBarVisible) {
309 | // There is no status bar/nav bar, so just fade the screenshot away in place
310 | anim.setDuration(SCREENSHOT_FAST_DROP_OUT_DURATION);
311 | anim.addUpdateListener(new AnimatorUpdateListener() {
312 | @Override
313 | public void onAnimationUpdate(ValueAnimator animation) {
314 | float t = (Float) animation.getAnimatedValue();
315 | float scaleT = (SCREENSHOT_DROP_IN_MIN_SCALE + mBgPaddingScale)
316 | - t * (SCREENSHOT_DROP_IN_MIN_SCALE - SCREENSHOT_FAST_DROP_OUT_MIN_SCALE);
317 | mBackgroundView.setAlpha((1f - t) * BACKGROUND_ALPHA);
318 | mScreenshotView.setAlpha(1f - t);
319 | mScreenshotView.setScaleX(scaleT);
320 | mScreenshotView.setScaleY(scaleT);
321 | }
322 | });
323 | } else {
324 | // In the case where there is a status bar, animate to the origin of the bar (top-left)
325 | final float scaleDurationPct = (float) SCREENSHOT_DROP_OUT_SCALE_DURATION
326 | / SCREENSHOT_DROP_OUT_DURATION;
327 | final Interpolator scaleInterpolator = new Interpolator() {
328 | @Override
329 | public float getInterpolation(float x) {
330 | if (x < scaleDurationPct) {
331 | // Decelerate, and scale the input accordingly
332 | return (float) (1f - Math.pow(1f - (x / scaleDurationPct), 2f));
333 | }
334 | return 1f;
335 | }
336 | };
337 |
338 | // Determine the bounds of how to scale
339 | float halfScreenWidth = (w - 2f * mBgPadding) / 2f;
340 | float halfScreenHeight = (h - 2f * mBgPadding) / 2f;
341 | final float offsetPct = SCREENSHOT_DROP_OUT_MIN_SCALE_OFFSET;
342 | final PointF finalPos = new PointF(
343 | -halfScreenWidth + (SCREENSHOT_DROP_OUT_MIN_SCALE + offsetPct) * halfScreenWidth,
344 | -halfScreenHeight + (SCREENSHOT_DROP_OUT_MIN_SCALE + offsetPct) * halfScreenHeight);
345 |
346 | // Animate the screenshot to the status bar
347 | anim.setDuration(SCREENSHOT_DROP_OUT_DURATION);
348 | anim.addUpdateListener(new AnimatorUpdateListener() {
349 | @Override
350 | public void onAnimationUpdate(ValueAnimator animation) {
351 | float t = (Float) animation.getAnimatedValue();
352 | float scaleT = (SCREENSHOT_DROP_IN_MIN_SCALE + mBgPaddingScale)
353 | - scaleInterpolator.getInterpolation(t)
354 | * (SCREENSHOT_DROP_IN_MIN_SCALE - SCREENSHOT_DROP_OUT_MIN_SCALE);
355 | mBackgroundView.setAlpha((1f - t) * BACKGROUND_ALPHA);
356 | mScreenshotView.setAlpha(1f - scaleInterpolator.getInterpolation(t));
357 | mScreenshotView.setScaleX(scaleT);
358 | mScreenshotView.setScaleY(scaleT);
359 | mScreenshotView.setTranslationX(t * finalPos.x);
360 | mScreenshotView.setTranslationY(t * finalPos.y);
361 | }
362 | });
363 | }
364 | return anim;
365 | }
366 |
367 | private void notifyScreenshotError(Context context) {
368 | if (mOnScreenShotListener != null) {
369 | mOnScreenShotListener.onFinishShot(false);
370 | }
371 | }
372 |
373 | private void saveScreenshotInWorkerThread() {
374 | if (mOnScreenShotListener != null) {
375 | mOnScreenShotListener.onFinishShot(true);
376 | }
377 | }
378 |
379 |
380 | public interface onScreenShotListener {
381 |
382 | public void onStartShot();
383 |
384 | public void onFinishShot(boolean success);
385 | }
386 | }
387 |
--------------------------------------------------------------------------------
/app/src/main/java/com/branch/www/screencapture/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.branch.www.screencapture;
2 |
3 | import android.content.Context;
4 | import android.content.Intent;
5 | import android.media.projection.MediaProjectionManager;
6 | import android.os.Build;
7 | import android.os.Bundle;
8 | import android.support.v4.app.FragmentActivity;
9 |
10 | public class MainActivity extends FragmentActivity {
11 |
12 |
13 | public static final int REQUEST_MEDIA_PROJECTION = 18;
14 |
15 |
16 | @Override
17 | protected void onCreate(Bundle savedInstanceState) {
18 | super.onCreate(savedInstanceState);
19 | setContentView(R.layout.activity_main);
20 |
21 | requestCapturePermission();
22 | }
23 |
24 |
25 | public void requestCapturePermission() {
26 |
27 | if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
28 | //5.0 之后才允许使用屏幕截图
29 |
30 | return;
31 | }
32 |
33 | MediaProjectionManager mediaProjectionManager = (MediaProjectionManager)
34 | getSystemService(Context.MEDIA_PROJECTION_SERVICE);
35 | startActivityForResult(
36 | mediaProjectionManager.createScreenCaptureIntent(),
37 | REQUEST_MEDIA_PROJECTION);
38 | }
39 |
40 | @Override
41 | protected void onActivityResult(int requestCode, int resultCode, Intent data) {
42 | super.onActivityResult(requestCode, resultCode, data);
43 |
44 | switch (requestCode) {
45 | case REQUEST_MEDIA_PROJECTION:
46 |
47 | if (resultCode == RESULT_OK && data != null) {
48 | FloatWindowsService.setResultData(data);
49 | startService(new Intent(getApplicationContext(), FloatWindowsService.class));
50 | }
51 | break;
52 | }
53 |
54 | }
55 |
56 | }
57 |
--------------------------------------------------------------------------------
/app/src/main/java/com/branch/www/screencapture/PreviewPictureActivity.java:
--------------------------------------------------------------------------------
1 | package com.branch.www.screencapture;
2 |
3 | import android.content.Context;
4 | import android.content.Intent;
5 | import android.graphics.Bitmap;
6 | import android.os.Bundle;
7 | import android.support.annotation.Nullable;
8 | import android.support.v4.app.FragmentActivity;
9 | import android.util.Log;
10 | import android.view.View;
11 | import android.widget.ImageView;
12 |
13 | /**
14 | *
15 | */
16 | public class PreviewPictureActivity extends FragmentActivity implements GlobalScreenshot.onScreenShotListener {
17 |
18 | public static final Intent newIntent(Context context) {
19 | Intent intent = new Intent(context, PreviewPictureActivity.class);
20 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
21 | return intent;
22 | }
23 |
24 | private ImageView mPreviewImageView;
25 |
26 |
27 | @Override
28 | protected void onCreate(@Nullable Bundle savedInstanceState) {
29 | super.onCreate(savedInstanceState);
30 |
31 | setContentView(R.layout.activity_preview_layout);
32 | mPreviewImageView = (ImageView) findViewById(R.id.preview_image);
33 |
34 | GlobalScreenshot screenshot = new GlobalScreenshot(getApplicationContext());
35 |
36 | Bitmap bitmap = ((ScreenCaptureApplication) getApplication()).getmScreenCaptureBitmap();
37 |
38 |
39 | Log.e("ryze", "预览图片");
40 | mPreviewImageView.setImageBitmap(bitmap);
41 | mPreviewImageView.setVisibility(View.GONE);
42 |
43 | if (bitmap != null) {
44 | screenshot.takeScreenshot(bitmap, this, true, true);
45 | }
46 |
47 | }
48 |
49 | @Override
50 | public void onStartShot() {
51 |
52 | }
53 |
54 | @Override
55 | public void onFinishShot(boolean success) {
56 | mPreviewImageView.setVisibility(View.VISIBLE);
57 | }
58 | }
59 |
--------------------------------------------------------------------------------
/app/src/main/java/com/branch/www/screencapture/ScreenCaptureApplication.java:
--------------------------------------------------------------------------------
1 | package com.branch.www.screencapture;
2 |
3 | import android.app.Application;
4 | import android.graphics.Bitmap;
5 |
6 | /**
7 | * Created by Ryze on 2016-7-20.
8 | */
9 | public class ScreenCaptureApplication extends Application {
10 |
11 |
12 | private Bitmap mScreenCaptureBitmap;
13 |
14 | @Override
15 | public void onCreate() {
16 | super.onCreate();
17 | }
18 |
19 |
20 | public Bitmap getmScreenCaptureBitmap() {
21 | return mScreenCaptureBitmap;
22 | }
23 |
24 | public void setmScreenCaptureBitmap(Bitmap mScreenCaptureBitmap) {
25 | this.mScreenCaptureBitmap = mScreenCaptureBitmap;
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/screenshot_panel.9.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/goodbranch/ScreenCapture/6169e3c82d6bf775df534d0a79fa2d307264acb4/app/src/main/res/drawable/screenshot_panel.9.png
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
12 |
13 |
17 |
18 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_preview_layout.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
12 |
13 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/global_screenshot.xml:
--------------------------------------------------------------------------------
1 |
2 |
16 |
19 |
24 |
31 |
36 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/goodbranch/ScreenCapture/6169e3c82d6bf775df534d0a79fa2d307264acb4/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/goodbranch/ScreenCapture/6169e3c82d6bf775df534d0a79fa2d307264acb4/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/goodbranch/ScreenCapture/6169e3c82d6bf775df534d0a79fa2d307264acb4/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_imagetool_crop.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/goodbranch/ScreenCapture/6169e3c82d6bf775df534d0a79fa2d307264acb4/app/src/main/res/mipmap-xxhdpi/ic_imagetool_crop.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/goodbranch/ScreenCapture/6169e3c82d6bf775df534d0a79fa2d307264acb4/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/goodbranch/ScreenCapture/6169e3c82d6bf775df534d0a79fa2d307264acb4/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/values-w820dp/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 64dp
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #3F51B5
4 | #303F9F
5 | #FF4081
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 16dp
4 | 16dp
5 | 10dp
6 |
7 |
8 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | ScreenCapture
3 |
4 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/app/src/test/java/com/branch/www/screencapture/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.branch.www.screencapture;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * To work on unit tests, switch the Test Artifact in the Build Variants view.
9 | */
10 | public class ExampleUnitTest {
11 | @Test
12 | public void addition_isCorrect() throws Exception {
13 | assertEquals(4, 2 + 2);
14 | }
15 | }
--------------------------------------------------------------------------------
/build.gradle:
--------------------------------------------------------------------------------
1 | // Top-level build file where you can add configuration options common to all sub-projects/modules.
2 |
3 | buildscript {
4 | repositories {
5 | jcenter()
6 | }
7 | dependencies {
8 | classpath 'com.android.tools.build:gradle:2.1.0'
9 |
10 | // NOTE: Do not place your application dependencies here; they belong
11 | // in the individual module build.gradle files
12 | }
13 | }
14 |
15 | allprojects {
16 | repositories {
17 | jcenter()
18 | }
19 | }
20 |
21 | task clean(type: Delete) {
22 | delete rootProject.buildDir
23 | }
24 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 |
3 | # IDE (e.g. Android Studio) users:
4 | # Gradle settings configured through the IDE *will override*
5 | # any settings specified in this file.
6 |
7 | # For more details on how to configure your build environment visit
8 | # http://www.gradle.org/docs/current/userguide/build_environment.html
9 |
10 | # Specifies the JVM arguments used for the daemon process.
11 | # The setting is particularly useful for tweaking memory settings.
12 | # Default value: -Xmx10248m -XX:MaxPermSize=256m
13 | # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
14 |
15 | # When configured, Gradle will run in incubating parallel mode.
16 | # This option should only be used with decoupled projects. More details, visit
17 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
18 | # org.gradle.parallel=true
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/goodbranch/ScreenCapture/6169e3c82d6bf775df534d0a79fa2d307264acb4/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Mon Dec 28 10:00:20 PST 2015
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.10-all.zip
7 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10 | DEFAULT_JVM_OPTS=""
11 |
12 | APP_NAME="Gradle"
13 | APP_BASE_NAME=`basename "$0"`
14 |
15 | # Use the maximum available, or set MAX_FD != -1 to use that value.
16 | MAX_FD="maximum"
17 |
18 | warn ( ) {
19 | echo "$*"
20 | }
21 |
22 | die ( ) {
23 | echo
24 | echo "$*"
25 | echo
26 | exit 1
27 | }
28 |
29 | # OS specific support (must be 'true' or 'false').
30 | cygwin=false
31 | msys=false
32 | darwin=false
33 | case "`uname`" in
34 | CYGWIN* )
35 | cygwin=true
36 | ;;
37 | Darwin* )
38 | darwin=true
39 | ;;
40 | MINGW* )
41 | msys=true
42 | ;;
43 | esac
44 |
45 | # Attempt to set APP_HOME
46 | # Resolve links: $0 may be a link
47 | PRG="$0"
48 | # Need this for relative symlinks.
49 | while [ -h "$PRG" ] ; do
50 | ls=`ls -ld "$PRG"`
51 | link=`expr "$ls" : '.*-> \(.*\)$'`
52 | if expr "$link" : '/.*' > /dev/null; then
53 | PRG="$link"
54 | else
55 | PRG=`dirname "$PRG"`"/$link"
56 | fi
57 | done
58 | SAVED="`pwd`"
59 | cd "`dirname \"$PRG\"`/" >/dev/null
60 | APP_HOME="`pwd -P`"
61 | cd "$SAVED" >/dev/null
62 |
63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
64 |
65 | # Determine the Java command to use to start the JVM.
66 | if [ -n "$JAVA_HOME" ] ; then
67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
68 | # IBM's JDK on AIX uses strange locations for the executables
69 | JAVACMD="$JAVA_HOME/jre/sh/java"
70 | else
71 | JAVACMD="$JAVA_HOME/bin/java"
72 | fi
73 | if [ ! -x "$JAVACMD" ] ; then
74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
75 |
76 | Please set the JAVA_HOME variable in your environment to match the
77 | location of your Java installation."
78 | fi
79 | else
80 | JAVACMD="java"
81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
82 |
83 | Please set the JAVA_HOME variable in your environment to match the
84 | location of your Java installation."
85 | fi
86 |
87 | # Increase the maximum file descriptors if we can.
88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
89 | MAX_FD_LIMIT=`ulimit -H -n`
90 | if [ $? -eq 0 ] ; then
91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
92 | MAX_FD="$MAX_FD_LIMIT"
93 | fi
94 | ulimit -n $MAX_FD
95 | if [ $? -ne 0 ] ; then
96 | warn "Could not set maximum file descriptor limit: $MAX_FD"
97 | fi
98 | else
99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
100 | fi
101 | fi
102 |
103 | # For Darwin, add options to specify how the application appears in the dock
104 | if $darwin; then
105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
106 | fi
107 |
108 | # For Cygwin, switch paths to Windows format before running java
109 | if $cygwin ; then
110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
112 | JAVACMD=`cygpath --unix "$JAVACMD"`
113 |
114 | # We build the pattern for arguments to be converted via cygpath
115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
116 | SEP=""
117 | for dir in $ROOTDIRSRAW ; do
118 | ROOTDIRS="$ROOTDIRS$SEP$dir"
119 | SEP="|"
120 | done
121 | OURCYGPATTERN="(^($ROOTDIRS))"
122 | # Add a user-defined pattern to the cygpath arguments
123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
125 | fi
126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
127 | i=0
128 | for arg in "$@" ; do
129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
131 |
132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
134 | else
135 | eval `echo args$i`="\"$arg\""
136 | fi
137 | i=$((i+1))
138 | done
139 | case $i in
140 | (0) set -- ;;
141 | (1) set -- "$args0" ;;
142 | (2) set -- "$args0" "$args1" ;;
143 | (3) set -- "$args0" "$args1" "$args2" ;;
144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
150 | esac
151 | fi
152 |
153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
154 | function splitJvmOpts() {
155 | JVM_OPTS=("$@")
156 | }
157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
159 |
160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
161 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12 | set DEFAULT_JVM_OPTS=
13 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windowz variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app'
2 |
--------------------------------------------------------------------------------