【问题标题】:How can I getHeight of View before removing that View during OnCreate?在 OnCreate 期间删除该视图之前如何获取视图高度?
【发布时间】:2016-01-30 10:46:43
【问题描述】:

因此,在 oncreate 期间并不是真的希望这样做,尽管我认为我想要使用的动画在图像而不是 FrameLayout 上会更好地工作。

所以到目前为止我想到的是(通过阅读https://stackoverflow.com/a/4406090/1815624),使用 ViewTreeObserver 我可以在底部显示的函数中使用 getHeight,这是错误发生的地方,它指出 view.getWidth(), view.getHeight()

真的,我只是想获得该视图的图像并使用removePieChart(fl); 之类的内容对原始图像进行核对尽管我一使用removePieChart(fl); 就会发生错误...

定时事件可能会起作用,但这似乎是个坏主意……有什么建议吗?

请,谢谢

    protected void onCreate(Bundle savedInstanceState) {
        final FrameLayout tv = (FrameLayout)findViewById(R.id.pieChart);
        final ViewTreeObserver observer= tv.getViewTreeObserver();
        observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                View fl = findViewById(R.id.pieChart);
                image.setImageBitmap(viewToBitmap(fl));
                removePieChart(fl);
//                observer.removeOnGlobalLayoutListener(this);
            }
        });
    }
    private void removePieChart(View fl) {
        ((ViewManager)fl.getParent()).removeView(fl);
    }

这里是来自https://stackoverflow.com/a/21726101/1815624的viewToBitmap方法供参考

public Bitmap viewToBitmap(View view) {
    Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    view.draw(canvas);
    try {
        FileOutputStream output = new FileOutputStream(Environment.getExternalStorageDirectory() + "/file.png");
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, output);
        output.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return bitmap;
}

【问题讨论】:

  • 可能对你有用 fl.getMeasuredHeight()
  • 我试过了,但没有用...谢谢您的建议..还有吗?
  • onCreate() 中还没有创建好视图,所以大多数情况下你会得到 0。
  • 您无法移除尚未添加的视图
  • 知道这是首先使用监听器的原因,因为我需要它被渲染复制和销毁......

标签: java android


【解决方案1】:

我建议使用我不久前从 Square 制作的已删除 Flow-Sample 中抢夺的以下类:

import android.view.View;
import android.view.ViewTreeObserver;

public final class ViewUtils {
    public interface OnMeasuredCallback {
        void onMeasured(View view, int width, int height);
    }

    public static void waitForMeasure(final View view, final OnMeasuredCallback callback) {
        int width = view.getWidth();
        int height = view.getHeight();

        if (width > 0 && height > 0) {
            callback.onMeasured(view, width, height);
            return;
        }

        view.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            @Override public boolean onPreDraw() {
                final ViewTreeObserver observer = view.getViewTreeObserver();
                if (observer.isAlive()) {
                    observer.removeOnPreDrawListener(this);
                }

                callback.onMeasured(view, view.getWidth(), view.getHeight());

                return true;
            }
        });
    }

    private ViewUtils() {
    }
}

然后你把它当作

ViewUtils.waitForMeasure(view, new ViewUtils.OnMeasuredCallback() {
    public void onMeasured(View view, int width, int height) {
        Log.d(TAG, "Do something with height [" + height + "]");
    }
});

【讨论】:

    【解决方案2】:
       **Try this code**
    
        final LinearLayout layout = (LinearLayout) findViewById(R.id.layout);
        final ViewTreeObserver observer= layout.getViewTreeObserver();
        observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener()
        {
                @Override
                public void onGlobalLayout()
                {
                        Log.d("Log", "Height: " + layout.getHeight());
                }
        });
    

    【讨论】:

      【解决方案3】:

      首先要删除侦听器addOnGlobalLayoutListener,以阻止它重复调用removePieChart() 方法。完成后,您可以删除视图。

      所以onGlobalLayout 变了:

              public void onGlobalLayout() {
      
                  View fl = findViewById(R.id.pieChart);
                  image.setImageBitmap(viewToBitmap(fl));
                  removePieChart(this); //TODO remove the PieChart resource
              }
      

      removePieChart() 也发生了变化

      private void removePieChart(ViewTreeObserver.OnGlobalLayoutListener a) {
      
          final FrameLayout fl = (FrameLayout)findViewById(R.id.pieChart);
          final ViewTreeObserver observer= fl.getViewTreeObserver();
           observer.removeOnGlobalLayoutListener(a);
          ((ViewManager)fl.getParent()).removeView(fl);
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-03-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-08-29
        • 2011-12-08
        相关资源
        最近更新 更多