繁体   English   中英

如何在Android中以编程方式捕获手机屏幕

[英]how to capture the phone screen programmatically in android

我想知道如何以编程方式捕获电话屏幕。 我只是谷歌搜索,知道我必须使用view.getDrawingCache()。 但是getDrawingCache()始终返回null。

我的xml布局是

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/screenlayout">
<ImageView  
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:id="@+id/screen"
/>
</LinearLayout>

Java文件::

 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    View vScreen = findViewById(R.id.screenlayout);
    ImageView imageview = (ImageView)findViewById(R.id.screen);

    Bitmap bitmap;
    View v1 = vScreen.getRootView();
    v1.setDrawingCacheEnabled(true);
    bitmap = Bitmap.createBitmap(v1.getDrawingCache());   //getting null here     
    v1.setDrawingCacheEnabled(false)      
    imageview.setImageBitmap(bitmap);

}

有人可以纠正我哪里出问题了吗?

使用以下代码来避免空位图:


public class AndroidWebImage extends Activity {

ImageView bmImage; 
LinearLayout view;
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);

      view = (LinearLayout)findViewById(R.id.screen);
      bmImage = (ImageView)findViewById(R.id.image);

      view.setDrawingCacheEnabled(true);
      // this is the important code :)  
      // Without it the view will have a dimension of 0,0 and the bitmap will be null          

      view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), 
            MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));

      view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight()); 

      view.buildDrawingCache(true);
      Bitmap b = Bitmap.createBitmap(view.getDrawingCache());
      view.setDrawingCacheEnabled(false); // clear drawing cache

      bmImage.setImageBitmap(b);   

};


}

好好看看,您必须使用:

 view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), 
                MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));

 view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight()); 

我使用了以下main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
   android:id="@+id/screen"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  >
<TextView
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="@string/hello"
  />
<ImageView
  android:id="@+id/image"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
/>
</LinearLayout>

结果是:

在此处输入图片说明 参考

所有允许截图的程序仅能在有根手机上运行吗? 如果您的手机是扎根手机,则可以尝试使用此功能:

public class ScreenCapture extends Activity{

        LinearLayout view;

        @Override

        protected void onCreate(Bundle savedInstanceState) {

                // TODO Auto-generated method stub

                super.onCreate(savedInstanceState);

                setContentView(R.layout.main);



                view = (LinearLayout)findViewById(R.id.screen);

                Button myBtn = (Button)findViewById(R.id.myBtn);



                myBtn.setOnClickListener(new View.OnClickListener(){

                        @Override

                        public void onClick(View v) {

                                // TODO Auto-generated method stub

                                View v1 = view.getRootView();

                                System.out.println("Root View : "+v1);

                                v1.setDrawingCacheEnabled(true);

                                Bitmap bm = v1.getDrawingCache();



                                System.out.println("Bitmap : "+bm);

                                showScreen(bm);

                        }

                });



        }

}

您还可以看到此库: Android屏幕截图库 (ASL)使您可以以编程方式从Android设备捕获屏幕截图,而无需具有root访问权限

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM