Android Take Screeshot宽度过宽

时间:2018-09-23 09:44:56

标签: android

我正在使用下面的代码以编程方式捕获线性布局的屏幕布局,问题是屏幕截图图像太宽,好像是横向图像,我的活动是potrait,其中height为width> width但对于由于某些原因,此代码捕获了宽度>高度的屏幕截图,并且看起来像是水平矩形而不是垂直矩形。视图的方向是正确的,因此它肯定会以正确的方向(potrait)捕获,但是太宽了,视图分开得更远,更小。

    private void takeScreenShot() {

    ll_screenshot = findViewById(R.id.ll_screenshot);

    try {

        cacheDir = new File(android.os.Environment.getExternalStorageDirectory(), "store");

        if (!cacheDir.exists()) {
            cacheDir.mkdirs();
        }

        ll_screenshot.setDrawingCacheEnabled(true);
        ll_screenshot.measure(
                View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
                View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
        ll_screenshot.layout(0, 0, ll_screenshot.getMeasuredWidth(), ll_screenshot.getMeasuredHeight());

        ll_screenshot.buildDrawingCache(true);
        Bitmap bm = Bitmap.createBitmap(ll_screenshot.getDrawingCache());
        ll_screenshot.setDrawingCacheEnabled(false); // clear drawing cache

        path = new File(android.os.Environment.getExternalStorageDirectory(),"store") + "/screenshot.jpg";
        savePic(bm, path);

        shareFile();

    } catch (NullPointerException e) {
        e.printStackTrace();
    }
}
public static void savePic(Bitmap b, String strFileName) {
    FileOutputStream fos;
    try {
        fos = new FileOutputStream(strFileName);
        b.compress(Bitmap.CompressFormat.PNG, 90, fos);
        fos.flush();
        fos.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
private void shareFile() {

    String file_path = path.toString();

    Intent intentShareFile = new Intent(Intent.ACTION_SEND);
    File fileWithinMyDir = new File(file_path);

    if(fileWithinMyDir.exists()) {
        intentShareFile.setType("image/*");
        intentShareFile.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://"+file_path));

        intentShareFile.putExtra(Intent.EXTRA_SUBJECT, "Share File...");
        intentShareFile.putExtra(Intent.EXTRA_TEXT, "Share File...");

        startActivity(Intent.createChooser(intentShareFile, "Share File"));
    }
}

屏幕截图: enter image description here

编辑(XML):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@drawable/bg_menu_h"
    >
 <LinearLayout
    android:id="@+id/ll_screenshot"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:background="@drawable/bg_menu_h"
     android:layout_weight="1"
    >
     <LinearLayout
         android:layout_width="match_parent"
         android:layout_height="40dp"
         android:orientation="horizontal"
         android:background="@drawable/bg_menu_h"
         >
         <TextView
             android:id="@+id/tvItem"
             android:layout_height="match_parent"
             android:layout_width="match_parent"
             android:textStyle="bold"
             android:gravity="center"
             android:text=""
             android:textSize="25sp"
             />
     </LinearLayout>

     <LinearLayout
         android:layout_width="match_parent"
         android:layout_height="40dp"
         android:orientation="horizontal"
         android:background="@drawable/bg_menu_h"
         >
         <TextView
             android:id="@+id/tvBrandLeft"
             android:layout_height="match_parent"
             android:layout_width="match_parent"
             android:gravity="center"
             android:text="Sunlight"
             android:textSize="25sp"
             android:layout_marginLeft="10dp"
             android:layout_weight="1"
             />
         <TextView
             android:id="@+id/tvBrandRight"
             android:layout_height="match_parent"
             android:layout_width="match_parent"
             android:gravity="center"
             android:text="Sunlight"
             android:textSize="25sp"
             android:layout_marginLeft="10dp"
             android:layout_weight="1"
             />
     </LinearLayout>

     <LinearLayout
         android:layout_width="match_parent"
         android:layout_height="40dp"
         android:orientation="horizontal"
         android:background="@drawable/bg_menu_h"
         >
         <TextView
             android:id="@+id/tvSizeLeft"
             android:layout_height="match_parent"
             android:layout_width="match_parent"
             android:gravity="center"
             android:text="1KG"
             android:textSize="25sp"
             android:layout_marginLeft="10dp"
             android:layout_weight="1"
             />
         <TextView
             android:id="@+id/tvSizeRight"
             android:layout_height="match_parent"
             android:layout_width="match_parent"
             android:gravity="center"
             android:text="1KG"
             android:textSize="25sp"
             android:layout_marginLeft="10dp"
             android:layout_weight="1"
             />
     </LinearLayout>
     <LinearLayout
         android:id="@+id/ll_middle"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:orientation="horizontal"
         android:layout_weight="1"
         >
         <ImageView
             android:id="@+id/ivTrophyLeft"
             android:layout_width="60dp"
             android:layout_height="60dp"
             android:layout_gravity="bottom|left"
             android:background="@android:color/transparent"
             android:src="@drawable/questionmark"
             android:adjustViewBounds="true"
             />
         <ImageView
             android:id="@+id/ivItemLeft"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             android:layout_gravity="bottom|left"
             android:background="@android:color/transparent"
             android:src="@drawable/questionmark"
             android:adjustViewBounds="true"
             android:layout_weight="1"
             android:paddingRight="5dp"
             />
         <ImageView
             android:id="@+id/ivItemRight"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             android:layout_gravity="bottom|left"
             android:background="@android:color/transparent"
             android:src="@drawable/questionmark"
             android:adjustViewBounds="true"
             android:layout_weight="1"
             android:paddingRight="5dp"
             />
         <ImageView
             android:id="@+id/ivTrophyRight"
             android:layout_width="60dp"
             android:layout_height="60dp"
             android:layout_gravity="bottom|left"
             android:background="@android:color/transparent"
             android:src="@drawable/questionmark"
             android:adjustViewBounds="true"
             />
     </LinearLayout>
    <LinearLayout
        android:id="@+id/ll_price"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:orientation="horizontal"
        >
        <TextView 
            android:id="@+id/tvPriceLeft"
            android:layout_height="match_parent"
            android:layout_width="match_parent" 
            android:textStyle="bold" 
            android:gravity="center"
            android:text="0.00" 
            android:textSize="35sp"
            android:layout_weight="1"
            />
        <TextView 
            android:id="@+id/tvPriceDifference"
            android:layout_height="70dp"
            android:layout_width="70dp" 
            android:textStyle="bold" 
            android:gravity="center" 
            android:text="0.00"
            android:textSize="20sp"
            android:background="@drawable/roundstar"
            android:textColor="@color/white"
             />
        <TextView 
            android:id="@+id/tvPriceRight"
            android:layout_height="match_parent"
            android:layout_width="match_parent" 
            android:textStyle="bold" 
            android:gravity="center"
            android:text="0.00"
            android:textSize="35sp"
            android:layout_weight="1"
             />
    </LinearLayout>
    <LinearLayout
        android:id="@+id/ll_shop"
        android:layout_width="match_parent"
        android:layout_height="55dp"
        android:orientation="horizontal"
        >
    <ImageView 
            android:id="@+id/ivShopLeft"
            android:layout_width="match_parent" 
            android:layout_height="match_parent" 
            android:layout_gravity="bottom|left" 
            android:background="@android:color/transparent" 
            android:src="@drawable/shop" 
            android:adjustViewBounds="true" 
            android:layout_weight="1"
            android:paddingRight="5dp"
            android:layout_margin="5dp"
            />
        <ImageView 
            android:id="@+id/ivShopRight"
            android:layout_width="match_parent" 
            android:layout_height="match_parent" 
            android:layout_gravity="bottom|right" 
            android:background="@android:color/transparent" 
            android:src="@drawable/shop" 
            android:adjustViewBounds="true" 
            android:layout_weight="1"
            android:paddingLeft="5dp"
            android:layout_margin="5dp"
            />
    </LinearLayout>
     <LinearLayout
         android:id="@+id/ll_bottom_sub"
         android:layout_width="match_parent"
         android:layout_height="20dp"
         android:orientation="horizontal"
         android:gravity="right"
         >
         <ImageView
             android:id="@+id/ivLogo"
             android:layout_height="match_parent"
             android:layout_width="20dp"
             android:src="@drawable/ic_launcher"
             android:background="@android:color/transparent"
             android:scaleType="fitEnd"
             />
         <TextView
             android:id="@+id/tvCompare"
             android:layout_height="match_parent"
             android:layout_width="wrap_content"
             android:textStyle="bold|italic"
             android:gravity="left|bottom"
             android:text="App"
             android:textSize="10sp"
             android:layout_marginRight="10dp"
             />
     </LinearLayout>
 </LinearLayout>
<LinearLayout
    android:id="@+id/ll_blank"
    android:layout_width="match_parent"
    android:layout_height="75dp"
    android:orientation="horizontal"
    >
</LinearLayout>

1 个答案:

答案 0 :(得分:0)

在您的方法takeScreenShot()中进行此更改。

private void takeScreenShot() {

    ll_screenshot = findViewById(R.id.main);
----
   //other code
----
}

//更改为R.id.main的{​​{1}}