我需要以编程方式截取活动视图的截图。 我找到了很多答案,怎么做, 但没有回答如何用打开的对话框
来做到这一点答案 0 :(得分:7)
好的,这个有点棘手。据我所知,没有直接的方法可以做到这一点。这是有效的。我试图拍摄单个照片并将它们分层。
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();
}
我刚试过这个并且有效。 希望这可以帮助。如果它不起作用,请告诉我。
答案 1 :(得分:5)
试试这个图书馆:https://github.com/jraska/Falcon。 它包含所有活动窗口的截图,包括对话框,可以找出问题所在。
答案 2 :(得分:1)
使用连接了设备/仿真器的DDMS“设备”窗口中的“屏幕捕获”按钮。这允许您截取屏幕上任何内容的屏幕截图,包括对话框打开时。
编辑:当我回答这个问题时,不清楚是否需要以编程方式完成,后来进行了编辑以澄清。
答案 3 :(得分: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>
答案 4 :(得分:0)
这对我有用(但没有对话):
a_dict = temporary_job_instance.__dict__
del a_dict['_state']
del a_dict['id']
Job.objects.create(**a_dict)
}
截图类:
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"));
}
});
}
}
XML:
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());
}