12

我需要以编程方式截取活动视图的屏幕截图。我找到了很多答案如何做到这一点,但没有回答如何通过打开的对话框来做到这一点

4

4 回答 4

8

好的,这有点棘手。据我所知,没有直接的方法可以做到这一点。这是可行的。我尝试拍摄单独的照片并将它们分层。

Dialog dialog = // Dialog that is showing

View mainView = getSupportActivity().getWindow().getDecorView();
mainView = getSupportActivity().getWindow().getDecorView()
        .findViewById(android.R.id.content);
mainView.setDrawingCacheEnabled(true);
// This is the bitmap for the main activity
Bitmap bitmap = mainView.getDrawingCache();

View dialogView = dialog.getView();
int location[] = new int[2];
mainView.getLocationOnScreen(location);
int location2[] = new int[2];
dialogView.getLocationOnScreen(location2);

dialogView.setDrawingCacheEnabled(true);
// This is the bitmap for the dialog view
Bitmap bitmap2 = dialogView.getDrawingCache();

Canvas canvas = new Canvas(bitmap);
// Need to draw the dialogView into the right position
canvas.drawBitmap(bitmap2, location2[0] - location[0], location2[1] - location[1],
        new Paint());

String filename = // filename to save
File myPath = new File(filename);
FileOutputStream fos = null;
try {
    fos = new FileOutputStream(myPath);
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
    fos.flush();
    fos.close();
    Trace.d("Twitter", "The file path is " + myPath);
} catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

我刚试过这个,它奏效了。希望这可以帮助。让我知道它是否不起作用。

于 2014-01-30T22:07:25.443 回答
1

我有以编程方式捕获屏幕的代码..

希望这段代码对你有帮助..

public class CaptureScreenShots extends Activity {
LinearLayout L1;
ImageView image;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.screen_shots);
     L1 = (LinearLayout) findViewById(R.id.LinearLayout01);
        Button but = (Button) findViewById(R.id.munchscreen);
        but.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                View v1 = L1.getRootView();
                v1.setDrawingCacheEnabled(true);
                Bitmap bm = v1.getDrawingCache();
                BitmapDrawable bitmapDrawable = new BitmapDrawable(bm);
                image = (ImageView) findViewById(R.id.screenshots);
                image.setBackgroundDrawable(bitmapDrawable);
            }
        });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.screen_shots, menu);
    return true;
}

}

下面是布局文件

    <?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/LinearLayout01"
    >
<Button
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/munch"
    android:id="@+id/munchscreen"
    />
  <ImageView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/screenshots"
    android:contentDescription="@string/app_name"
    />

</LinearLayout>
于 2014-02-06T06:13:43.530 回答
0

在连接了设备/模拟器的情况下,使用 DDMS 的“设备”窗口中的“屏幕捕获”按钮。这使您可以截取屏幕上的任何内容,包括打开对话框时的屏幕截图。

在此处输入图像描述

编辑:当我回答这个问题时,不清楚是否需要以编程方式完成,后来被编辑澄清。

于 2013-06-30T18:49:37.010 回答
0

这对我有用(但没有对话框):

public class MainActivity extends AppCompatActivity {

private View main;
private ImageView imageView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //Here we Checking CALL_PHONE permission

    setContentView(R.layout.activity_main);
    main = findViewById(R.id.main);
    imageView = (ImageView) findViewById(R.id.imageView);
    Button btn = (Button) findViewById(R.id.btn);
    btn.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View view) {

            Bitmap b = Screenshot.takescreenshotOfRoot(imageView);
            imageView.setImageBitmap(b);
            main.setBackgroundColor(Color.parseColor("#999999"));
        }
    });

}

}

截图类:

public class Screenshot {

public static Bitmap takescreenshot (View v) {
    v.setDrawingCacheEnabled(true);
    v.buildDrawingCache(true);
    Bitmap b = Bitmap.createBitmap(v.getDrawingCache());
    v.setDrawingCacheEnabled(false);
    return b;
}

public static Bitmap takescreenshotOfRoot(View v) {
    return takescreenshot(v.getRootView());

}

}

XML:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout    xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.balance.MainActivity">

<Button
    android:id="@+id/btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:text="Take Screenshot"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.055" />

<ImageView
    android:id="@+id/imageView"
    android:layout_width="355dp"
    android:layout_height="485dp"
    android:layout_marginTop="8dp"
    app:layout_constraintTop_toTopOf="parent"
    app:srcCompat="@android:drawable/menuitem_background"
    tools:layout_editor_absoluteX="17dp" />


  </android.support.constraint.ConstraintLayout>    
于 2017-12-29T00:09:13.547 回答