在OnCreate中删除该视图之前,如何获取View ofHeight?

时间:2016-01-30 10:46:43

标签: java android

所以我不想在onc​​reate期间这样做,尽管我认为我想要使用的动画会更好地使用图像而不是FrameLayout。

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

真的,我只想获取视图的图像并使用类似removePieChart(fl); 的内容来核对原始图像尽管我使用removePieChart(fl);时错误发生... < / p>

定时活动可能有效,但似乎是一个坏主意......有什么建议吗?

请,谢谢

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

3 个答案:

答案 0 :(得分: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());
            }
    });

答案 1 :(得分:1)

首先要删除侦听器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);
}

答案 2 :(得分:0)

我建议使用我刚刚从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 + "]");
    }
});