プログラムでアクティビティ ビューのスクリーンショットを撮る必要があります。私はそれを行う方法の多くの答えを見つけましたが、開いたダイアログでそれを行う方法の答えはありません
4356 次
4 に答える
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 に答える