Android 将视图转换为位图

Android 将视图转换为位图,android,bitmap,android-canvas,Android,Bitmap,Android Canvas,可能重复: 我正在尝试从下面的参考链接将视图转换为位图 现在的问题是如何获得仅从视图转换的位图。在示例中,作者使用了relativelayout.dispatchDraw(c),但这一行给了我编译时错误,即 方法dispatchDraw(Canvas)from 类型视图组不可见 这是我的代码,我在onCreate函数中编写了以下代码 Canvas c=null; //Create Layout RelativeLayout relativeView ;

可能重复:

我正在尝试从下面的参考链接将视图转换为位图

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

方法dispatchDraw(Canvas)from 类型视图组不可见

这是我的代码,我在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的bacground位图,即bgblack,这适用于我:

Bitmap viewCapture = null;

theViewYouWantToCapture.setDrawingCacheEnabled(true);

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

theViewYouWantToCapture.setDrawingCacheEnabled(false);
我认为视图必须是可见的(即getVisiblity()==view.visible)。如果您试图捕获它,但同时将其隐藏给用户,您可以将其移出屏幕或在其上放置一些东西

以下是我的解决方案:

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;
}

享受:)

我以前也遇到过同样的问题,也没有找到解决办法。有人告诉我,图形缓存可能是一个点。但是我无法以正确的方式运行它。如果我使用relativeView.setDrawingCacheEnabled(true),我不知道如何使用draw Cache;之后,如果我尝试使用relativeView.getDrawingCache()获取缓存,我会得到空bitmapyeah--requestLayout()必须被调用,但我总是得到一个黑色的bitmapi。我尝试了requestLayout(),但它仍然不起作用。现在我没有得到空位图,因为我在调用relativeView.getDrawingCache()之前使用了relativeView.layout(0,0,10,10),但是返回的位图没有显示我使用AddViewIt添加到relativeView中的ImageView和TextView。但是如何提高图像质量呢。转换时,位图看起来模糊或半透明。谢谢。这是来自API的简单方法:通过将标志设置为true并调用getDrawingCache(),可以使用此API手动生成此视图的位图副本。