Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> Android

Android में प्रोग्रामेटिकली स्क्रीनशॉट कैसे लें?

यह उदाहरण दर्शाता है कि Android ऐप में व्यू का स्क्रीनशॉट प्रोग्रामेटिकली कैसे लिया जाए और उसे ImageView में दिखाया जाए।

चरण 1: नया प्रोजेक्ट बनाएं

Android Studio में नया प्रोजेक्ट बनाएं।

चरण 2: लेआउट XML

res/layout/activity_main.xml - बटन और ImageView के साथ:

<?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: Screenshot हेल्पर क्लास

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: MainActivity.java

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) {
                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>

ऐप चलाएं

"Take screenshot" बटन दबाएं - वर्तमान स्क्रीन का स्क्रीनशॉट ImageView में दिखेगा।

Android में प्रोग्रामेटिकली स्क्रीनशॉट कैसे लें?

Android में प्रोग्रामेटिकली स्क्रीनशॉट कैसे लें?

  1. ऐप के बिना एंड्रॉइड स्क्रीनशॉट कैसे लें ऐप के बिना एंड्रॉइड स्क्रीनशॉट कैसे लें

    मजेदार ट्वीट्स से लेकर काम से संबंधित मामलों तक, अपने फोन की स्क्रीन की तस्वीर लेना उपयोगी हो सकता है। जैसे, आप अपने डिवाइस की स्क्रीन की तस्वीरें लेने के लिए ऐप्स की ओर रुख करने के लिए ललचा सकते हैं। जबकि ऐप्स पूरी तरह से काम कर सकते हैं, आपको वास्तव में अपने डिवाइस पर स्क्रीनशॉट करने के लिए ऐप की

  1. Mac पर स्क्रीनशॉट कैसे लें Mac पर स्क्रीनशॉट कैसे लें

    Apple Mac वह सब कुछ करता है जो एक औसत कंप्यूटर उपयोगकर्ता अपने कंप्यूटर का उपयोग करने के लिए संभवतः करना चाहेगा, और इसमें स्क्रीनशॉट कैप्चर करना शामिल है।हालांकि, अधिकांश Mac उपयोगकर्ताओं को ठीक से पता नहीं है कि वे अपने Mac पर स्क्रीनशॉट कैसे कैप्चर कर सकते हैं, और यहां तक कि अगर वे जानते हैं, तो व

  1. एंड्रॉइड रिकवरी मोड के स्क्रीनशॉट लें: पूर्ण चरण-दर-चरण गाइड एंड्रॉइड रिकवरी मोड के स्क्रीनशॉट लें: पूर्ण चरण-दर-चरण गाइड

    स्क्रीनशॉट लेना अपने समूह के साथ सही जानकारी साझा करने के सर्वोत्तम तरीकों में से एक है। स्क्रीनशॉट किसी भी चीज़ के लिए किसी भी वैध सबूत जितना ही अच्छा काम करता है। यह ज्ञात है कि एंड्रॉइड में स्क्रीनशॉट लेना वॉल्यूम डाउन बटन और पावर बटन को एक साथ दबाने जितना आसान है। हालांकि, यदि आप अपने एंड्रॉइड क