Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Android

วิธีถ่ายภาพหน้าจอ (Screenshot) โดยโปรแกรมใน Android

การถ่ายภาพหน้าจอ (Screenshot) โดยเขียนโค้ดใน Android

ตัวอย่างนี้สาธิตวิธีการจับภาพหน้าจอ (View) เป็น Bitmap โดยใช้ Drawing Cache แล้วนำไปแสดงใน ImageView

ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่

เปิด Android Studio เลือก File → New Project และกรอกข้อมูลที่จำเป็น

ขั้นตอนที่ 2: จัดทำ Layout (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="ถ่ายภาพหน้าจอ"/>
    <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: สร้างคลาสช่วยถ่ายภาพ (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 = findViewById(R.id.imageView);
        Button btn = findViewById(R.id.button);
        
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // ถ่ายภาพ Root 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>

การทดสอบ

รันแอปพลิเคชัน กดปุ่ม "ถ่ายภาพหน้าจอ" จะจับภาพหน้าจอปัจจุบันแสดงใน ImageView

วิธีถ่ายภาพหน้าจอ (Screenshot) โดยโปรแกรมใน Android

วิธีถ่ายภาพหน้าจอ (Screenshot) โดยโปรแกรมใน Android

หมายเหตุสำคัญ

  • Drawing Cache ถูก deprecated ใน API 28+ ควรใช้ PixelCopy API หรือ View.draw(Canvas) แทนในโปรเจกต์ใหม่
  • วิธีนี้จับได้เฉพาะ View hierarchy ของแอปตัวเอง ไม่รวม System UI