So it is not really desired to do this during oncreate though it is my thought that the animations I want to use will work much better with an image rather than a FrameLayout.
So What I have figured so far is (by reading https://stackoverflow.com/a/4406090/1815624), using ViewTreeObserver I am able to use getHeight in the function shown at the bottom which is where the error occurs where it states view.getWidth(), view.getHeight()
Really I just want to get that image of the view and nuke the original with something like removePieChart(fl);
Though as soon I used removePieChart(fl);
the errors happen...
A timed event may work but it seems like a bad idea...Any suggestions?
Please and Thanks
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);
}
and for reference here is the viewToBitmap method from https://stackoverflow.com/a/21726101/1815624
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;
}