安卓:生成视图的位图无图纸 [英] Android: Generate bitmap of a view without drawing
问题描述
我们必须产生从特定视图的位图的问题。的限制是,它不能被呈现的视图(图)。有没有人有任何提示如何解决此问题?
类视图的文档( http://developer.android.com /reference/android/view/View.html )具有用于由Android渲染视图的步骤一些解释。在这种情况下,我们会在步骤得到布局,而不是引的。任何人谁有任何想法,可以显示一个例子吗?
我的code正在生成异常:
错误 - >宽度和高度必须> 0
...
公共静态位图loadBitmapFromView(视图v){
位图B = NULL;
尝试{
B = Bitmap.createBitmap(
v.getWidth(),
v.getHeight(),
Bitmap.Config.ARGB_8888);
帆布C =新的Canvas(B);
v.measure(v.getWidth(),v.getHeight());
v.layout(0,0,v.getWidth(),v.getHeight());
v.draw(C); }赶上(例外五){ Log.e(MainActivity.TAG,错误 - >中+ e.getMessage());
} 返回b;
}
公共无效卡(视图v){
LayoutInflater膨胀=(LayoutInflater)getBaseContext()
.getSystemService(LAYOUT_INFLATER_SERVICE);
查看查看=新景(getBaseContext());
鉴于= inflate.inflate(R.layout.list_item,NULL);
Log.d(MainActivity.TAG的getWidth - >中+ view.getWidth());
Log.d(MainActivity.TAG的getHeight - >中+ view.getHeight()); 位图B = loadBitmapFromView(视图);
如果(B!= NULL){ 的LinearLayout mainLayout =(的LinearLayout)findViewById(R.id.LinearLayout1);
ImageView的形象=新ImageView的(本);
image.setImageBitmap(二); mainLayout.addView(图片);
}
}
我找到了解决办法是这样的:
公共静态位图getScreenViewBitmap(最终查看V){
v.setDrawingCacheEnabled(真); v.measure(MeasureSpec.makeMeasureSpec(0,MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0,MeasureSpec.UNSPECIFIED));
v.layout(0,0,v.getMeasuredWidth(),v.getMeasuredHeight()); v.buildDrawingCache(真);
位图B = Bitmap.createBitmap(v.getDrawingCache());
v.setDrawingCacheEnabled(假); //明确绘图缓存 返回b;
}
We have a problem to generate a bitmap from a particular view. The restriction is that it can not be rendered view (drawing). Does anyone have any tips how to solve this?
The documentation of the class view (http://developer.android.com/reference/android/view/View.html) has some explanation of the steps used by Android to render a View. In the case, we would get in step "layout", but not in the "drawing". Anyone who has any idea, could show an example?
My code is generating the exception: error -> width and height must be> 0
...
public static Bitmap loadBitmapFromView(View v) {
Bitmap b = null;
try {
b = Bitmap.createBitmap(
v.getWidth(),
v.getHeight(),
Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
v.measure(v.getWidth(), v.getHeight());
v.layout(0, 0, v.getWidth(), v.getHeight());
v.draw(c);
} catch (Exception e) {
Log.e(MainActivity.TAG, "error -> "+e.getMessage());
}
return b;
}
public void snap(View v) {
LayoutInflater inflate = (LayoutInflater) getBaseContext()
.getSystemService(LAYOUT_INFLATER_SERVICE);
View view = new View(getBaseContext());
view = inflate.inflate(R.layout.list_item, null);
Log.d(MainActivity.TAG, "getWidth -> "+view.getWidth());
Log.d(MainActivity.TAG, "getHeight -> "+view.getHeight());
Bitmap b = loadBitmapFromView(view);
if (b != null) {
LinearLayout mainLayout = (LinearLayout) findViewById(R.id.LinearLayout1);
ImageView image = new ImageView(this);
image.setImageBitmap(b);
mainLayout.addView(image);
}
}
I found the solution this way:
public static Bitmap getScreenViewBitmap(final View v) {
v.setDrawingCacheEnabled(true);
v.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
v.buildDrawingCache(true);
Bitmap b = Bitmap.createBitmap(v.getDrawingCache());
v.setDrawingCacheEnabled(false); // clear drawing cache
return b;
}
这篇关于安卓:生成视图的位图无图纸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!