1

プログラムで電話画面をキャプチャする方法を知りたいです。私はちょうどグーグルで、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>

ジャバファイル::

 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);

}

誰かが私が間違っているところを修正してもらえますか?

4

2 に答える 2

1

nullビットマップを回避するには、次のコードを使用します。


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>

結果は次のとおりです。

ここに画像の説明を入力 参照

于 2012-05-19T05:05:43.423 に答える
0

スクリーンショットを許可するすべてのプログラムは、ルート化された電話でのみ機能しますか? お使いの携帯電話がルート化されている場合は、これを使用して試すことができます:

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 Screenshot Library (ASL) を使用すると、root アクセス権限がなくても Android デバイスからスクリーンショットをプログラムでキャプチャできます。

于 2012-05-18T16:59:06.950 に答える