获取具有不可见TextView或ImageView的RelativeLayout位图

时间:2017-08-31 05:41:50

标签: android-bitmap drawingcache

我要创建一个相对布局的位图,该布局具有可见的ImageView,其中包含两个不可见的TextView和一个不可见的ImageView。但是不可见的视图数据未在位图中显示。如果我将所有那些不可见的视图设置为可见,则会在位图中显示,但如果隐藏则不显示。

我正在使用以下代码 -

 private Bitmap getBitmap(View v) {
    Bitmap bmp = null, b1 = null;
    RelativeLayout targetView = (RelativeLayout) v;
    targetView.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
    targetView.buildDrawingCache();
    b1 = targetView.getDrawingCache();
    bmp = b1.copy(Bitmap.Config.ARGB_8888, true);
    targetView.destroyDrawingCache();
    return bmp;
}

我也使用了以下链接,但这也没有给我预期的结果。

Getting bitmap from a view visible-invisible

我真的在修复。

1 个答案:

答案 0 :(得分:2)

绘图缓存会保存当前在屏幕上绘制的位图。当然,这包含隐藏的视图。

您在链接中提供的文章与您的代码之间的主要区别在于文章中,为不可见视图构建了位图缓存。

但是,您有一个可见父级,其中包含不可见视图。当您创建父级的绘图缓存时,当然不会渲染不可见的视图。

为了显示不可见的视图,您需要在位图中自己绘制视图,然后在与父图像连接的位图内绘制该位图。

代码示例:

//these fields should be initialized before using
TextView invisibleTextView1;

TextView invisibleTextView2;

ImageView invisibleImageView;

private Bitmap getBitmap(View v) {
    Bitmap bmp = null;
    Bitmap b1 = null;
    RelativeLayout targetView = (RelativeLayout) v;
    targetView.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
    targetView.buildDrawingCache();
    b1 = targetView.getDrawingCache();
    bmp = b1.copy(Bitmap.Config.ARGB_8888, true);
    targetView.destroyDrawingCache();

    //create a canvas which will be used to draw the invisible views
    //inside the bitmap returned from the drawing cache
    Canvas fullCanvas = new Canvas(bmp);

    //create a list of invisible views
    List<View> invisibleViews = Arrays.asList(invisibleTextView1, invisibleImageView, invisibleTextView2);

    //iterate over the invisible views list
    for (View invisibleView : invisibleViews) {

        //create a bitmap the size of the current invisible view
        //in this bitmap the view will draw itself
        Bitmap invisibleViewBitmap = Bitmap.createBitmap(invisibleView.getWidth(), invisibleView.getHeight(), Bitmap.Config.ARGB_8888);

        //wrap the bitmap in a canvas. in this canvas the view will draw itself when calling "draw" 
        Canvas invisibleViewCanvas = new Canvas(invisibleViewBitmap);

        //instruct the invisible view to draw itself in the canvas we created
        invisibleView.draw(invisibleViewCanvas);

        //the view drew itself in the invisibleViewCanvas, which in term modified the invisibleViewBitmap
        //now, draw the invisibleViewBitmap in the fullCanvas, at the view's position
        fullCanvas.drawBitmap(invisibleViewBitmap, invisibleView.getLeft(), invisibleView.getTop(), null);

        //finally recycle the invisibleViewBitmap
        invisibleViewBitmap.recycle();
    }

    return bmp;
}

最后提到:

  • 如果您的隐藏视图具有visibility = View.GONE,那么在循环中调用layout(...)之前,您应该draw(...)
  • 如果不可见视图的父视图不占据整个屏幕,则不会在正确的位置绘制不可见的视图,因为getLeft()&amp; getTop()返回左侧&amp;顶部属性(以像素为单位),相对于父位置。如果您的隐形视图位于仅覆盖部分屏幕的父级内,请改为使用:

    fullCanvas.drawBitmap(invisibleViewBitmap, invisibleView.getLeft() + parent.getLeft(), v.getTop() + v.getTop(), null);

让我知道这是否有效!

相关问题