Android
 Computer >> コンピューター >  >> プログラミング >> Android

プログラムでAndroidでスクリーンショットを撮る方法は?


この例は、Androidでプログラムでスクリーンショットを撮る方法を示しています。

ステップ1 − Android Studioで新しいプロジェクトを作成し、[ファイル]⇒[新しいプロジェクト]に移動して、新しいプロジェクトを作成するために必要なすべての詳細を入力します。

ステップ2 −次のコードをres / layout/activity_main.xmlに追加します。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
   style="?attr/actionButtonStyle"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:id="@+id/main">
   <Button
      android:id="@+id/button"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_centerHorizontal="true"
      android:layout_marginTop="30dp"
      android:text="Take screenshot"/>
   <ImageView
      android:id="@+id/imageView"
      android:layout_below="@id/button"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:layout_centerHorizontal="true"
      android:layout_marginTop="30dp"
      android:scaleType="fitCenter"/>
</RelativeLayout>

ステップ3 −新しいJavaクラスを作成し、screenshot.javaに次のコードを追加します

import android.graphics.Bitmap;
import android.view.View;
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 takescreenshotOfRootView(View v) {
      return takescreenshot(v.getRootView());
   }
}

ステップ4 −次のコードをsrc / MainActivity.java

に追加します
import android.graphics.Bitmap;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
   private View main;
   private ImageView imageView;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      main = findViewById(R.id.main);
      imageView = (ImageView) findViewById(R.id.imageView);
      Button btn = (Button) findViewById(R.id.button);
      btn.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View view) {
            Bitmap b = Screenshot.takescreenshotOfRootView(imageView);
            imageView.setImageBitmap(b);
            main.setBackgroundColor(Color.parseColor("#999999"));
         }
      });
   }
}

ステップ5 −次のコードをandroidManifest.xmlに追加します

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android" package="app.com.sample">
   <application
      android:allowBackup="true"
      android:icon="@mipmap/ic_launcher"
      android:label="@string/app_name"
      android:roundIcon="@mipmap/ic_launcher_round"
      android:supportsRtl="true"
      android:theme="@style/AppTheme">
      <activity android:name=".MainActivity">
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
      </activity>
   </application>
</manifest>

アプリケーションを実行してみましょう。実際のAndroidモバイルデバイスをコンピューターに接続していると思います。 android studioからアプリを実行するには、プロジェクトのアクティビティファイルの1つを開き、ツールバーの[実行]アイコンをクリックします。オプションとしてモバイルデバイスを選択し、デフォルトの画面を表示するモバイルデバイスを確認します-

プログラムでAndroidでスクリーンショットを撮る方法は?

プログラムでAndroidでスクリーンショットを撮る方法は?


  1. 方法:Macでスクリーンショットを撮る

    Apple Macは、平均的なコンピュータユーザーが自分のコンピュータを使用して実行したいと思う可能性のあるすべてのことを実行します。これには、スクリーンショットのキャプチャも含まれます。 ただし、ほとんどのMacユーザーは、Macでスクリーンショットをキャプチャする方法を正確に知らず、たとえそうだとしても、Macが多種多様な種類のスクリーンショットをキャプチャできるという事実に気づいていません。 以下は、Macでさまざまな種類のスクリーンショットをキャプチャする手順です。 画面全体のスクリーンショットを撮る方法: Macの画面に希望どおりの表示が表示されていることを確認してから、コ

  2. Android でリカバリ モードのスクリーンショットを撮る方法

    スクリーンショットを撮ることは、グループと真の情報を共有するための最良の方法の 1 つです。スクリーンショットは、有効な証拠と同じように機能します。 Android でスクリーンショットを撮るのは、音量を下げるボタンと電源ボタンを同時に押すのと同じくらい簡単であることが知られています。ただし、起動時に Android のスクリーンショットを撮りたい場合、前述のキーの組み合わせでは同じものを撮れない場合があります。さらに、Android には 3 つのカスタム リカバリが付属していますが、いずれも起動時のスクリーンショットを撮ることができません。これは、Android のリカバリ モードにより、