以A"截图"在Android的特定布局截图、布局、QUOT、Android

2023-09-12 22:37:30 作者:衹上

我有是紧密相连的两个主要问题。我在看从编程的角度来看这些问题。

(1) - 我要采取的具体布局的内容截图,即滚动型嵌套在的LinearLayout

(2) - 随着滚动型具有溢出了画面的(因而滚动成为可能),我怎么能保证屏幕截图包括那些元素含量在屏幕上不显示?

这是code我用的是当前块。它的获取屏幕截图但仅用于整个屏幕的作业。 这是即使 R.id.boss 滚动型,而不是主要的ID 的LinearLayout

 查看视图= findViewById(R.id.boss);
视图V = view.getRootView(); //这似乎并没有有所作为
v.setDrawingCacheEnabled(真正的);
v.measure(MeasureSpec.makeMeasureSpec(0,MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0,MeasureSpec.UNSPECIFIED));
v.layout(0,0,v.getMeasuredWidth(),v.getMeasuredHeight());
v.buildDrawingCache(真正的);
位图B = Bitmap.createBitmap(u.getDrawingCache());
v.setDrawingCacheEnabled(假);
 

在此先感谢。

编辑:

我犯了一些错误。我用 R.id.boss 这是错误的资源。我现在可以采取滚动型的截图孤独,少了超出屏幕的部分。

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:ID =@ + ID /老大
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:layout_gravity =顶
机器人:方向=垂直>

    <的TextView
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT
    机器人:文本=F
    机器人:textAppearance =机器人:ATTR / textAppearanceMedium/>

    <的TextView
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文本=通过图片的URL分析
        机器人:textAppearance =:/>中的Andr​​oid ATTR / textAppearanceSmall?

        <的LinearLayout
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =WRAP_CONTENT
             >

                        <的EditText
                            机器人:ID =@ + ID / mUrl
                            机器人:layout_width =FILL_PARENT
                            机器人:layout_height =WRAP_CONTENT
                            机器人:layout_weight =0.7
                            机器人:文本=HTTP://>

                            <不是requestFocus />
                        < /的EditText>

                        < ImageView的
                            机器人:ID =@ +用户ID /呼叫
                            机器人:layout_width =75dp
                            机器人:layout_height =50dp
                            机器人:layout_weight =0.3
                            机器人:文本=ABC
                            机器人:SRC =@可绘制/运行/>

        < / LinearLayout中>

     <的TextView
         机器人:layout_width =FILL_PARENT
         机器人:layout_height =WRAP_CONTENT
         机器人:文本=ABC
         机器人:textAppearance =:/>中的Andr​​oid ATTR / textAppearanceSmall?

     <的LinearLayout
         机器人:layout_width =FILL_PARENT
         机器人:layout_height =WRAP_CONTENT>

         <的EditText
             机器人:ID =@ + ID /文件路径
             机器人:layout_width =FILL_PARENT
             机器人:layout_height =WRAP_CONTENT
             机器人:layout_weight =0.55/>

         < ImageView的
             机器人:ID =@ + ID / CAM
             机器人:layout_width =75dp
             机器人:layout_height =50dp
             机器人:layout_weight =0.15
             机器人:SRC =@可绘制/ CAM/>

         < ImageView的
             机器人:ID =@ + ID /浏览
             机器人:layout_width =75dp
             机器人:layout_height =50dp
             机器人:layout_weight =0.15
             机器人:SRC =@可绘制/文件夹
             机器人:文本=B/>

         < ImageView的
             机器人:ID =@ + ID /上传
             机器人:layout_width =75dp
             机器人:layout_height =50dp
             机器人:layout_weight =0.15
             机器人:SRC =@可绘制/运行
             机器人:文本=A/>
     < / LinearLayout中>


     <的LinearLayout
         机器人:ID =@ + ID /巴巴
         机器人:layout_width =FILL_PARENT
         机器人:layout_height =WRAP_CONTENT
         机器人:方向=垂直>


         <滚动型
             机器人:ID =@ + ID /滚动
             机器人:layout_width =FILL_PARENT
             机器人:layout_height =150dp
             机器人:layout_weight =0.7>

             <的LinearLayout
                 机器人:layout_width =FILL_PARENT
                 机器人:layout_height =186dp
                 机器人:方向=垂直>

                 < ImageView的
                     机器人:ID =@ + ID / PIC
                     机器人:layout_width =FILL_PARENT
                     机器人:layout_height =FILL_PARENT
                     机器人:layout_weight =1/>

                 <的TextView
                     机器人:layout_width =WRAP_CONTENT
                     机器人:layout_height =WRAP_CONTENT
                     机器人:layout_weight =1
                     机器人:文本=面部识别
                     机器人:textAppearance =机器人:ATTR / textAppearanceMedium/>

                 <的TextView
                     机器人:ID =@ + ID /文本3
                     机器人:layout_width =WRAP_CONTENT
                     机器人:layout_height =WRAP_CONTENT
                     机器人:layout_weight =1
                     机器人:文本=小文
                     机器人:textAppearance =:/>中的Andr​​oid ATTR / textAppearanceSmall?

                 <的TextView
                     机器人:ID =@ + ID / avmarwe
                     机器人:layout_width =WRAP_CONTENT
                     机器人:layout_height =WRAP_CONTENT
                     机器人:layout_weight =1
                     机器人:文本=性别和年龄
                     机器人:textAppearance =机器人:ATTR / textAppearanceMedium/>

                 <的TextView
                     机器人:ID =@ + ID / text1中
                     机器人:layout_width =WRAP_CONTENT
                     机器人:layout_height =WRAP_CONTENT
                     机器人:layout_weight =1
                     机器人:文本=小文
                     机器人:textAppearance =:/>中的Andr​​oid ATTR / textAppearanceSmall?

                 <的TextView
                     机器人:ID =@ + ID / skahasd
                     机器人:layout_width =WRAP_CONTENT
                     机器人:layout_height =WRAP_CONTENT
                     机器人:layout_weight =1
                     机器人:文本=前pression和心情
                     机器人:textAppearance =机器人:ATTR / textAppearanceMedium/>

                 <的TextView
                     机器人:ID =@ + ID /文本2
                     机器人:layout_width =WRAP_CONTENT
                     机器人:layout_height =WRAP_CONTENT
                     机器人:layout_weight =1
                     机器人:文本=小文
                     机器人:textAppearance =:/>中的Andr​​oid ATTR / textAppearanceSmall?

                 <的TextView
                     机器人:ID =@ + ID / dsfsfs
                     机器人:layout_width =WRAP_CONTENT
                     机器人:layout_height =WRAP_CONTENT
                     机器人:layout_weight =1
                     机器人:文本=名人面部匹配
                     机器人:textAppearance =机器人:ATTR / textAppearanceMedium/>

                 <的TextView
                     机器人:ID =@ + ID /文本4
                     机器人:layout_width =WRAP_CONTENT
                     机器人:layout_height =WRAP_CONTENT
                     机器人:layout_weight =1
                     机器人:文本=小文
                     机器人:textAppearance =:/>中的Andr​​oid ATTR / textAppearanceSmall?
             < / LinearLayout中>
         < /滚动型>

     < / LinearLayout中>


    <的LinearLayout
        机器人:ID =@ + ID / linearLayout1
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_gravity =底和GT;

        <按钮
            机器人:ID =@ + ID / C
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =50dp
            机器人:layout_weight =0.7
            机器人:文本=/>

        <按钮
            机器人:ID =@ + ID /股
            机器人:layout_width =70dp
            机器人:layout_height =50dp
            机器人:layout_weight =0.3
            机器人:文本=/>

    < / LinearLayout中>

< / LinearLayout中>
 
从入门到放弃

解决方案

感谢你们,我终于发现了什么是错误的。

视图V = view.getRootView(); 不应该被使用,因为它会叫我不希望根视图。我误以为这没有区别,因为我已经进入了错误的资源ID。

MeasureSpec 不知何故没有给出一个好交代的宽度和高度。所以,我最终使用另一种方法:

  ...
滚动型Z =(滚动型)findViewById(R.id.scroll);
INT totalHeight = z.getChildAt(0).getHeight();
INT totalWidth = z.getChildAt(0).getWidth();
u.layout(0,0,totalWidth,totalHeight);
...
 

作为滚动型的总高度可以由单个子元素,它具有被确定。

做了这些改变之后,我现在可以采取滚动型嵌套的屏幕截图,及其所有内容,可见或不可见。对于任何有兴趣,这里是code块,包括保存位图:

 查看U = findViewById(R.id.scroll);
            u.setDrawingCacheEnabled(真正的);
            滚动型Z =(滚动型)findViewById(R.id.scroll);
            INT totalHeight = z.getChildAt(0).getHeight();
            INT totalWidth = z.getChildAt(0).getWidth();
            u.layout(0,0,totalWidth,totalHeight);
            u.buildDrawingCache(真正的);
            位图B = Bitmap.createBitmap(u.getDrawingCache());
            u.setDrawingCacheEnabled(假);

            //保存位图
            字符串抽= Environment.getExternalStorageDirectory()的toString()+文件分割符+文件夹。
            。字符串文件名=新的SimpleDateFormat(yyyyMMddhhmm'_report.jpg')格式(新的日期());
            文件mypath中=新的文件(抽,文件名);
            FileOutputStream中FOS = NULL;
            尝试 {
                FOS =新的FileOutputStream(mypath中);
                b.com preSS(Bitmap.Com pressFormat.JPEG,100,FOS);
                fos.flush();
                fos.close();
                MediaStore.Images.Media.insertImage(getContentResolver(),B,屏幕,屏幕);
            }赶上(FileNotFoundException异常E){
                // TODO自动生成的catch块
                e.printStackTrace();
            }赶上(例外五){
                // TODO自动生成的catch块
                e.printStackTrace();
            }
 

I have two main issues which are closely linked. I am looking at these problems from a programmatic point of view.

(1) - I wish to take a screenshot of the contents of a SPECIFIC layout, i.e. a ScrollView nested in a LinearLayout.

(2) - As the ScrollView has content that spills out of the screen (hence scrolling made possible), how can I ensure that the screenshot includes the elements that are not visible on the screen?

This is the current block of code I use. It does the job of taking a screenshot but only for the entire screen. This is even though R.id.boss is the ID of the ScrollView and not the main LinearLayout.

View view = findViewById(R.id.boss);
View v = view.getRootView();// this does not seem to make a difference
v.setDrawingCacheEnabled(true);                                                
v.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), 
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());    
v.buildDrawingCache(true);
Bitmap b = Bitmap.createBitmap(u.getDrawingCache());             
v.setDrawingCacheEnabled(false);

Thanks in advance.

EDIT:

I've made a few mistakes. I used R.id.boss which is the wrong resource. I am now able to take a screenshot of the scrollview alone, less the out-of-screen parts.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/boss"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="top"
android:orientation="vertical" >

    <TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="F"
    android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Analyze via image URL"
        android:textAppearance="?android:attr/textAppearanceSmall" />

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
             >

                        <EditText
                            android:id="@+id/mUrl"
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"
                            android:layout_weight="0.7"
                            android:text="http://" >

                            <requestFocus />
                        </EditText>

                        <ImageView
                            android:id="@+id/call"
                            android:layout_width="75dp"
                            android:layout_height="50dp"
                            android:layout_weight="0.3"
                            android:text="ABC"
                            android:src="@drawable/run" />

        </LinearLayout>

     <TextView
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:text="ABC"
         android:textAppearance="?android:attr/textAppearanceSmall" />

     <LinearLayout
         android:layout_width="fill_parent"
         android:layout_height="wrap_content" >

         <EditText
             android:id="@+id/filepath"
             android:layout_width="fill_parent"
             android:layout_height="wrap_content"
             android:layout_weight="0.55" />

         <ImageView
             android:id="@+id/cam"
             android:layout_width="75dp"
             android:layout_height="50dp"
             android:layout_weight="0.15"
             android:src="@drawable/cam" />

         <ImageView
             android:id="@+id/browse"
             android:layout_width="75dp"
             android:layout_height="50dp"
             android:layout_weight="0.15"
             android:src="@drawable/folder"
             android:text="B" />

         <ImageView
             android:id="@+id/upload"
             android:layout_width="75dp"
             android:layout_height="50dp"
             android:layout_weight="0.15"
             android:src="@drawable/run"
             android:text="A" />
     </LinearLayout>


     <LinearLayout
         android:id="@+id/baba"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:orientation="vertical" >


         <ScrollView
             android:id="@+id/scroll"
             android:layout_width="fill_parent"
             android:layout_height="150dp"
             android:layout_weight="0.7" >

             <LinearLayout
                 android:layout_width="fill_parent"
                 android:layout_height="186dp"
                 android:orientation="vertical" >

                 <ImageView
                     android:id="@+id/pic"
                     android:layout_width="fill_parent"
                     android:layout_height="fill_parent"
                     android:layout_weight="1" />

                 <TextView
                     android:layout_width="wrap_content"
                     android:layout_height="wrap_content"
                     android:layout_weight="1"
                     android:text="Facial recognition"
                     android:textAppearance="?android:attr/textAppearanceMedium" />

                 <TextView
                     android:id="@+id/text3"
                     android:layout_width="wrap_content"
                     android:layout_height="wrap_content"
                     android:layout_weight="1"
                     android:text="Small Text"
                     android:textAppearance="?android:attr/textAppearanceSmall" />

                 <TextView
                     android:id="@+id/avmarwe"
                     android:layout_width="wrap_content"
                     android:layout_height="wrap_content"
                     android:layout_weight="1"
                     android:text="Gender and age"
                     android:textAppearance="?android:attr/textAppearanceMedium" />

                 <TextView
                     android:id="@+id/text1"
                     android:layout_width="wrap_content"
                     android:layout_height="wrap_content"
                     android:layout_weight="1"
                     android:text="Small Text"
                     android:textAppearance="?android:attr/textAppearanceSmall" />

                 <TextView
                     android:id="@+id/skahasd"
                     android:layout_width="wrap_content"
                     android:layout_height="wrap_content"
                     android:layout_weight="1"
                     android:text="Expression and mood"
                     android:textAppearance="?android:attr/textAppearanceMedium" />

                 <TextView
                     android:id="@+id/text2"
                     android:layout_width="wrap_content"
                     android:layout_height="wrap_content"
                     android:layout_weight="1"
                     android:text="Small Text"
                     android:textAppearance="?android:attr/textAppearanceSmall" />

                 <TextView
                     android:id="@+id/dsfsfs"
                     android:layout_width="wrap_content"
                     android:layout_height="wrap_content"
                     android:layout_weight="1"
                     android:text="Celebrity Facial Match"
                     android:textAppearance="?android:attr/textAppearanceMedium" />

                 <TextView
                     android:id="@+id/text4"
                     android:layout_width="wrap_content"
                     android:layout_height="wrap_content"
                     android:layout_weight="1"
                     android:text="Small Text"
                     android:textAppearance="?android:attr/textAppearanceSmall" />
             </LinearLayout>
         </ScrollView>

     </LinearLayout>


    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom" >

        <Button
            android:id="@+id/c"
            android:layout_width="fill_parent"
            android:layout_height="50dp"
            android:layout_weight="0.7"
            android:text="" />

        <Button
            android:id="@+id/share"
            android:layout_width="70dp"
            android:layout_height="50dp"
            android:layout_weight="0.3"
            android:text="" />

    </LinearLayout>

</LinearLayout>

解决方案

Thanks to you guys, I've finally found out what was wrong.

View v = view.getRootView(); should not be used because it will call the root view which I do not want. I mistakenly thought this did not make a difference because I had entered the wrong resource ID.

MeasureSpec somehow did not give a good account of the width and height. So I ended up using another method:

...
ScrollView z = (ScrollView) findViewById(R.id.scroll);
int totalHeight = z.getChildAt(0).getHeight();
int totalWidth = z.getChildAt(0).getWidth();
u.layout(0, 0, totalWidth, totalHeight);
...

As ScrollView's total height can be determined by the single child element that it has.

After making these changes, I am now able to take a screenshot of a nested ScrollView and all its contents, visible or not. For anyone interested, here is the block of code including the saving of the bitmap:

            View u = findViewById(R.id.scroll);
            u.setDrawingCacheEnabled(true);                                                
            ScrollView z = (ScrollView) findViewById(R.id.scroll);
            int totalHeight = z.getChildAt(0).getHeight();
            int totalWidth = z.getChildAt(0).getWidth();
            u.layout(0, 0, totalWidth, totalHeight);    
            u.buildDrawingCache(true);
            Bitmap b = Bitmap.createBitmap(u.getDrawingCache());             
            u.setDrawingCacheEnabled(false);

            //Save bitmap
            String extr = Environment.getExternalStorageDirectory().toString() +   File.separator + "Folder";
            String fileName = new SimpleDateFormat("yyyyMMddhhmm'_report.jpg'").format(new Date());
            File myPath = new File(extr, fileName);
            FileOutputStream fos = null;
            try {
                fos = new FileOutputStream(myPath);
                b.compress(Bitmap.CompressFormat.JPEG, 100, fos);
                fos.flush();
                fos.close();
                MediaStore.Images.Media.insertImage(getContentResolver(), b, "Screen", "screen");
            }catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }