简体   繁体   English

截取屏幕截图后,布局变了

[英]after taking screenshot the layout are streached

I created screen shot activity in programatically in android.using scroll view the after taking screen shot the layout are stretched. 我在android.programming中以编程方式创建了屏幕截图活动。 I will give the images for before taking after taking screen shot.this will help you to understand my problem.i don't know how to tell? 屏幕快照后,我将在拍摄之前给出图像。这将帮助您理解我的问题。我不知道该如何分辨?

before taking screen shot the layout , 在截屏之前 图片1

after taking screen shot the layout 截屏后的布局

图片2

you know the difference of two images.the activity after taking screen shot it extended i don't know why it happen? 您知道两个图像的区别。截屏后的活动扩展了,我不知道为什么会发生? how to solve this? 如何解决呢?

My code : 我的代码:

public class MainActivity extends AppCompatActivity {

    File cacheDir;
    final Context context = this;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button print = (Button) findViewById(R.id.btn_print);

        print.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                takeScreenShot();
            }
        });



    }

       private void takeScreenShot() {

        View u =  findViewById(R.id.activity_main);

        int spec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        u.measure(spec, spec);
        u.layout(0, 0, u.getMeasuredWidth(), u.getMeasuredHeight());

        Bitmap b = getBitmapFromView(u,u.getMeasuredHeight(),u.getMeasuredWidth());

        final String root = Environment.getExternalStorageDirectory().toString();

        File myPath = new File(root + "/saved_img");
        myPath.mkdirs();
        Random generator = new Random();
        int n = 10000;
        n = generator.nextInt(n);
        String fname = "Image-"+n+".jpg";
        File file = new File(myPath, fname);
        FileOutputStream fos = null;
        if(file.exists()) file.delete();
        try{
            fos = new FileOutputStream(file);
            b.compress(Bitmap.CompressFormat.JPEG, 100, fos);
            fos.flush();
            fos.close();


        } catch(FileNotFoundException e){
            e.printStackTrace();
        } catch (Exception e){
            e.printStackTrace();
        }

        Toast.makeText(this,"screen captured",Toast.LENGTH_SHORT).show();
    }



    public Bitmap getBitmapFromView(View u, int totalHeight, int totalWidth){
        Bitmap returnedBitmap = Bitmap.createBitmap(totalWidth,totalHeight , Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(returnedBitmap);

        Drawable bgDrawable = u.getBackground();
        if (bgDrawable != null)
            bgDrawable.draw(canvas);
        else
            canvas.drawColor(Color.WHITE);
        u.draw(canvas);
        return returnedBitmap;
    }
}

xml : xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.own.scrollviewimg.MainActivity"

    >


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        >

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="print"
        android:id="@+id/btn_print"
        />


    <ScrollView
        android:id="@+id/horizontalscroll"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            >


            <ImageView
                android:layout_width="500dp"
                android:layout_height="200dp"
                android:src="@mipmap/ic_launcher"
                />

              <TextView
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:text="@string/testdata"
                  android:textSize="25sp"
                  android:textStyle="bold"
                  />


              </LinearLayout>

          </ScrollView>


        </LinearLayout>

</RelativeLayout>

try this code 试试这个代码

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 v1 = getWindow().getDecorView().getRootView();
        v1.setDrawingCacheEnabled(true);
        Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
        v1.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();
    }
}

In your takeScreenShot() block, why are you using View.MeasureSpec 在您的takeScreenShot()块中,为什么要使用View.MeasureSpec
You can use view.getMeasuredHeight() and view.getMeasuredWidth() to the the height and width from the root view itself and also try to get the view from already inflated root by 您可以使用view.getMeasuredHeight()view.getMeasuredWidth()到根视图本身的高度和宽度,也可以尝试从已经膨胀的根中获取视图

View view = getWindow().getDecorView().getRootView();

So just comment out 所以就注释掉

 /* int spec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
    u.measure(spec, spec);
    u.layout(0, 0, u.getMeasuredWidth(), u.getMeasuredHeight()); */

and the rest is fine 其余的都很好

Note: the width of your ImageView may be more than the current screen size 注意:ImageView的宽度可能大于当前的屏幕尺寸

finally i found the answer.it's simple.i just recall the layout on after button click. 终于我找到了答案。很简单。我只需单击按钮后就可以查看布局。

my solved answer : 我解决的答案:

print.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                takeScreenShot();
                setContentView(R.layout.activity_main);
            }
        });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM