将视图转换为位图

10

可能是重复的问题:
在Android中将视图转换为位图而不显示它?

我试图从以下引用链接将视图转换为位图

链接文字

现在的问题是如何仅从视图中获取正在转换的位图。在示例中,作者使用了relativelayout.dispatchDraw(c),但这行代码会给我编译时错误,即:

来自ViewGroup的dispatchDraw(Canvas)方法不可见

这是我的代码,我已经将以下代码写入onCreate函数中

    Canvas c=null;

    //Create Layout
    RelativeLayout relativeView ;           
    RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
    RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT
    );

    relativeView = new RelativeLayout(this);            
    relativeView.setLayoutParams(lp);

    //Background of Layout
    Bitmap viewBgrnd  = BitmapFactory.decodeResource(getResources(),R.drawable.bgblack);
    relativeView.setBackgroundDrawable(new BitmapDrawable(viewBgrnd));

    //associated with canvas 
    Bitmap returnedBitmap =               Bitmap.createBitmap(320,480,Bitmap.Config.ARGB_8888);     
    c = new Canvas(returnedBitmap);
    Paint paint = new Paint();


    //Create Imageview that holds image             
    ImageView newImage = new ImageView(this);
    Bitmap srcBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.bgpink);
    newImage.setImageBitmap(srcBitmap);

    TextView newText = new  TextView(this);

    newText.setText("This is the text that its going to appear");       

    c.drawBitmap(viewBgrnd, 0, 0, paint);
            relativeView.layout(100, 0, 256, 256);  
    relativeView.addView(newImage);
    relativeView.addView(newText);


        // here i am getting compile time error so for timing i have replaced this line
        // with relativeView.draw(c);

    relativeView.dispatchDraw(c);

在这里,返回的位图应该包含(ImageView和TextView)的图像,但是这个位图只包含relativeView的背景位图,即bgblack。


1
我以前也遇到过同样的问题,但是没有找到解决方法。有人告诉我,绘图缓存可能是一个问题所在。但是我无法以正确的方式使其正常工作。 - Impression
我不知道如何使用绘制缓存。 - Hunt
如果我使用relativeView.setDrawingCacheEnabled(true);,然后尝试使用relativeView.getDrawingCache()获取缓存,我会得到一个空位图。 - Hunt
1
是的 - 必须调用requestLayout(),但我总是得到一个黑色位图。 - Impression
我尝试了requestLayout(),但仍然不起作用。现在,由于在调用relativeView.getDrawingCache()之前使用了relativeView.layout(0,0,10,10),我不再收到空位图,但返回的位图未显示我使用addView添加到relativeView中的ImageView和TextView。 - Hunt
2个回答

28

这是我的解决方案:

public static Bitmap getBitmapFromView(View view) {
    Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(returnedBitmap);
    Drawable bgDrawable =view.getBackground();
    if (bgDrawable!=null) 
        bgDrawable.draw(canvas);
    else 
        canvas.drawColor(Color.WHITE);
    view.draw(canvas);
    return returnedBitmap;
}

祝您愉快 :)


6
这对我很有效:
Bitmap viewCapture = null;

theViewYouWantToCapture.setDrawingCacheEnabled(true);

viewCapture = Bitmap.createBitmap(theViewYouWantToCapture.getDrawingCache());

theViewYouWantToCapture.setDrawingCacheEnabled(false);

我认为视图必须是可见的(即getVisiblity()== View.VISIBLE)。如果您试图捕获它但同时向用户隐藏它,您可以将其移动到屏幕外或放置在其上。


1
这个问题是图片质量如何提高。在转换位图时,看起来有点模糊或半透明。 - Praveen
谢谢。这是一个简单直接的 API 方法:可以通过将标志设置为 true 并调用 getDrawingCache(),手动生成此视图的位图副本。 - Dmytro Boichenko

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接