如何拍摄ImageView的屏幕截图?

时间:2013-11-16 00:10:15

标签: android imageview screenshot bmp

您好我正在尝试使用Imageview的屏幕截图,我有一些绘图。 我能够做绘图部分并保存部分,因为我使用了

 "View v = view.getRootView();"

要在整个屏幕之前进行捕捉,我发现它有效,因为它可以在画廊中看到它。

但是我现在只需捕捉图像视图的截图(不是整个屏幕)而且我遇到了麻烦。当我的用户按下保存按钮时,它希望它创建一个仅仅是imageview的bmp。目前它使用我的两种方法为我捕获黑屏。我认为它的高度和宽度都合适,但它仍然不起作用。

我从Taking a "screenshot" of a specific layout in 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);
...

方法1捕获imageView的屏幕截图

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

方法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:

<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>

This is screenshot of eclipse that was displayed on the emulator then drawned with one line at the top then saved and loaded back to the imageView

1 个答案:

答案 0 :(得分:0)

你为什么打电话给z.buildDrawingCache(true)

这应该足以创建视图的位图

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