安卓:位图,从超大的观点 [英] Android: Bitmap from oversized view
问题描述
问题描述:
我已经测试了以下两种方法中,为了创建一个从视图位图snaphot(例如RelativeLayout的在这种情况下)。这两种方法的工作非常适合查看哪些维度(如宽度和高度)小于设备的屏幕(如480x900)的尺寸。观的可见部分被捕获并写入该位图。不幸的是,位图是黑色的,在查看的unvisible部分。
PROBLEM DESCRIPTION:
I have tested the following two methods, in order to create a bitmap snaphot from a View (e.g. a RelativeLayout in this case). Both methods work excellent for Views which dimensions (e.g. width and height) are less than the dimensions of the device screen (e.g. 480x900). The visible part of the View is captured and written into the bitmap. Unfortunately, the bitmap is black in the unvisible part of the View.
问:
我怎样才能捕捉也是鉴于unvisible部分?
QUESTION:
How can I capture also the unvisible part of the 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);
}
}
推荐答案
我在寻找的解决方案,这一点,并在结束时,设法拿出自己的解决方案其实很简单。
I was searching for the solution to this too and, in the end, managed to come up with my own solution which is actually quite simple.
在我的情况,我有一个的LinearLayout
其中载有一系列的查看
元素,其中一些有时呈未在屏幕上,垂直下方屏幕边界的末端。我能保存查看
作为位图(见 loadBitmapFromView()
,下同),但遇到了麻烦,当它扩展超出屏幕的底部。
In my case, I had a LinearLayout
which contained a number of View
elements, some of which sometimes were not on screen, vertically below the end of the screen bounds. I was able to save the View
as a Bitmap (see loadBitmapFromView()
, below) but had trouble when it extended beyond the bottom of the screen.
我的解决办法是让一个滚动型
复合的的LinearLayout
部分。
My solution was to make the LinearLayout
part of a ScrollView
composite.
例如。
此:
<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
,以确保滚动型扩展到内容的高度。
Note use of wrap_content
to ensure ScrollView scales to the height of your content.
<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>
这似乎保证了屏幕外的项目可以手动布局的时候,我想了查看
保存为位图。我用下面的方法来保存该位图。我发现这早得多的SO,但我似乎无法找到秩序的问题,以正确地引用它。(不管你是谁 - !谢谢)
This seemed to ensure that the offscreen items could be manually laid out when I wanted to save the View
as a bitmap. I used the following method to save the bitmap. I found this much earlier on SO but I can't seem to find the question in order to reference it properly (whoever you are - thank you!).
有关下面的方法,我传递一个参照的LinearLayout
上方(<$ C C $> my_linear_layout )
For the following method, I pass in a reference to the LinearLayout
above (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;
}
这篇关于安卓:位图,从超大的观点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!