点击android中的活动截图

时间:2015-03-02 10:06:43

标签: android

我想截取一个活动的屏幕截图并在按钮点击的下一个活动中显示它。 我正在使用以下代码,但它不起作用。
如果有人知道答案,请告诉我:

choose.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    mainlayout.setDrawingCacheEnabled(true);
                    mainlayout.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
                            View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
                    mainlayout.layout(0, 0, mainlayout.getMeasuredWidth(), mainlayout.getMeasuredHeight());
                    mainlayout.buildDrawingCache(true);
                    Bitmap b = Bitmap.createBitmap(mainlayout.getDrawingCache());
                    mainlayout.setDrawingCacheEnabled(false); // clear drawing cache
                    Intent i4=new Intent(Screen3.this,Screen4.class);
                    i4.putExtra("BitmapImage", b);
                    startActivity(i4);
                }//onclick
            });

1 个答案:

答案 0 :(得分:0)

public static Bitmap captureScreen(View v){

        Bitmap bitmap = null;

        try {
            if(v!=null)
            {
                 int width = v.getWidth();
                 int height = v.getHeight();

                 Bitmap screenshot = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
                 v.draw(new Canvas(screenshot));
                }
             } 
        catch (Exception e){

             Log.d("captureScreen", "Failed");
        }

     return bitmap;
     }

v是正在捕捉的内容(任何布局)