安卓实现截屏以及录屏功能Demo 安卓实现截屏以及录屏功能Demo
package com.dzjin.screen.screenshotandrecorddemo;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.media.projection.MediaProjectionManager;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
public class MainActivity extends AppCompatActivity {
private LinearLayout linearLayout=null;
private Button buttonRecord=null;
private Button buttonCapture=null;
private boolean isRecord=false;
private int mScreenWidth;
private int mScreenHeight;
private int mScreenDensity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getScreenBaseInfo();
linearLayout=(LinearLayout)findViewById(R.id.linearLayout);
buttonRecord=(Button)findViewById(R.id.buttonRecord);
buttonCapture=(Button)findViewById(R.id.buttonCapture);
buttonRecord.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(isRecord){
stopScreenRecord();
}else{
startScreenRecord();
}
}
});
buttonCapture.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
capture(linearLayout);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 1000){
if(resultCode == RESULT_OK){
//获得录屏权限,启动Service进行录制
Intent intent=new Intent(MainActivity.this,ScreenRecordService.class);
intent.putExtra("resultCode",resultCode);
intent.putExtra("resultData",data);
intent.putExtra("mScreenWidth",mScreenWidth);
intent.putExtra("mScreenHeight",mScreenHeight);
intent.putExtra("mScreenDensity",mScreenDensity);
startService(intent);
Toast.makeText(this,"录屏开始",Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(this,"录屏失败",Toast.LENGTH_SHORT).show();
}
}
}
//start screen record
private void startScreenRecord(){
//Manages the retrieval of certain types of MediaProjection tokens.
MediaProjectionManager mediaProjectionManager=
(MediaProjectionManager)getSystemService(Context.MEDIA_PROJECTION_SERVICE);
//Returns an Intent that must passed to startActivityForResult() in order to start screen capture.
Intent permissionIntent=mediaProjectionManager.createScreenCaptureIntent();
startActivityForResult(permissionIntent,1000);
isRecord=true;
buttonRecord.setText(new String("停止录屏"));
}
//stop screen record.
private void stopScreenRecord(){
Intent service = new Intent(this, ScreenRecordService.class);
stopService(service);
isRecord=false;
buttonRecord.setText(new String("开始录屏"));
Toast.makeText(this,"录屏成功",Toast.LENGTH_SHORT).show();
}
public void capture(View v){
//格式化时间作为截屏文件名
SimpleDateFormat simpleDateFormat=new SimpleDateFormat("YY-MM-DD-HH-MM-SS");
//在获取外部存储的时候,一定注意添加权限,如果添加权限还不能成功,则手动在应用中开启权限。
String filePathName= Environment.getExternalStorageDirectory() "/" simpleDateFormat.format(new Date()) ".png";
//Find the topmost view in the current view hierarcht.
View view=v.getRootView();
// Enable or disable drawing cache.
view.setDrawingCacheEnabled(true);
// Calling this method is equivalent to calling buildDrawingCache(false);
// In order to force drawing cache to be buuild.
view.buildDrawingCache();
//Calling this method is equivalent to calling getDrawingCache(false);
//Return the Bitmap in which this view drawing is cached.
Bitmap bitmap=view.getDrawingCache();
try{
System.out.println(filePathName);
FileOutputStream fileOutputStream=new FileOutputStream(filePathName);
//Write a compressed version of bitmap to specified outputStream.
bitmap.compress(Bitmap.CompressFormat.PNG,100,fileOutputStream);
Toast.makeText(this,"Cpature Succeed",Toast.LENGTH_SHORT).show();
}catch (Exception e){
e.printStackTrace();
Toast.makeText(this,"Cpature Failed",Toast.LENGTH_SHORT).show();
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// 在这里将BACK键模拟了HOME键的返回桌面功能(并无必要)
if (keyCode == KeyEvent.KEYCODE_BACK) {
simulateHome();
return true;
}
return super.onKeyDown(keyCode, event);
}
/**
* 获取屏幕基本信息
*/
private void getScreenBaseInfo() {
//A structure describing general information about a display, such as its size, density, and font scaling.
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
mScreenWidth = metrics.widthPixels;
mScreenHeight = metrics.heightPixels;
mScreenDensity = metrics.densityDpi;
}
/**
* 模拟HOME键返回桌面的功能
*/
private void simulateHome() {
//Intent.ACTION_MAIN,Activity Action: Start as a main entry point, does not expect to receive data.
Intent intent = new Intent(Intent.ACTION_MAIN);
//If set, this activity will become the start of a new task on this history stack.
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//This is the home activity, that is the first activity that is displayed when the device boots.
intent.addCategory(Intent.CATEGORY_HOME);
this.startActivity(intent);
}
}
资源文件列表
ScreenShotAndRecordDemo/.gitignore , 127
ScreenShotAndRecordDemo/.gradle/4.1/fileChanges/last-build.bin , 1
ScreenShotAndRecordDemo/.gradle/4.1/fileContent/fileContent.lock , 17
ScreenShotAndRecordDemo/.gradle/4.1/fileHashes/fileHashes.bin , 242319
ScreenShotAndRecordDemo/.gradle/4.1/fileHashes/fileHashes.lock , 17
ScreenShotAndRecordDemo/.gradle/4.1/fileHashes/resourceHashesCache.bin , 19925
ScreenShotAndRecordDemo/.gradle/4.1/javaCompile/classAnalysis.bin , 785264
ScreenShotAndRecordDemo/.gradle/4.1/javaCompile/jarAnalysis.bin , 321993
ScreenShotAndRecordDemo/.gradle/4.1/javaCompile/javaCompile.lock , 17
ScreenShotAndRecordDemo/.gradle/4.1/javaCompile/taskHistory.bin , 428677
ScreenShotAndRecordDemo/.gradle/4.1/javaCompile/taskJars.bin , 20814
ScreenShotAndRecordDemo/.gradle/4.1/taskHistory/fileSnapshots.bin , 2165341
ScreenShotAndRecordDemo/.gradle/4.1/taskHistory/taskHistory.bin , 68547
ScreenShotAndRecordDemo/.gradle/4.1/taskHistory/taskHistory.lock , 17
ScreenShotAndRecordDemo/.gradle/buildOutputCleanup/built.bin , 0
ScreenShotAndRecordDemo/.gradle/buildOutputCleanup/cache.properties , 51
ScreenShotAndRecordDemo/.gradle/buildOutputCleanup/cache.properties.lock , 2
ScreenShotAndRecordDemo/.idea/gradle.xml , 626
ScreenShotAndRecordDemo/.idea/libraries/android_arch_core_common_1_0_0_jar.xml , 533
ScreenShotAndRecordDemo/.idea/libraries/android_arch_lifecycle_common_1_0_0_jar.xml , 548
ScreenShotAndRecordDemo/.idea/libraries/android_arch_lifecycle_runtime_1_0_0.xml , 658
ScreenShotAndRecordDemo/.idea/libraries/com_android_support_animated_vector_drawable_26_1_0.xml , 742
ScreenShotAndRecordDemo/.idea/libraries/com_android_support_appcompat_v7_26_1_0.xml , 682
ScreenShotAndRecordDemo/.idea/libraries/com_android_support_constraint_constraint_layout_1_0_2.xml , 501
ScreenShotAndRecordDemo/.idea/libraries/com_android_support_constraint_constraint_layout_solver_1_0_2_jar.xml , 372
ScreenShotAndRecordDemo/.idea/libraries/com_android_support_support_annotations_26_1_0_jar.xml , 608
ScreenShotAndRecordDemo/.idea/libraries/com_android_support_support_compat_26_1_0.xml , 692
ScreenShotAndRecordDemo/.idea/libraries/com_android_support_support_core_ui_26_1_0.xml , 697
ScreenShotAndRecordDemo/.idea/libraries/com_android_support_support_core_utils_26_1_0.xml , 712
ScreenShotAndRecordDemo/.idea/libraries/com_android_support_support_fragment_26_1_0.xml , 702
ScreenShotAndRecordDemo/.idea/libraries/com_android_support_support_media_compat_26_1_0.xml , 722
ScreenShotAndRecordDemo/.idea/libraries/com_android_support_support_v4_26_1_0.xml , 672
ScreenShotAndRecordDemo/.idea/libraries/com_android_support_support_vector_drawable_26_1_0.xml , 737
ScreenShotAndRecordDemo/.idea/libraries/com_android_support_test_espresso_espresso_core_3_0_1.xml , 710
ScreenShotAndRecordDemo/.idea/libraries/com_android_support_test_espresso_espresso_idling_resource_3_0_1.xml , 765
ScreenShotAndRecordDemo/.idea/libraries/com_android_support_test_rules_1_0_1.xml , 652
ScreenShotAndRecordDemo/.idea/libraries/com_android_support_test_runner_1_0_1.xml , 657
ScreenShotAndRecordDemo/.idea/libraries/com_google_code_findbugs_jsr305_2_0_1_jar.xml , 359
ScreenShotAndRecordDemo/.idea/libraries/com_squareup_javawriter_2_1_1_jar.xml , 538
ScreenShotAndRecordDemo/.idea/libraries/javax_inject_javax_inject_1_jar.xml , 528
ScreenShotAndRecordDemo/.idea/libraries/junit_junit_4_12_jar.xml , 487
ScreenShotAndRecordDemo/.idea/libraries/net_sf_kxml_kxml2_2_3_0_jar.xml , 285
ScreenShotAndRecordDemo/.idea/libraries/org_hamcrest_hamcrest_core_1_3_jar.xml , 543
ScreenShotAndRecordDemo/.idea/libraries/org_hamcrest_hamcrest_integration_1_3_jar.xml , 578
ScreenShotAndRecordDemo/.idea/libraries/org_hamcrest_hamcrest_library_1_3_jar.xml , 557
ScreenShotAndRecordDemo/.idea/misc.xml , 1611
ScreenShotAndRecordDemo/.idea/modules.xml , 383
ScreenShotAndRecordDemo/.idea/runConfigurations.xml , 564
ScreenShotAndRecordDemo/.idea/workspace.xml , 159954
ScreenShotAndRecordDemo/ScreenShotAndRecordDemo.iml , 877
ScreenShotAndRecordDemo/app/.gitignore , 8
ScreenShotAndRecordDemo/app/app.iml , 11587
ScreenShotAndRecordDemo/app/build/generated/mockable-android-26.v3.jar , 2811099
ScreenShotAndRecordDemo/app/build/generated/source/buildConfig/androidTest/debug/com/dzjin/screen/screenshotandrecorddemo/test/BuildConfig.java , 497
ScreenShotAndRecordDemo/app/build/generated/source/buildConfig/debug/com/dzjin/screen/screenshotandrecorddemo/BuildConfig.java , 487
ScreenShotAndRecordDemo/app/build/generated/source/r/androidTest/debug/android/support/test/espresso/R.java , 259
ScreenShotAndRecordDemo/app/build/generated/source/r/androidTest/debug/android/support/test/espresso/idling/concurrent/R.java , 277
ScreenShotAndRecordDemo/app/build/generated/source/r/androidTest/debug/android/support/test/rules/R.java , 256
ScreenShotAndRecordDemo/app/build/generated/source/r/androidTest/debug/android/support/test/runner/R.java , 257
ScreenShotAndRecordDemo/app/build/generated/source/r/androidTest/debug/com/dzjin/screen/screenshotandrecorddemo/test/R.java , 586041
ScreenShotAndRecordDemo/app/build/generated/source/r/debug/android/arch/lifecycle/R.java , 252
ScreenShotAndRecordDemo/app/build/generated/source/r/debug/android/support/compat/R.java , 7739
ScreenShotAndRecordDemo/app/build/generated/source/r/debug/android/support/constraint/R.java , 15438
ScreenShotAndRecordDemo/app/build/generated/source/r/debug/android/support/coreui/R.java , 7739
ScreenShotAndRecordDemo/app/build/generated/source/r/debug/android/support/coreutils/R.java , 7742
ScreenShotAndRecordDemo/app/build/generated/source/r/debug/android/support/fragment/R.java , 7741
ScreenShotAndRecordDemo/app/build/generated/source/r/debug/android/support/graphics/drawable/R.java , 7750
ScreenShotAndRecordDemo/app/build/generated/source/r/debug/android/support/graphics/drawable/animated/R.java , 7759
ScreenShotAndRecordDemo/app/build/generated/source/r/debug/android/support/mediacompat/R.java , 9594
ScreenShotAndRecordDemo/app/build/generated/source/r/debug/android/support/v4/R.java , 9585
ScreenShotAndRecordDemo/app/build/generated/source/r/debug/android/support/v7/appcompat/R.java , 116815
ScreenShotAndRecordDemo/app/build/generated/source/r/debug/com/dzjin/screen/screenshotandrecorddemo/R.java , 581402
ScreenShotAndRecordDemo/app/build/intermediates/blame/res/androidTest/debug/multi-v2/debug.json , 149199
ScreenShotAndRecordDemo/app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-af.json , 1112
ScreenShotAndRecordDemo/app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-am.json , 1109
ScreenShotAndRecordDemo/app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-ar.json , 1113
ScreenShotAndRecordDemo/app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-az.json , 1093
ScreenShotAndRecordDemo/app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-b+sr+Latn.json , 1142
ScreenShotAndRecordDemo/app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-be.json , 1115
ScreenShotAndRecordDemo/app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-bg.json , 1115
ScreenShotAndRecordDemo/app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-bn.json , 1117
ScreenShotAndRecordDemo/app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-bs.json , 1115
ScreenShotAndRecordDemo/app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-ca.json , 1115
ScreenShotAndRecordDemo/app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-cs.json , 1113
ScreenShotAndRecordDemo/app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-da.json , 1111
ScreenShotAndRecordDemo/app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-de.json , 1113
ScreenShotAndRecordDemo/app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-el.json , 1116
ScreenShotAndRecordDemo/app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-en-rAU.json , 1127
ScreenShotAndRecordDemo/app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-en-rGB.json , 1127
ScreenShotAndRecordDemo/app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-en-rIN.json , 1127
ScreenShotAndRecordDemo/app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-es-rUS.json , 1131
ScreenShotAndRecordDemo/app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-es.json , 1114
ScreenShotAndRecordDemo/app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-et.json , 1116
ScreenShotAndRecordDemo/app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-eu.json , 1114
ScreenShotAndRecordDemo/app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-fa.json , 1114
ScreenShotAndRecordDemo/app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-fi.json , 1111
ScreenShotAndRecordDemo/app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-fr-rCA.json , 1132
ScreenShotAndRecordDemo/app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-fr.json , 1116
ScreenShotAndRecordDemo/app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-gl.json , 1115
ScreenShotAndRecordDemo/app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-gu.json , 1113
ScreenShotAndRecordDemo/app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-h720dp-v13.json , 795
ScreenShotAndRecordDemo/app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-hdpi-v4.json , 824
ScreenShotAndRecordDemo/app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-hi.json , 1114
ScreenShotAndRecordDemo/app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-hr.json , 1113
ScreenShotAndRecordDemo/app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-hu.json , 1116
ScreenShotAndRecordDemo/app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-hy.json , 1112
ScreenShotAndRecordDemo/app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-in.json , 1114
ScreenShotAndRecordDemo/app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-is.json , 1110
ScreenShotAndRecordDemo/app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-it.json , 1112
ScreenShotAndRecordDemo/app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-iw.json , 1110
ScreenShotAndRecordDemo/app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-ja.json , 1107
ScreenShotAndRecordDemo/app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-ka.json , 1114
ScreenShotAndRecordDemo/app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-kk.json , 1113
ScreenShotAndRecordDemo/app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-km.json , 1112
ScreenShotAndRecordDemo/app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-kn.json , 1115
ScreenShotAndRecordDemo/app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-ko.json , 1105
ScreenShotAndRecordDemo/app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-ky.json , 1113
ScreenShotAndRecordDemo/app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-land.json , 816
ScreenShotAndRecordDemo/app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-large-v4.json , 910
ScreenShotAndRecordDemo/app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-ldltr-v21.json , 792
ScreenShotAndRecordDemo/app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-lo.json , 1111
ScreenShotAndRecordDemo/app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-lt.json , 1116
ScreenShotAndRecordDemo/app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-lv.json , 1115
ScreenShotAndRecordDemo/app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-mk.json , 1076
ScreenShotAndRecordDemo/app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-ml.json , 1115
ScreenShotAndRecordDemo/app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-mn.json , 1112
ScreenShotAndRecordDemo/app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-mr.json , 1115
ScreenShotAndRecordDemo/app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-ms.json , 1114
ScreenShotAndRecordDemo/app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-my.json , 1116
ScreenShotAndRecordDemo/app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-nb.json , 1109
ScreenShotAndRecordDemo/app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-ne.json , 1117
ScreenShotAndRecordDemo/app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-night-v8.json , 879
ScreenShotAndRecordDemo/app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-nl.json , 1116
ScreenShotAndRecordDemo/app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-pa.json , 1111
ScreenShotAndRecordDemo/app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-pl.json , 1114
ScreenShotAndRecordDemo/app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-port.json , 771
ScreenShotAndRecordDemo/app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-pt-rBR.json , 1132
ScreenShotAndRecordDemo/app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-pt-rPT.json , 1132
ScreenShotAndRecordDemo/app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-pt.json , 1116
ScreenShotAndRecordDemo/app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-ro.json , 1116
ScreenShotAndRecordDemo/app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-ru.json , 1115
ScreenShotAndRecordDemo/app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-si.json , 1114
ScreenShotAndRecordDemo/app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-sk.json , 1112
ScreenShotAndRecordDemo/app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-sl.json , 1114
ScreenShotAndRecordDemo/app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-sq.json , 1114
ScreenShotAndRecordDemo/app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-sr.json , 1114
ScreenShotAndRecordDemo/app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-sv.json , 1113
ScreenShotAndRecordDemo/app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-sw.json , 1112
ScreenShotAndRecordDemo/app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-sw600dp-v13.json , 904
ScreenShotAndRecordDemo/app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-ta.json , 1115
ScreenShotAndRecordDemo/app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-te.json , 1116
最多只能显示150条信息!
