简体   繁体   English

如何拍摄ImageView的屏幕截图?

[英]How to take screen shot of a ImageView?

Hello i'm trying to take a screen shot of an Imageview that i have some drawing on it. 您好,我正在尝试拍摄Imageview的屏幕截图,上面有一些绘图。 I'm able to do the drawing part and saving part because I used the 我可以做绘图和保存零件,因为我使用了

 "View v = view.getRootView();"

To caputure the whole screen before and i see that it works because it can see it in gallery. 捕获整个屏幕之前,我看到它可行,因为它可以在图库中看到它。

However i need to only capture the screenshot of a imageview at the moment(not whole screen) and i'm having trouble. 但是,我现在只需要捕获imageview的屏幕截图(而不是整个屏幕),就遇到了麻烦。 When my user press the save button it want it to create a bmp of just the imageview. 当我的用户按下“保存”按钮时,它希望它创建一个仅包含imageview的bmp。 Currently it captures a blackscreen for me with my two method before. 目前,它使用我之前的两种方法为我捕获了黑屏。 I thought it got the height and width right and it still doesn't work. 我以为它的高度和宽度正确,但仍然无法正常工作。

i'm getting my method 2 ideas from Taking a "screenshot" of a specific layout in Android but it is not working even though a lot of other questions are similar to it and i checked them all out. 我从在Android中获取特定布局的“屏幕截图”时得到了方法2的想法,但是即使很多其他问题都与之类似,但仍无法正常工作,我还是全部检查了一下。

//In constructor to let u guys know how i set up the imageView
imageView.setImageBitmap(bitmap);
...
imageView = (ImageView) this.findViewById(R.id.ImageView);
...

Method 1 to capture screen shot of the imageView 方法1捕获imageView的屏幕截图

Bitmap bm = Bitmap.createBitmap(imageView.getWidth(), imageView.getHeight(), Bitmap.Config.ARGB_8888);

method 2 to capture screen shot of the imageView 方法2捕获imageView的屏幕截图

View z = (ImageView) findViewById(R.id.ImageView);
               z.setDrawingCacheEnabled(true);   
               int totalHeight = z.getHeight();
               int totalWidth = z.getWidth();
                z.layout(0, 0, totalWidth, totalHeight);       
                z.buildDrawingCache(true);
                Bitmap bm = Bitmap.createBitmap(z.getDrawingCache());             
                z.setDrawingCacheEnabled(false);
                    Toast.makeText(Draw.this, "Taking Screenshot", Toast.LENGTH_SHORT).show();  
                    MediaStore.Images.Media.insertImage(getContentResolver(), bm, null, null); 

XML here: XML在这里:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/RL"
        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=".Draw" >

    <ImageView
        android:id="@+id/ImageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/btnLoad2"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:src="@drawable/ic_launcher" />

    <Button
        android:id="@+id/BtnSave"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/ImageView"
        android:layout_alignTop="@+id/btnLoad2"
        android:text="Save Picture" />

    <Button
        android:id="@+id/btnLoad2"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/ImageView"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="40dp"
        android:layout_toLeftOf="@+id/BtnSave"
        android:text="@string/Load" />

</RelativeLayout>

这是Eclipse的屏幕快照,该屏幕快照显示在模拟器上,然后在顶部绘制一行,然后保存并加载回imageView

Why are you calling z.buildDrawingCache(true) ? 为什么要调用z.buildDrawingCache(true)

This should be sufficent to create a bitmap of your View 这足以创建您的View的位图

View z = (ImageView) findViewById(R.id.ImageView);
z.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(v.getDrawingCache());

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

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