• Android截图<包括Alertdiaog>


    1.使用的系统内部的截屏方法……

    2.

    public class MainActivity extends AppCompatActivity {
    
        private static final String TAG = "MainActivity";
        View view1;
        int x=1;
        private static final int REQUEST_MEDIA_PROJECTION = 1;
        private MediaProjectionManager projectionManager;
        boolean screenCapture = false;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        }
    
        public void click(View view) {
    
             AlertDialog.Builder localBuilder = new AlertDialog.Builder(this);
            localBuilder.setTitle("自定义列表对话框").setIcon(R.mipmap.ic_launcher);
             view1=getLayoutInflater().inflate(R.layout.layout, null);
            localBuilder.setView(getLayoutInflater().inflate(R.layout.layout, null));
            localBuilder.setPositiveButton("确定", new DialogInterface.OnClickListener()
            {
                public void onClick(DialogInterface paramAnonymousDialogInterface, int paramAnonymousInt)
                {
                    /**
                     *
                     * 操作
                     * */
                    // 获取屏幕
                    screenCapture = true;
                    x=1;
                    takeScreenshot2();
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
    
    
                }
            }).setNegativeButton("取消", new DialogInterface.OnClickListener()
            {
                public void onClick(DialogInterface paramAnonymousDialogInterface, int paramAnonymousInt)
                {
                    /**
                     *
                     * 操作
                     * */
    
                }
            }).create().show();
    
        }
    
        public void takeScreenshot2() {
            try {
                projectionManager = (MediaProjectionManager) getSystemService(Context.MEDIA_PROJECTION_SERVICE);
                startActivityForResult(
                        projectionManager.createScreenCaptureIntent(),
                        REQUEST_MEDIA_PROJECTION);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
    
        @Override
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
             if (requestCode == REQUEST_MEDIA_PROJECTION && screenCapture==true) {
                /**
                 *
                 */
    
    
                try {
                    final int mWidth = getWindowManager().getDefaultDisplay().getWidth();
                    final int mHeight = getWindowManager().getDefaultDisplay().getHeight();
                    final ImageReader mImageReader = ImageReader.newInstance(mWidth, mHeight, PixelFormat.RGBA_8888, 2);
                    DisplayMetrics metrics = new DisplayMetrics();
                    getWindowManager().getDefaultDisplay().getMetrics(metrics);
                    int mScreenDensity = metrics.densityDpi;
                    final MediaProjection mProjection = projectionManager.getMediaProjection(-1, data);
                    Log.i(TAG, "onActivityResult2: "+screenCapture+"**"+x);
                    final VirtualDisplay virtualDisplay = mProjection.createVirtualDisplay("screen-mirror",
                            mWidth, mHeight, mScreenDensity, DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR,
                            mImageReader.getSurface(), null, null);
                    mImageReader.setOnImageAvailableListener(new ImageReader.OnImageAvailableListener() {
                        @Override
                        public void onImageAvailable(ImageReader reader) {
                            if(x!=1){
                                return;
                            }
                            else{
                                try {
                                    Log.i(TAG, "onActivityResult3: "+screenCapture+"**"+x);
                                    /**
                                     * 去掉--->mProjection.stop(); 会出现不停的截图现象
                                     */
                                    x=2;
                                    mProjection.stop();
                                    Image image = mImageReader.acquireLatestImage();
                                    final Image.Plane[] planes = image.getPlanes();
                                    final ByteBuffer buffer = planes[0].getBuffer();
                                    int offset = 0;
                                    int pixelStride = planes[0].getPixelStride();
                                    int rowStride = planes[0].getRowStride();
                                    int rowPadding = rowStride - pixelStride * mWidth;
                                    Bitmap bitmap = Bitmap.createBitmap(mWidth + rowPadding / pixelStride, mHeight, Bitmap.Config.ARGB_8888);
                                    bitmap.copyPixelsFromBuffer(buffer);
                                    image.close();
                                    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy_MM_dd_hh_mm_ss");
                                    String strDate = dateFormat.format(new java.util.Date());
                                    /**
                                     * 截图的保存路径……
                                     */
                                    String pathImage = Environment.getExternalStorageDirectory().getPath() + "/";
                                    /**
                                     * 截图名称……
                                     */
                                    String nameImage = pathImage + strDate + ".png";
                                    if (bitmap != null) {
                                        try {
                                            File fileImage = new File(nameImage);
                                            if (!fileImage.exists()) {
                                                fileImage.createNewFile();
                                            }
                                            FileOutputStream out = new FileOutputStream(fileImage);
                                            if (out != null) {
                                                bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
                                                out.flush();
                                                out.close();
                                                Toast.makeText(MainActivity.this, "图片保存成功!", Toast.LENGTH_SHORT).show();
                                                Intent media = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
                                                Uri contentUri = Uri.fromFile(fileImage);
                                                media.setData(contentUri);
                                                getApplicationContext().sendBroadcast(media);
                                                screenCapture = false;
                                            }
                                        } catch (FileNotFoundException e) {
                                            e.printStackTrace();
                                        } catch (IOException e) {
                                            e.printStackTrace();
                                        }
                                    } else {
                                        Toast.makeText(MainActivity.this, "cannot get phone's screen", Toast.LENGTH_SHORT).show();
    
                                    }
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }
    
                        }
                    }, null);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                /**
                 *
                 */
            }
    
    
        }
    
    
    
    
    
    
    }
    今天多一点积累,明天少一分烦恼
  • 相关阅读:
    MySQL源代码解读(二)
    MySQL源代码解读(一)
    C语言中如何对串口进行操作
    mysql 运行概图
    Yacc 与 Lex
    外键建索引
    提高SQL查询效率
    MySQL源代码分析:(1)Main函数
    Linux "could not open default font 'fixed'."
    转帖 浅析ARM汇编语言子例程设计方法
  • 原文地址:https://www.cnblogs.com/galibujianbusana/p/6391470.html
Copyright © 2020-2023  润新知