我正在以编程方式创建一个永远不会被绘制到屏幕上的view
(view
不会从XML中膨胀或在onCreate
期间在屏幕上设置。相反,我想创建一个用于图像的视图位图。但是,我无法获得位图/正确的位图。
我查看了网站,并使用了各种问题,例如:Converting a view to Bitmap without displaying it in Android?。最初,我使用了以下代码:
public Bitmap loadBitmapFromView(View v)
{
Bitmap bitmap = Bitmap.createBitmap(v.getLayoutParams().width, v.getLayoutParams().height, Bitmap.Config.ARGB_8888); //v.getWidth() and v.getHeight() are 0 since view isn't drawn
Canvas canvas = new Canvas(bitmap);
v.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
v.layout(0, 0, v.getLayoutParams().width, v.getLayoutParams().height); //v.getMeasuredHeight() and v.getMeasuredWidth() are zero --- not sure why?
v.draw(canvas);
return bitmap;
}
此代码创建了一个位图,但它始终为空,并且没有view
作为图像。然后我尝试了第三个答案(@Dwivedi Ji),为方便起见:
private Bitmap getViewBitmap(View v) {
v.clearFocus();
v.setPressed(false);
v.setDrawingCacheEnabled(true);
boolean willNotCache = v.willNotCacheDrawing();
v.setWillNotCacheDrawing(false);
int color = v.getDrawingCacheBackgroundColor();
v.setDrawingCacheBackgroundColor(0);
if (color != 0) {
v.destroyDrawingCache();
}
v.buildDrawingCache();
Bitmap cacheBitmap = v.getDrawingCache(); //This was always null
if (cacheBitmap == null) {
Log.e(TAG, "failed getViewBitmap(" + v + ")", new RuntimeException());
return null;
}
Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);
// Restore the view
v.destroyDrawingCache();
v.setWillNotCacheDrawing(willNotCache);
v.setDrawingCacheBackgroundColor(color);
v.setDrawingCacheEnabled(false);
return bitmap;
}
v.getDrawingCache()
调用始终返回null,这使我没有位图。
如何使用view
图像获取此位图?我为这个冗长的问题道歉(有点新发布在这里,如果您需要其他信息/说明,请告诉我)。
提前致谢!
修改
我尝试在位图中创建的类如下:
public class CircleView extends View
{
... //Constructors - Nothing special here
@Override protected void onDraw(Canvas canvas)
{
canvas.drawColor(0x00000000);
if(m_lineWidthPx == 0) //Previously set
return;
float centerX = getMeasuredHeight() / 2.f;
float centerY = getMeasuredHeight() / 2.f;
float radius = getMeasuredWidth() / 2.f - (m_lineWidthPx) / 2.f;
m_paint.setStrokeWidth(m_lineWidthPx); //m_paint is instantiated in the constructors
m_paint.setStyle(Paint.Style.STROKE);
m_paint.setAntiAlias(true);
canvas.drawCircle(centerX, centerY, radius, m_paint);
}
}
课堂上没有太多,基本上画一个圆圈。我省略了构造函数,因为那里没有特殊的东西。他们只需设置m_lineWidthPx
变量并实例化m_paint
变量。
答案 0 :(得分:1)
检查你的位图的大小,是不是0x0 pt?
我使用这种方法来做到这一点:
public static Bitmap getBitmapFromView(View view){
view.measure(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
Bitmap bitmap = Bitmap.createBitmap(view.getWidth(),
view.getHeight(),
Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(bitmap);
view.draw(c);
return bitmap;
}
答案 1 :(得分:1)
我需要在一段时间之前从View创建一个位图。经过大量的测试/失败后,我终于开始使用这种方法:
public static Bitmap getBitmapFromView(View view) {
view.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
Bitmap bitmap = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
view.draw(canvas);
return bitmap;
}