【问题标题】:Android - Capture ScreenShot Programmatically without title barAndroid - 以编程方式捕获屏幕截图而没有标题栏
【发布时间】:2014-01-20 07:18:11
【问题描述】:

我正在开发一个应用程序,我想截取当前屏幕的屏幕截图,但没有标题栏。我知道捕获屏幕截图的代码,但无法自定义它。

代码:

munchscreen.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);
            }
        });

当我点击Clip 时,它应该会截取下面显示的屏幕截图,但不应该包括我圈出的部分..

XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#D8D5AA"
>


  <LinearLayout 
    android:id="@+id/clip_from_web_linearlayout1"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:background="@drawable/header"
    android:orientation="horizontal"             
    >
         <TextView 
        android:id="@+id/clip_from_web_textview_back"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="Back"
        android:layout_gravity="center"
        android:layout_marginLeft="5dp"
        android:textColor="#FFFFFF"
        android:textSize="18sp"
        /> 

    <TextView 
        android:id="@+id/clip_from_web_textview_header_title"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="Clip From Web"
        android:layout_gravity="center"
        android:layout_marginLeft="60dp"
        android:textColor="#FFFFFF"
        android:textSize="18sp"
        /> 

      <TextView 
        android:id="@+id/clip_from_web_textview_clip"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="Clip"
        android:layout_gravity="center"
        android:layout_marginLeft="60dp"
        android:textColor="#FFFFFF"
        android:textSize="18sp"
        /> 

    </LinearLayout>


          <Button
             android:layout_width="fill_parent"
             android:layout_height="wrap_content"
             android:text="@string/munch"
             android:id="@+id/munchscreen"
             android:layout_below="@+id/clip_from_web_linearlayout1"
           />
           <ImageView
             android:layout_width="200dp"
             android:layout_height="200dp"
             android:id="@+id/screenshots"
             android:layout_below="@+id/munchscreen"
             android:contentDescription="@string/app_name"
             android:layout_centerInParent="true"

             />

<RelativeLayout
    android:id="@+id/clip_from_web_bottom_bar"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_alignParentBottom="true"
    android:background="@drawable/header" >

    <Button 
        android:id="@+id/clip_from_web_previous"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_marginLeft="4dp"
        android:background="@android:drawable/ic_media_previous"
        android:layout_marginTop="2dp"
        />

    <Button 
        android:id="@+id/clip_from_web_reload"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:background="@android:drawable/ic_menu_rotate"
        android:layout_marginTop="5dp"
        />

    <Button 
        android:id="@+id/clip_from_web_next"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_toRightOf="@+id/clip_from_web_reload"
        android:layout_marginLeft="102dp"
        android:background="@android:drawable/ic_media_next"
        android:layout_marginTop="2dp"
        />

</RelativeLayout>

截屏前的图像..

截屏后的图像..

【问题讨论】:

  • @Piyush Gupta 我无法发布我的 xml。但是,我正在演示程序上尝试它,所以我将发布该程序的 xml..
  • 您是否在任何地方使用网络视图?
  • @Piyush Gupta:请检查我更新的问题。我已经发布了整个 XML 和正确的屏幕截图
  • @JiteshUpadhyay:: 不工作.. 请检查我更新的问题

标签: android


【解决方案1】:

把它带到另一个线性布局

  <LinearLayout 
    android:id="@+id/linear"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">       

       <Button
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:text="@string/munch"
         android:id="@+id/munchscreen"

       />
       <ImageView
         android:layout_width="200dp"
         android:layout_height="200dp"
         android:id="@+id/screenshots"
         android:contentDescription="@string/app_name"
         />
  </LinearLayout>

现在在 oncreate() 方法中找到此线性布局的 ID,使其成为全局变量

 LinearLayout ll;

 ll = (LinearLayout)findViewById(R.id.linear);

现在捕捉这个屏幕

 ll.setDrawingCacheEnabled(true);
 ll.buildDrawingCache(true);
 Bitmap cs = Bitmap.createBitmap(ll.getDrawingCache());
 ll.setDrawingCacheEnabled(false);

现在将此位图设置为您的 ImageView。

【讨论】:

  • 什么是rel? .. 最后一行
  • 请将您的代码编辑为ll.setDrawingCacheEnabled(true); .. 您写的是假的。如果我这样做,那么它会显示黑色图像。当我做到这一点时,它就完美了.. :)
  • 看看我写的第一行是正确的..因为它试图为它做准备,然后捕获屏幕它应该是错误的..所以不需要改变
  • 好的。美好的。但是,我告诉你我遇到的事情。当我实现它时,它对我有用。谢谢你..
【解决方案2】:

试试这个...

    munchscreen.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            View contentView = getWindow().getDecorView().findViewById(android.R.id.content);
            contentView.setDrawingCacheEnabled(true);
            contentView.buildDrawingCache();
            Bitmap drawingCache = contentView.getDrawingCache();
            BitmapDrawable bitmapDrawable = new BitmapDrawable(getResources(), drawingCache);
            image = (ImageView) findViewById(R.id.screenshots);
            image.setBackgroundDrawable(bitmapDrawable);
        }
    });

【讨论】:

    【解决方案3】:

    各位朋友,我已经解决了上述问题。

    按照以下步骤:-

    1. activity_main.xml

      <RelativeLayout
          android:id="@+id/toolbar_rl"
          android:layout_width="match_parent"
          android:layout_height="?android:actionBarSize"
          android:background="@color/header_color"
          android:visibility="visible">
      
          <LinearLayout
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="horizontal"
              android:weightSum="0"
              tools:ignore="UselessParent">
      
              <ImageView
                  android:layout_width="wrap_content"
                  android:layout_height="match_parent"
                  android:contentDescription="@string/app_name"
                  android:padding="@dimen/_8sdp"
                  android:src="@drawable/ic_back_24" />
      
              <TextView
                  android:layout_width="0dp"
                  android:layout_height="wrap_content"
                  android:layout_gravity="center"
                  android:layout_marginEnd="@dimen/_10sdp"
                  android:layout_marginStart="@dimen/_20sdp"
                  android:layout_weight="1"
                  android:gravity="center"
                  android:text="@string/one_time"
                  android:textColor="@color/white"
                  android:textSize="@dimen/_13sdp"
                  tools:ignore="RtlHardcoded" />
      
      
              <ImageView
                  android:layout_width="wrap_content"
                  android:layout_height="match_parent"
                  android:contentDescription="@string/app_name"
                  android:padding="@dimen/_8sdp"
                  android:src="@drawable/ic_menu_icon" />
      
      
          </LinearLayout>
      
      </RelativeLayout>
      
      <ScrollView
          android:id="@+id/tabScroll"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:fillViewport="true">
      
          <TableLayout
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:stretchColumns="1">
      
              <ImageView
                  android:layout_width="fill_parent"
                  android:layout_height="210dp"
                  android:adjustViewBounds="true"
                  android:background="@drawable/otp_background"
                  android:contentDescription="@string/app_name"
                  android:scaleType="fitXY">
      
              </ImageView>
      
              <TextView
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:layout_marginTop="@dimen/_16sdp"
                  android:background="#fff"
                  android:gravity="center"
                  android:text="Enter Unique Identification Number"
                  android:textColor="@color/header_color"
                  android:textSize="@dimen/_16sdp" />
      
              <LinearLayout
                  android:id="@+id/layout_otp"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:layout_gravity="center"
                  android:layout_marginTop="@dimen/_26sdp"
                  android:gravity="top|center"
                  android:orientation="horizontal">
      
                  <EditText
                      android:id="@+id/editTextone"
                      android:layout_width="@dimen/_40sdp"
                      android:layout_height="@dimen/_42sdp"
                      android:inputType="number"
                      android:maxLength="1"
                      android:nextFocusRight="@+id/editText_two"
                      tools:ignore="LabelFor">
      
                      <requestFocus />
                  </EditText>
      
                  <EditText
                      android:id="@+id/editTexttwo"
                      android:layout_width="@dimen/_40sdp"
                      android:layout_height="@dimen/_42sdp"
                      android:inputType="numberDecimal"
                      android:maxLength="1"
                      tools:ignore="LabelFor" />
      
                  <EditText
                      android:id="@+id/editTextthree"
                      android:layout_width="@dimen/_40sdp"
                      android:layout_height="@dimen/_42sdp"
                      android:inputType="numberDecimal"
                      android:maxLength="1"
                      tools:ignore="LabelFor" />
      
                  <EditText
                      android:id="@+id/editTextfour"
                      android:layout_width="@dimen/_40sdp"
                      android:layout_height="@dimen/_42sdp"
                      android:inputType="numberDecimal"
                      android:maxLength="1"
                      tools:ignore="LabelFor" />
      
                  <EditText
                      android:id="@+id/editTextfive"
                      android:layout_width="@dimen/_40sdp"
                      android:layout_height="@dimen/_42sdp"
                      android:inputType="numberDecimal"
                      android:maxLength="1"
                      tools:ignore="LabelFor" />
      
              </LinearLayout>
      
      
              <Button
                  android:id="@+id/btn_submit"
                  android:layout_width="@dimen/_220sdp"
                  android:layout_height="@dimen/_40sdp"
                  android:layout_gravity="center"
                  android:layout_marginBottom="@dimen/_46sdp"
                  android:layout_marginLeft="@dimen/_16sdp"
                  android:layout_marginRight="@dimen/_16sdp"
                  android:layout_marginTop="@dimen/_26sdp"
                  android:background="@drawable/editbox_one"
                  android:text="@string/capture"
                  android:textAllCaps="false"
                  android:textColor="#fff"
                  android:textSize="@dimen/_16sdp"
                  android:typeface="serif" />
          </TableLayout>
      </ScrollView>
      

      1. 这个 xml 看起来像。

    1. 在 MainActivity 中添加一些代码。

      3.1 声明按钮。

        Button button_submit;
      

      3.2 初始化按钮。

       button_submit = (Button) findViewById(R.id.btn_submit);
      

      3.3 将 set OnClickListener 设置为此按钮并调用一个方法,即

       takeScreenshot();
      

      注意:- 当您单击此按钮时,时间会自动捕获没有标题栏的屏幕,它将以当前日期、时间和 .jpg 扩展名存储在文件探索中(例如,Fri May 11 151938 GMT+0530 2018.jpg ) 并将捕获的图像作为位图,如果您愿意,可以将此位图设置为 ImageView 。

    2. takeScreenshot() 的代码。

       private void takeScreenshot() {
      
       Date now = new Date();
       android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now);
      
      try {
      
      String mPath = Environment.getExternalStorageDirectory().toString() + "/" + now + ".jpg";
      
      View u = findViewById(R.id.tabScroll);
      u.setDrawingCacheEnabled(true);
      ScrollView z = (ScrollView) findViewById(R.id.tabScroll);
      z.setBackgroundColor(getResources().getColor(R.color.white));
      int totalHeight = z.getChildAt(0).getHeight();
      int totalWidth = z.getChildAt(0).getWidth();
      u.layout(0, 0, totalWidth, totalHeight);
      u.buildDrawingCache(true);
      Bitmap bitmap = Bitmap.createBitmap(u.getDrawingCache());
      u.setDrawingCacheEnabled(false);
      
      File imageFile = new File(mPath);
      
      FileOutputStream outputStream = new FileOutputStream(imageFile);
      int quality = 100;
      bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);
      outputStream.flush();
      outputStream.close();
      
       } catch (Throwable e) {
      
        e.printStackTrace();
      }
      }
      
    3. 输出看起来像..

    希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-05
      • 2017-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-20
      相关资源
      最近更新 更多