【问题标题】:Get bitmap of RelativeLayout having invisible TextView or ImageView获取具有不可见 TextView 或 ImageView 的 RelativeLayout 位图
【发布时间】:2017-08-31 05:41:50
【问题描述】:

我要创建一个相对布局的位图,该布局具有一个可见的 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

我真的在修复中。

【问题讨论】:

  • 您的链接已损坏,能否更新一下?
  • 抱歉,我已经更新了链接
  • 直到我得到解决方案,我试图通过在此链接的帮助下使用画布在位图上添加那些不可见的解决方案 - stackoverflow.com/questions/7184506/…

标签: android-bitmap drawingcache


【解决方案1】:

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

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

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

为了让您的不可见视图出现,您需要自己在位图中绘制视图,然后在延续父级的位图中绘制该位图。

代码示例:

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

最后提及:

  • 如果您的不可见视图的可见性 = View.GONE,您应该在每个视图上都使用 layout(...),然后在循环中调用 draw(...)
  • 如果你的不可见视图的父视图没有占据整个屏幕,那么你的不可见视图将不会被绘制在正确的位置,因为getLeft() & getTop() 以像素为单位返回left & top 属性,相对于父位置。如果您的不可见视图位于仅覆盖部分屏幕的父级中,请改用:

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

让我知道这是否有效!

【讨论】:

  • 非常感谢您的快速回复。
  • 请检查这些图像是否符合我的要求,并通过可见视图和更新代码获得结果。我认为它需要对布局大小和位置进行更多计算。必填:mega.nz/#!Tk5yiIgC!-jGvt07yDEJ65k19sYqivPgKBg600sZwrcJRKRmwa_s 结果:mega.nz/#!vkwBCKhI!7RcSacv8whq-vMs48FNnoDKoKWVUt1ZctETM2fVn6YE
  • 我在 for 循环中调用了 view.layout(...),在之前的答案版本中。如果您的视图是 Invisible 而不是 Gone,那么调用 view.layout(...) 是不必要的,而且它的参数错误。您使用的是我提供的代码的当前版本,还是调用layout 的代码?只是想知道如何继续
  • 另外,当您说您的视图是“隐藏的”时,您是指 View.INVISIBLE 还是 View.GONE?我之所以问,是因为 View.INVISIBLE 中的视图仍将由父视图布局,而 View.GONE 中的视图默认情况下不会,您必须自己调用 measure(...)layout(...),然后再调用 @987654334 @
  • 我正在使用 View.INVISIBLE
猜你喜欢
  • 2011-05-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-07
  • 1970-01-01
  • 2015-08-03
  • 2014-04-04
相关资源
最近更新 更多