强制刷新不可见的Android布局

时间:2012-05-31 03:27:56

标签: android android-layout

有没有人知道如何强制刷新/绘制不可见的布局?

我有一个复杂的应用程序,其中一个布局目前可能不可见,但我们想将其转换为位图并以另一个更小,缩放,可见的布局显示。

我可以轻松地将布局复制到位图,然后将该位图放入较小的可见窗口中的ImageView中。但我们遇到的问题是,如果在不可见的窗口中更改,删除或添加视图,Android实际上并没有绘制它。因此,放置在较小的可见布局中的获取位图是陈旧且静态的。

那么有没有办法强制不可见的布局进行重新绘制?

1 个答案:

答案 0 :(得分:0)

扩展LinearLayout并覆盖onMeasure功能,返回完整的版面尺寸(屏幕+屏幕外)。将此布局用作不可见的布局

此代码可能会让您入门..

public class YourLayout extends LinearLayout {
  private Context myContext;

  public YourLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
  }

  @Override
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
    /*
     *  Magic!!!  Android doesn't draw parts of layout which is offscreen. Since YourLinear layout has some offscreen part
     *  its offscreen portions didn't get drawn. 
     *  This onMeasure function determines how much pixel of a layout need to be drawn. 
     *  widthMeasureSpec        ->  Width of YourLayout onscreen
     *  heightMeasureSpec       ->  height of YourLayout on screen
     *  your_view_offscreen_width   ->  width of offscreen part
     *  your_view_offscreen_height->   height of offscreen part
     *  So heightMeasureSpec + your_view_offscreen_height draws complete height of YourLayout whether it is onscreen or offscreen. 
     *  So widthMeasureSpec + your_view_offscreen_width draws complete width of YourLayout whether it is onscreen or offscreen
     */
      super.onMeasure(widthMeasureSpec + your_view_offscreen_width, heightMeasureSpec + your_view_offscreen_height);
   }
}

现在您可以将此布局用作不可见的布局..即如果您使用xml进行布局,则可以像这样使用它

 <com.your.package.YourLayout layout_width="fill_parent" layout_height="fill_parent"
  ......
 >