我们在从特定视图生成位图时遇到了问题。限制是它不能被渲染视图(绘图)。有没有人有任何提示如何解决这个问题?
类视图的文档 ( http://developer.android.com/reference/android/view/View.html ) 对 Android 用于呈现视图的步骤进行了一些解释。在这种情况下,我们会进入“布局”步骤,但不会进入“绘图”。任何有任何想法的人都可以举一个例子吗?
我的代码正在生成异常:错误 -> 宽度和高度必须> 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);
}
}