java - 在 OnCreate 期间删除 View 之前如何获取 View 的高度?

标签 java android

因此,虽然我认为我想要使用的动画与图像而不是 FrameLayout 配合使用效果更好,但实际上并不希望在 oncreate 期间执行此操作。

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

真的,我只是想获得 View 的图像,并使用诸如 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;
}

最佳答案

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

关于java - 在 OnCreate 期间删除 View 之前如何获取 View 的高度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35100828/

相关文章:

android - 了解 measureWithLargestChild : Why breaks the layout?

java - 获取经度和纬度

Java : How to return intermediate results from a Thread

java - 带有 Spring 数据的 JPA

android - 如何在android studio中使用firebase auth删除注册用户?

android - 我们如何在 android 中拥有条件资源值?

java - 解析异常;必须被捕获 (Try/Catch) (Java)

java - 如何在列表中的另一个项目之后添加一个项目?

android - 可过滤搜索 View 上的大黑框

Android 对话框宽度