Android:来自超大视图的位图

时间:2012-02-15 16:03:49

标签: android view bitmap

问题描述:
我已经测试了以下两种方法,以便从View创建一个位图snaphot(例如在这种情况下为RelativeLayout)。这两种方法对于视图都很有效,其尺寸(例如宽度和高度)小于设备屏幕的尺寸(例如480×900)。捕获视图的可见部分并将其写入位图。不幸的是,在View的不可见部分中,位图是黑色的。

问题:
我如何捕捉视图中不可见的部分?

CODE:

public class NewRelativeLayout extends RelativeLayout{

private Bitmap bmp;

public NewRelativeLayout(Context context){
    super(context);
    this.setLayoutParams(new RelativeLayout.LayoutParams(2000,2000));
    // add here methods to configure the RelativeLayout or to add children
    }

//This is the first method
public void makeSnapshot_Method1(){
    this.setDrawingCacheEnabled(true);   
    bmp = Bitmap.createBitmap(this.getDrawingCache());   
    this.setDrawingCacheEnabled(false);
    }

//This is the second method
public void makeSnapshot_Method2(){
    bmp = Bitmap.createBitmap(2000, 2000, Bitmap.Config.ARGB_8888);
    Canvas helpCanvas = new Canvas(bmp);
    this.draw(helpCanvas);
    }

}

3 个答案:

答案 0 :(得分:1)

我也在寻找解决方案,最后设法提出了我自己的解决方案,实际上非常简单。

在我的情况下,我有一个LinearLayout,其中包含许多View个元素,其中一些元素有时不在屏幕上,垂直位于屏幕边界的下方。我能够将View保存为位图(请参阅下面的loadBitmapFromView()),但是当它超出屏幕底部时会出现问题。

我的解决方案是制作LinearLayout复合词的ScrollView部分。

e.g。

:此:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/my_linear_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
 >
<!-- Some Views added here at runtime -->
</LinearLayout>

成为:

请注意使用wrap_content确保ScrollView缩放到内容的高度。

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scroll"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    >

    <LinearLayout
        android:id="@+id/my_linear_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
     >
        <!-- Some Views added here at runtime -->
    </LinearLayout>
</ScrollView>

这似乎可以确保在我想将View保存为位图时可以手动布局屏幕外项目。我使用以下方法来保存位图。我之前发现的很多,但是我似乎无法找到问题以便正确引用它(无论你是谁 - 谢谢!)。

对于以下方法,我传递了对LinearLayout上方my_linear_layout的引用。

public static Bitmap loadBitmapFromView(View view) {
    Bitmap bitmap = null;

    // width measure spec
    int widthSpec = View.MeasureSpec.makeMeasureSpec(
            view.getMeasuredWidth(), View.MeasureSpec.AT_MOST);
    // height measure spec
    int heightSpec = View.MeasureSpec.makeMeasureSpec(
            view.getMeasuredHeight(), View.MeasureSpec.AT_MOST);

    // measure the view
    view.measure(widthSpec, heightSpec);

    // set the layout sizes
    int left = view.getLeft();
    int top = view.getTop();
    int width = view.getMeasuredWidth();
    int height = view.getMeasuredHeight();
    int scrollX = view.getScrollX();
    int scrollY = view.getScrollY();

    view.layout(left, top, width + left, height + top);

    // create the bitmap

    bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),
            Bitmap.Config.ARGB_8888);

    // create a canvas used to get the view's image and draw it on the
    // bitmap

    Canvas c = new Canvas(bitmap);
    // position the image inside the canvas
    c.translate(-view.getScrollX(), -view.getScrollY());
    // get the canvas
    view.draw(c);

    return bitmap;
}

答案 1 :(得分:0)

取决于您要捕获的图像,视图背景或前景中的图像。无论哪种方式,您必须从它们获取Drawable对象,将它们转换为BitmapDrawable并从它们获取Bitmap。 代码: -

BitmapDrawable bd=(BitmapDrawable) view.getBackground(); //if you need background image
BitmapDrawable bd=(BitmapDrawable) view.getDrawable(); //if you need foreground image (Only available to some views who have an android:src xml property)
Bitmap bm=bd.getBitmap();

我希望这对你有用。

答案 2 :(得分:0)

我也试图通过ListView来解决这个问题,以获取屏幕上没有的所有行。

我找到了很多我想分享的内容:

此处有人询问是否可以从未绘制的视图创建位图: Bitmap from View not displayed on Android

在这里,他们声称它是重复的,并且它也得到了正确回答(?)它包含一些URL,其中包含一些旧的编码,但不编译。此外,可能的重复问题对我没有帮助。 Android get full View as Bitmap

我想我会尝试通过这种方法完成它: List View using bitmap 已经提出了相同的想法,现在我仍然要发现如何。

希望你能使用这些信息。